Skip to content

Instantly share code, notes, and snippets.

@doitian
Created December 25, 2019 08:04
Show Gist options
  • Save doitian/a2a0bfea5e01b946926449ce1b2a38fb to your computer and use it in GitHub Desktop.
Save doitian/a2a0bfea5e01b946926449ce1b2a38fb to your computer and use it in GitHub Desktop.
[Javascript for Automation ➤ Clipboard Parser] Access rich information about the clipboard #macOS #automation
ObjC.import('AppKit');
function pboardTypes() {
return ObjC.deepUnwrap(
$.NSPasteboard.generalPasteboard.pasteboardItems.js[0].types
);
}
function pboardUnpacked(strType) {
return ObjC.unwrap(
$.NSPasteboard.generalPasteboard.stringForType(
strType
)
)
}
function pboardPlist(strType) {
return ObjC.deepUnwrap(
$.NSPasteboard.generalPasteboard.propertyListForType(
strType
)
)
}
// Get both title and URL and use the "Copy Link".
function pboardURLAsMarkdown() {
// pboardTypes()
// -> ["public.url", "public.url-name", "public.utf8-plain-text"]
const title = pboardUnpacked("public.url-name");
if (title !== undefined) {
return `[${title}](${pboardUnpacked("public.url")})`;
}
}
// Get the source URL of the content copied in Safari
function pboardSourceURL() {
// pboardTypes()
// -> ["com.apple.webarchive", "public.rtf", "public.html", "public.utf8-plain-text", "com.apple.WebKit.custom-pasteboard-data", "public.utf16-external-plain-text"]
const webarchive = pboardPlist("com.apple.webarchive");
if (webarchive !== undefined) {
return webarchive.WebMainResource.WebResourceURL;
}
}
function run() {
return pboardSourceURL();
}
@humbleCoder1990
Copy link

What's the ObjC.import('AppKit') ?

Where can I get it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment