Thursday, February 25, 2021

#1 2020-11-04 06:41:46 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Save multiply image format from NSPasteboard

Hi All.

I decided to edit a script from Shane I used for Core Image.
The script use NSPasteboard to output different image format.

It does work but could the speed be faster ??

EDIT: After a reboot of my machine it was much faster, sorry for false alarm. smile

Workflow: Copy Image from Internet and run the script. (it will ask where you like it to be saved)

Applescript:

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

set theTarget to POSIX path of (choose file name with prompt "" default name "image_001")

set pasteboard to current application's NSPasteboard's generalPasteboard()
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeTIFF)

saveImageDataToFormat(theData, theTarget, "bmp")
saveImageDataToFormat(theData, theTarget, "gif")
saveImageDataToFormat(theData, theTarget, "jpg")
saveImageDataToFormat(theData, theTarget, "png")
saveImageDataToFormat(theData, theTarget, "tif")

on saveImageDataToFormat(inputData, thePath, formatType)
   set thePath to thePath as string
   if (inputData = missing value) then error "No data found on clipboard"
   set imageRep to current application's NSBitmapImageRep's imageRepWithData:inputData
   if (formatType = "bmp") then
       set imageData to (imageRep's representationUsingType:(current application's NSBMPFileType) |properties|:(missing value))
   else if (formatType = "gif") then -- include Alpha channel
       set imageData to (imageRep's representationUsingType:(current application's NSGIFFileType) |properties|:(missing value))
   else if (formatType = "jpg") then
       set imageData to (imageRep's representationUsingType:(current application's NSJPEGFileType) |properties|:(missing value))
   else if (formatType = "png") then -- include Alpha channel
       set imageData to (imageRep's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value))
   else if (formatType = "tif") then -- include Alpha channel
       set imageData to (imageRep's representationUsingType:(current application's NSTIFFFileType) |properties|:(missing value))
   else
       error number 128
   end if
   
   set outputPath to thePath & "." & formatType
   imageData's writeToFile:outputPath atomically:true
end saveImageDataToFormat

Last edited by Fredrik71 (2020-11-04 08:21:14 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)