Created
December 25, 2019 08:04
-
-
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
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
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the ObjC.import('AppKit') ?
Where can I get it?