Last active
August 23, 2017 12:14
-
-
Save uhunkler/5465857 to your computer and use it in GitHub Desktop.
clipboard handling in JSTalk for Sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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