Place string on clipboard with ASObjC

I’m attempting to get the ASObjC equivalent to the following:

set the clipboard to "This is a test"

Shane has an example in his book, but it does a lot of other stuff, and I wasn’t able to modify it to copy a string to the clipboard (and nothing else). This is for learning purposes only. Thanks.

Ventura 13.1:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set nsstr to current application's NSString's stringWithString:"Hi there"
set pboard to current application's NSPasteboard's generalPasteboard
(pboard's setString:nsstr forType:(current application's NSPasteboardTypeString))

No idea why the last line has to be in parenthesis, though.

1 Like

Thanks chrillek. That worked but not always. I looked again at Shane’s script and he used the clearContents method before setting the clipboard, and that did the trick.