Sunday, February 23, 2020

#1 2020-01-28 08:41:19 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Save clipboard image to desktop file

This script saves an image on the clipboard to a file on the desktop. It prompts the user for a file name with extension and, optionally, for a maximum image width/height in pixels. Valid extensions and the resulting image formats are  GIF, JPG, PNG, and TIF.

Examples of user input are

file name.jpg
file name.png 800



The motivation for this script was:

https://macscripter.net/viewtopic.php?id=47361

The basic procedure employed in this script was obtained from:

https://discussions.apple.com/thread/2379870

Applescript:


main()

on main()
   
   display dialog "Enter a file name with extension and, if desired, a maximum image width/height." default button 2 default answer "clipboard image.png" with title "Clipboard Image Save"
   set userInput to text returned of result
   if userInput = "" then errorDialog("A file name was not entered.")
   
   try
       set text item delimiters to " "
       set imageSize to ((text item -1 of userInput) as integer) as text
       set fileName to text 1 thru text item -2 of userInput
       set text item delimiters to ""
       set changeSize to true
   on error
       set fileName to userInput
       set changeSize to false
   end try
   
   set text item delimiters to "."
   set fileExtension to text item -1 of fileName
   set text item delimiters to ""
   if fileExtension is not in {"gif", "jpg", "png", "tif"} then errorDialog("A valid file name or extension was not entered. Supported file extensions are gif, jpg, png, and tif.")
   
   set filePath to ((path to desktop) as text) & fileName
   set filePosixPath to quoted form of POSIX path of filePath
   
   try
       alias filePath
       set fileExists to true
   on error
       set fileExists to false
   end try
   
   if fileExists then display dialog "A file exists with the name " & quote & fileName & quote buttons {"Cancel", "Overwrite"} default button 2 with title "Clipboard Image Save"
   
   try
       if fileExtension = "gif" then
           set theImage to the clipboard as GIF picture
       else if fileExtension = "jpg" then
           set theImage to the clipboard as JPEG picture
       else if fileExtension = "png" then
           set theImage to the clipboard as {«class PNGf»} # thanks Fredrik71
       else if fileExtension = "tif" then
           set theImage to the clipboard as TIFF picture
       end if
   on error
       errorDialog("An image was not found on the clipboard.")
   end try
   
   try
       set openedFile to open for access filePath with write permission
       write theImage to openedFile
       close access openedFile
   on error
       try
           close access openedFile
       end try
   end try
   
   if changeSize then do shell script "sips --resampleHeightWidthMax " & imageSize & " " & filePosixPath
   
end main

on errorDialog(dialogText)
   display dialog dialogText buttons "OK" cancel button 1 default button 1 with title "Clipboard Image Save" with icon stop
end errorDialog

Last edited by peavine (2020-02-11 05:18:30 pm)


2018 Mac mini - macOS Catalina

Offline

 

#2 2020-02-02 03:12:39 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 351

Re: Save clipboard image to desktop file

This script is similar in operation to the script contained above but differs in that it:

* works on the file selected in the Finder;

* is faster in operation; and

* reads and writes PDF image files in addition to the other image formats.


Applescript:


main()

on main()
   
   set imageFormats to {"gif", "jpg", "pdf", "png", "tif"}
   
   set tempPosixFile to quoted form of "/tmp/Image Convert.tmp"
   
   tell application "Finder"
       set sourceFile to selection as alias list
       try
           set sourceFileName to name of item 1 of sourceFile
           set sourceFileExtension to name extension of item 1 of sourceFile
       end try
   end tell
   
   if (count sourceFile) ≠ 1 then errorDialog("Multiple or no files selected.")
   
   set sourceFile to item 1 of sourceFile as text
   set sourcePosixFile to quoted form of POSIX path of sourceFile
   
   if sourceFileExtension is not in imageFormats then errorDialog("The image format of the selected file is not supported. Valid formats are gif, jpg, png, pdf, and tif.")
   
   set sourceImageSize to paragraphs of (do shell script "sips -g pixelWidth -g pixelHeight " & sourcePosixFile)
   set text item delimiters to " "
   set sourceImageWidth to text item -1 of item 2 of sourceImageSize
   set sourceImageHeight to text item -1 of item 3 of sourceImageSize
   set text item delimiters to " "
   
   display dialog "Enter a file name with extension and, if desired, a maximum image width/height (existing is " & sourceImageWidth & "/" & sourceImageHeight & " pixels)." default button 2 default answer sourceFileName with title "Finder Image Convert"
   set userInput to text returned of result
   if userInput = "" then errorDialog("No text was entered.")
   
   try
       set text item delimiters to " "
       set targetImageSize to ((text item -1 of userInput) as integer) as text
       set targetFileName to text 1 thru text item -2 of userInput
       set text item delimiters to ""
       set changeSize to true
   on error
       set targetFileName to userInput
       set changeSize to false
   end try
   
   set text item delimiters to "."
   set targetFileExtension to text item -1 of targetFileName
   set text item delimiters to ""
   
   if targetFileExtension is not in imageFormats then errorDialog("A valid file name or extension was not entered. Supported file extensions are gif, jpg, png, pdf, and tif.")
   
   if sourceFileExtension = targetFileExtension then
       set changeFormat to false
   else
       set changeFormat to true
   end if
   
   set targetFile to ((path to desktop) as text) & targetFileName
   set targetPosixFile to quoted form of POSIX path of targetFile
   
   try
       alias targetFile
       set fileExists to true
   on error
       set fileExists to false
   end try
   
   if fileExists then display dialog "A file exists with the name " & quote & targetFileName & quote buttons {"Cancel", "Overwrite"} default button 2 with title "Finder Image Convert"
   
   do shell script "cp -f " & sourcePosixFile & " " & tempPosixFile
   
   if targetFileExtension = "jpg" then
       set targetFileExtension to "jpeg"
   else if targetFileExtension = "tif" then
       set targetFileExtension to "tiff"
   end if
   
   if changeFormat and changeSize then
       do shell script "sips --setProperty format " & targetFileExtension & " --resampleHeightWidthMax " & targetImageSize & " " & tempPosixFile
   else if changeFormat then
       do shell script "sips --setProperty format " & targetFileExtension & " " & tempPosixFile
   else if changeSize then
       do shell script "sips --resampleHeightWidthMax " & targetImageSize & " " & tempPosixFile
   end if
   
   try
       do shell script "mv -f " & tempPosixFile & " " & targetPosixFile
   on error
       errorDialog("An error occurred while writing the image file to the desktop.")
   end try
   
end main

on errorDialog(dialogText)
   display dialog dialogText buttons "OK" cancel button 1 default button 1 with title "Finder Image Convert" with icon stop
end errorDialog

Last edited by peavine (2020-02-11 05:17:54 pm)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)