Skip to content

Instantly share code, notes, and snippets.

@uhunkler
Last active August 23, 2017 12:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save uhunkler/5465857 to your computer and use it in GitHub Desktop.
Save uhunkler/5465857 to your computer and use it in GitHub Desktop.
clipboard handling in JSTalk for Sketch
// @requires https://gist.github.com/uhunkler/5465857
var text = 'to clipboard ' + new Date(),
returnedtext = '';
clipboard.set( text );
returnedtext = clipboard.get();
test.assert( text === returnedtext, 'Text correctly copied and read to/from the clipboard' );
test.show();
/**
* Clipboard text handling with a get and set function
* clipboard.get() // get text from the clipboard
* clipboard.set( 'text' ) // set the clipboard to the given text
*
* @type {Object}
*/
var clipboard = {
// store the pasetboard object
pasteBoard : null,
// save the pasteboard object
init : function()
{
this.pasteBoard = NSPasteboard.generalPasteboard();
},
// set the clipboard to the given text
set : function( text )
{
if( typeof text === 'undefined' ) return null;
if( !this.pasteBoard )
this.init();
this.pasteBoard.declareTypes_owner( [ NSPasteboardTypeString ], null );
this.pasteBoard.setString_forType( text, NSPasteboardTypeString );
return true;
},
// get text from the clipbaoard
get : function()
{
if( !this.pasteBoard )
this.init();
var text = this.pasteBoard.stringForType( NSPasteboardTypeString );
return text.toString();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment