Batch converting graphic files for Mac with AppleScript and ImageMagick
It is often desirable to convert a few graphic files into other formats, for
example .eps and .pdf when you work with LaTeX. The convert utility from
the ImageMagick software suite makes conversion an easy job, but some batch
work should be done automatically, right?
This AppleScript utility, which can be saved as a MacOSX application also, simply automates the process. You select a few graphic files, you specify extensions of formats that you want to convert them to, and this script does the rest, putting the converted files (same basenames as the originals, but different formats with different extensions you specified) to where the originals are.
-- customized split function
-- From julifos @ http://macscripter.net/viewtopic.php?id=24473
to split(someText, delimiter)
set AppleScript's text item delimiters to delimiter
set someText to someText's text items
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
return someText
end split
-- From http://www.macosxautomation.com/applescript/sbrt/index.html
on remove_extension(this_name)
if this_name contains "." then
set this_name to ¬
(the reverse of every character of this_name) as string
set x to the offset of "." in this_name
set this_name to (text (x + 1) thru -1 of this_name)
set this_name to (the reverse of every character of this_name) as string
end if
return this_name
end remove_extension
on get_extension(this_name)
if this_name contains "." then
set this_name to ¬
(the reverse of every character of this_name) as string
set x to the offset of "." in this_name
--set this_name to (text (x + 1) thru -1 of this_name)
set this_name to (text 1 thru (x - 1) of this_name)
set this_name to (the reverse of every character of this_name) as string
end if
return this_name
end get_extension
-- get the raw files to convert
set fileNames to ¬
(choose file with prompt ¬
"Select the graphic files to convert." with multiple selections allowed)
if fileNames is -128 then exit repeat
-- ask for the other formats to convert to
set exts to text returned of ¬
(display dialog ¬
"Enter a list of extnesions you want to convert to, separated by space." with title "Convert to... " default answer "eps pdf")
set extItems to split(exts, {" ", " ", " ", " ", " "})
-- start converting
repeat with filename in fileNames
repeat with ext in extItems
set n to POSIX path of (filename as text)
set bn to remove_extension(n)
set nn to bn & "." & ext
set cmd to "convert " & (quoted form of n) & " " & (quoted form of nn)
-- if ext the same then skip
if not (get_extension(n) as string = ext as string) then
do shell script cmd
end if
end repeat
end repeat
beep
display dialog "Conversions successful!" with title "Congratulations!"
Note: Make sure that the ImageMagick software is correctly installed on your
system. Also, make sure that the convert utility is found in your $PATH$;
otherwise specify where it is. Also, note that if unsupported files by convert
are selected, the script may complain.
Update: It is highly recommended that ImageMagick is compiled from source, instead of using (a) the one from MacOSX itself, or (b) the one from MacPorts, or (c) the one from the ImageMagick’s binary distributions. Lots of my conversion fail but the one that I compiled by myself works best.