There are many options in macOS that allow the user to convert picture files from one format to another. I wanted to handle this a bit differently and wrote the AppleScript included below.
To install this script:
-
Paste it into the Script Editor and save as an application.
-
Open a Finder window to the folder containing the script and command-drag the script file to the Finder toolbar. This will create an icon for the script.
-
If desired, set permissions in “Security and Preferences” to allow operation of the script without macOS security prompts.
To use the script, select one or more picture files in the Finder and click on the script icon in the toolbar. A dialog will request the desired image format and the new files will be created in the same folder as the source files. Additional image formats that can be added to the script are bmp, jp2, pict, qtif, psd, sgi, and tga.
Supported image formats of source picture files are not limited to those shown in the script and those noted above (e.g. CR2 works). If an image format is not supported, the script will report an error.
--Set variables to image file formats.
set imageFormats to {"gif", "jpg", "png", "pdf", "tiff"}
set imageFormatsCaps to {"GIF", "JPG", "PNG", "PDF", "TIFF"}
--Set variable to selected files.
tell application "Finder"
set sourceFiles to selection as alias list
end tell
--Notify if file not selected.
if (count sourceFiles) = 0 then
display dialog "File not selected" buttons {"OK"} ¬
cancel button 1 default button 1 with title "Picture Converter" with icon stop
end if
--Prompt for image format of destination file.
choose from list imageFormatsCaps with title "Picture Converter" with prompt ¬
"Select an image format:" default items (item 1 of imageFormatsCaps)
--Set variable to image format of destination file.
if result ≠ false then
set destinationExtensionCaps to item 1 of result
else
error number -128
end if
--Set variable to lowercase image format of destination file.
repeat with i from 1 to (count imageFormatsCaps)
if item i of imageFormatsCaps = destinationExtensionCaps then
set destinationExtension to item i of imageFormats
exit repeat
end if
end repeat
--Loop through source files.
repeat with aFile in sourceFiles
repeat 1 times
--Set variable to POSIX path of source file.
set aFile to POSIX path of aFile
--Set variables to source and destination file values.
set AppleScript's text item delimiters to {"."}
set sourceExtension to text item -1 of aFile
set destinationFile to (text items 1 thru -2 of aFile & destinationExtension) as text
set AppleScript's text item delimiters to {"/"}
set sourceFileName to text item -1 of aFile
set destinationFileName to text item -1 of destinationFile
set AppleScript's text item delimiters to ""
--Prompt if source and destination file formats are the same.
if sourceExtension = destinationExtension then
errorDialog("Skip", "The source file " & quote & sourceFileName & quote & ¬
" is already in the " & destinationExtensionCaps & " image format.")
exit repeat
end if
--Check if destination file exists.
try
alias POSIX file destinationFile
set fileExists to true
on error
set fileExists to false
end try
--Prompt if destination file exists.
if fileExists then errorDialog("Overwrite", "The destination file " & quote & ¬
destinationFileName & quote & " already exists.")
--Set variable to image format for use in the SIPS command line.
if destinationExtension = "jpg" then
set sipsFormat to "jpeg"
else
set sipsFormat to destinationExtension
end if
--Create files.
try
do shell script "sips --setProperty format " & sipsFormat & space & ¬
quoted form of aFile & " --out " & quoted form of destinationFile
on error errorMessage
errorDialog("Continue", "The source file " & quote & sourceFileName & quote & ¬
" could not be read. The error message was:" & return & return & errorMessage)
exit repeat
end try
end repeat
end repeat
--Error dialog.
on errorDialog(buttonText, dialogText)
display dialog dialogText buttons {"Cancel", buttonText} ¬
cancel button 1 default button 1 with title "Picture Converter" with icon stop
end errorDialog