diff --git a/.flowconfig b/.flowconfig index a8b7131..2f63d21 100644 --- a/.flowconfig +++ b/.flowconfig @@ -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 diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..9b1a69d --- /dev/null +++ b/.npmignore @@ -0,0 +1,10 @@ +misc/ +node_modules +*.tgz +.gitignore +.eslintrc +.jshintrc +Makefile +.npmignore +.jscs.json +.flowconfig diff --git a/LICENSE b/LICENSE index 9b778e0..56697e6 100644 --- a/LICENSE +++ b/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. diff --git a/README.md b/README.md index c88bea5..6af9db5 100644 --- a/README.md +++ b/README.md @@ -10,31 +10,43 @@ 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 +```bash +$ pb [type] +$ pb -m +``` 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 @@ -42,12 +54,13 @@ 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(); diff --git a/bin/pboard.njs b/bin/pboard.njs index d6d3136..800e89b 100755 --- a/bin/pboard.njs +++ b/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('../'); @@ -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; }); diff --git a/misc/flow.js b/misc/flow.js new file mode 100644 index 0000000..54ac8ce --- /dev/null +++ b/misc/flow.js @@ -0,0 +1,3 @@ +/*:: +declare module 'nodobjc' { declare var exports:any; } +*/ diff --git a/package.json b/package.json index a139414..e1e3c98 100644 --- a/package.json +++ b/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" ], diff --git a/pb.js b/pb.js index 03970f8..9bbd3b4 100644 --- a/pb.js +++ b/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*/ { 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*/ { 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); @@ -54,3 +55,15 @@ exports.set = function set(type/*:string*/, str/*:string*/)/*:void*/ { return x('setString', data, 'forType', t); } }; + +function NSArray2ArrayStr(arr)/*:Array*/ { + var out/*:Array*/ = []; + var len = arr('count'); + for(var i = 0; i < len; ++i) out[i] = arr('objectAtIndex', i).toString(); + return out; +} + +exports.available = function avail()/*:Array*/ { + var x = $.NSPasteboard('generalPasteboard'); + return NSArray2ArrayStr(x('types')); +};