Skip to content

Instantly share code, notes, and snippets.

@TBD
Last active December 13, 2018 13:55
Show Gist options
  • Save TBD/18b1297690de0fa5c2f268153b690b7f to your computer and use it in GitHub Desktop.
Save TBD/18b1297690de0fa5c2f268153b690b7f to your computer and use it in GitHub Desktop.
[macOS] save clipboard PNG or TIFF to current Finder window

what: save clipboard PNG or TIFF images to current Finder window

steps:

  1. Automator | New Quick Action
  2. drag from Utilities Run Javascript
  3. paste content from clipboard2file.js inside run function

tips:

ObjC.import('AppKit');
// --- setup
var app = Application.currentApplication()
var Finder = Application("Finder")
app.includeStandardAdditions = true
// --- custom setup
var filenamePrefix = "clipboard"
// --- main
var currentPath = posixPath(Finder.finderWindows[0]) + "/"
var hasPNG = clipboardTypes().includes("public.png")
var hasTIFF = clipboardTypes().includes("public.tiff")
if (hasPNG) { saveClipboardDataForType(".png") }
if (hasTIFF) { saveClipboardDataForType(".tiff") }
if (!hasPNG && !hasTIFF) {
app.say("no images in clipboard")
}
// +++ helper functions
// --- save binary data to file for specific type
function saveClipboardDataForType(extension) {
var fullName = currentPath + filenamePrefix + extension
clipboardDataForType("public" + extension).writeToFileAtomically(fullName, true)
// --- soundName from aiff files on /System/Library/Sounds/
app.displayNotification(fullName, {soundName: "Pop"})
}
// --- get binary data from clipboard for specific type
function clipboardDataForType(type){
var item = $.NSPasteboard.generalPasteboard.pasteboardItems.js[0]
return item.dataForType(type)
}
// --- array of item types in clipboard
function clipboardTypes() {
return ObjC.deepUnwrap($.NSPasteboard.generalPasteboard.pasteboardItems.js[0].types)
}
// --- by StackOverflow @mklement0
function posixPath(finderWindow) {
return $.NSURL.alloc.initWithString(finderWindow.target.url()).fileSystemRepresentation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment