Skip to content

Commit

Permalink
version bump 0.3.1: RTF + available()
Browse files Browse the repository at this point in the history
  • Loading branch information
SheetJSDev committed Jun 13, 2017
1 parent a4960e3 commit 6ae378e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .flowconfig
Expand Up @@ -5,5 +5,10 @@
pb.js

[libs]
misc/flow.js

[options]
module.file_ext=.js
module.file_ext=.njs
module.ignore_non_literal_requires=true
suppress_comment= \\(.\\|\n\\)*\\$FlowIgnore
10 changes: 10 additions & 0 deletions .npmignore
@@ -0,0 +1,10 @@
misc/
node_modules
*.tgz
.gitignore
.eslintrc
.jshintrc
Makefile
.npmignore
.jscs.json
.flowconfig
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (C) 2013-2015 SheetJS
Copyright (C) 2013-present SheetJS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
29 changes: 21 additions & 8 deletions README.md
Expand Up @@ -10,44 +10,57 @@ provide a more convenient way to access the pasteboard from node.

With [npm](https://www.npmjs.org/package/pb), global installation is easiest:

$ npm install -g pb
```bash
$ npm install -g pb
```

Depending on your system configuration, you may need to run as root:

$ sudo npm install -g pb
```bash
$ sudo npm install -g pb
```

## Command-Line Tool Usage

To get data from a specific pasteboard:

$ pb [type]
$ pb -m <type>
```bash
$ pb [type]
$ pb -m <type>
```

To set a specific pasteboard, be sure to pipe data into pb:

$ get_data | pb -s [type]
```bash
$ get_data | pb -s [type]
```

To list available pasteboards:

$ pb -l
```bash
$ pb -l
```

For example, to grab the contents of the HTML pasteboard and put it on the
plaintext pasteboard:

$ pb -m html | pb -s
```bash
$ pb -m html | pb -s
```

## Library Usage

From node, `pb` exposes:

- `get(type)`: get pasteboard data from specified pasteboard
- `set(type, data)`: set pasteboard data (overwrites other pasteboards)
- `available()`: enumerate populated pasteboards
- `gettypes()`: enumerate available pasteboards

For example, to grab the contents of the HTML pasteboard and put it on the
plaintext pasteboard:

```
```js
var pb = require('pb');
var HTMLOutput = pb.get('html');
var textOutput = pb.get();
Expand Down
4 changes: 2 additions & 2 deletions bin/pboard.njs
@@ -1,5 +1,5 @@
#!/usr/bin/env node
/* pb.js (C) 2013-2015 SheetJS -- http://sheetjs.com */
/* pb.js (C) 2013-present SheetJS -- http://sheetjs.com */
var n = "pb";
/* vim: set ts=2 ft=javascript: */
var X = require('../');
Expand All @@ -20,7 +20,7 @@ program.parse(process.argv);
var type = program.mode || program.args[0] || 'text';
if(!X.mytype(type)[0]) throw new Error("Unsupported type " + type);

if(program.listModes) { console.log(X.gettypes().join("\n")); return; }
if(program.listModes) { console.log(X.gettypes().join("\n")); process.exit(0); }
if(program.set) {
var data = "";
process.stdin.on('data', function(d) { data += d; });
Expand Down
3 changes: 3 additions & 0 deletions misc/flow.js
@@ -0,0 +1,3 @@
/*::
declare module 'nodobjc' { declare var exports:any; }
*/
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pb",
"version": "0.3.0",
"version": "0.3.1",
"author": "SheetJS",
"description": "Interface to OSX Pasteboard",
"keywords": [ "osx", "mac", "pasteboard", "clipboard" ],
Expand Down
31 changes: 22 additions & 9 deletions pb.js
@@ -1,34 +1,35 @@
/* pb.js -- (C) 2013-2015 SheetJS -- http://sheetjs.com */
/* pb.js -- (C) 2013-present SheetJS -- http://sheetjs.com */
/* vim: set ts=2: */
/*jshint node:true */
var $ = require('nodobj'+'c');
var $ = require('nodobjc');
$.framework('AppKit');
$.NSAutoreleasePool('alloc')('init');

var basetypes = [ 'HTML', 'PNG', 'TEXT' ];
exports.version = '0.3.1';

var basetypes = [ 'HTML', 'PNG', 'TEXT', 'RTF' ];
var alltypes;

var mytype = exports.mytype = function mytype(type/*:string*/)/*:Array<any>*/ {
if(!type) return [$.NSStringPboardType, 's'];
switch(type.toUpperCase()) {
case 'HTML': return [$.NSHTMLPboardType, 's'];
case 'PNG': return [$.NSHTMLPboardType, 'i'];
case 'TEXT': return [$.NSStringPboardType, 's'];
case 'HTML': return [$.NSHTMLPboardType, 's'];
case 'PNG': return [$.NSHTMLPboardType, 'i'];
case 'TEXT': return [$.NSStringPboardType, 's'];
case 'RTF': return [$.NSRTFPboardType, 's'];
}
return [$[type], 's'];
};

var filterfunc = function(x/*:string*/)/*:boolean*/ { return !!(x.match(/^NS.*PboardType$/)); };
exports.gettypes = function gettypes()/*:Array<any>*/ {
if(!alltypes) {
var filterfunc = function(x) { return x.match(/^NS.*PboardType$/); };
var nstypes = Object.keys($).filter(filterfunc);
alltypes = basetypes.concat(nstypes);
}
return alltypes;
};

exports.version = '0.3.0';

exports.get = function get(type/*:string*/)/*:any*/ {
var x = $.NSPasteboard('generalPasteboard');
var tt = mytype(type);
Expand All @@ -54,3 +55,15 @@ exports.set = function set(type/*:string*/, str/*:string*/)/*:void*/ {
return x('setString', data, 'forType', t);
}
};

function NSArray2ArrayStr(arr)/*:Array<string>*/ {
var out/*:Array<string>*/ = [];
var len = arr('count');
for(var i = 0; i < len; ++i) out[i] = arr('objectAtIndex', i).toString();
return out;
}

exports.available = function avail()/*:Array<string>*/ {
var x = $.NSPasteboard('generalPasteboard');
return NSArray2ArrayStr(x('types'));
};

0 comments on commit 6ae378e

Please sign in to comment.