|
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 |
|
} |