Write data to clipboard with a Dynamic Type Identifier

I’ve got an application that uses a Dynamic Type Identifier when you copy certain type of data. The actual data is XML, but the app doesn’t label it that way in the clipboard.
I’m able to read it out, but I’m having trouble writing it back.
Here’s example code:

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

set dynType to "dyn.12345"
set someData to "My Test Data"
set pasteboard to current application's NSPasteboard's generalPasteboard()
pasteboard's clearContents()
pasteboard's setData:someData forType:dynType

When I run this, I get the following error:

error "Calling -setData:forType: on NSPasteboard or NSPasteboardItem with object of type __NSCFString instead of NSData." number -10000

Any idea on how I can properly define the type?

Your someData is string while setData: requires NSData.

You need to use setString:forType: instead (or convert your string to NSData if you must use data for specific reasons).

1 Like

Thank you! I was misreading the error message, and thought it applied to what I specified in the forType param, not the setData param. It turns out that using setString worked well, and then my dynamic type identifier properly set the type in the clipboard for the app to paste it.