Picture format converter

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:

  1. Paste it into the Script Editor and save as an application.

  2. 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.

  3. 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

This is a nice script. I like the way you structured it logically, limited Finder’s involved to the absolute minimum, and used your try blocks sensibly.

As all the files get saved into a single folder (namely, the same folder in which the original files reside), one suggestion that would streamline the process and the script would be to make a single do shell script call that passes a concatenated string of file paths to the sips command and a single folder path as its –out parameter. Here’s an example of what I mean:

set format to "tiff"

tell application "Finder" to set sourceFiles to the selection as alias list
-- This just gets the folder containing the selection
set outputFolder to quoted form of POSIX path of alias (item 1 of sourceFiles & "::" as text)

set my text item delimiters to space

repeat with f in sourceFiles
		set f's contents to quoted form of POSIX path of f
end repeat

set shellCmd to {"sips --setProperty format", format, sourceFiles, "--out", outputFolder} as text

You can select a bunch of files in Finder, then run that script to see what the value of shellCmd evaluates to. The last line of this script would then be: do shell script shellCmd

CK. Thanks for looking at my script.

I rewrote my original script to include your suggestion and ran some timing tests. With a large number of files, the original script took almost twice as long to complete as the revised script. This is certainly a worthwhile improvement, and the following is the script I will use.


--Revised December 7, 2019 for Catalina compatibility.

--Set variables to image file formats.
set imageFormats to {"GIF", "JPEG", "PNG", "PDF", "TIFF"}

--Set variable to source files.
tell application "Finder" to set sourceFiles to selection as alias list

--Notify if file not selected.
if sourceFiles = {} 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 files.
choose from list imageFormats with title "Picture Converter" with prompt ¬
	"Select an image format:" default items (item 1 of imageFormats)

--Set variable to image format of destination file.
if result = false then
	error number -128
else
	set imageFormat to item 1 of result
	set imageFormat to makeLowercase(imageFormat)
end if

--Set variable to file extension to check if destination file exists.
if imageFormat = "JPEG" then
	set fileExtension to "jpg"
else
	set fileExtension to imageFormat
end if

--Check if destination file exists.
set AppleScript's text item delimiters to "."
set fileExists to false
repeat with aFile in sourceFiles
	set destinationFile to text 1 thru text item -2 of (aFile as text) & "." & fileExtension
	try
		alias destinationFile
		set fileExists to true
		exit repeat
	end try
end repeat
set AppleScript's text item delimiters to ""

--Prompt if destination file exists.
if fileExists then
	display dialog "One or more destination files already exist." buttons ¬
		{"Cancel", "Overwrite"} default button 2 with title "Picture Converter" with icon stop
end if

--Set variable to output folder for use in the sips command.
set AppleScript's text item delimiters to "/"
set outputFolder to text 1 thru text item -2 of (POSIX path of (item 1 of sourceFiles))
set AppleScript's text item delimiters to ""

--Set variable to source files with POSIX paths for use in the sips command.
repeat with aFile in sourceFiles
	set contents of aFile to quoted form of POSIX path of aFile & " "
end repeat

--Create files.
try
	do shell script "sips --setProperty format " & imageFormat & " " & sourceFiles & ¬
		"--out " & quoted form of outputFolder
on error errorMessage
	set dialogText to "The SIPS utility reported the following error:" & return & return & errorMessage
	display dialog dialogText buttons {"OK"} cancel button 1 default button 1 ¬
		with title "PictureConverter" with icon stop
end try

--Convert image format to lowercase for sips.
on makeLowercase(imageFormat)
	set upperCase to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	set lowerCase to "abcdefghijklmnopqrstuvwxyz"
	set imageFormat to characters of imageFormat
	repeat with aCharacter in imageFormat
		if aCharacter is in upperCase then
			set contents of aCharacter to (item (offset of aCharacter in upperCase) of lowerCase)
		end if
	end repeat
	return imageFormat as text
end makeLowercase