While automating a prepress workflow I lately needed a script to convert EPS to high quality/resolution TIFF files. Of course I could have easily used Photoshop for this task, but I wanted to avoid to spent $699 for Adobe’s image editor and decided to use the built-in tools of Mac OS X.
The result is a script named epsotiff, which I am happily sharing with you:
epsotiff was successfully tested on Intel and PowerPC based Macs running Mac OS X 10.4, 10.5 and 10.6.
The script accomplishes the conversion by executing the following steps:
- Converting the EPS file into a PDF file with the built-in pstopdf command
- Setting a background color for each page of the PDF file with bgpdf
- Scaling the PDF file with scalepdf
- Converting the modified PDF to TIFF with the built-in sips command
epsotiff saves the generated TIFF file in the same folder as the original EPS file and uses the following naming scheme:
EPS file name: newspaper_0909.eps
TIFF file name: newspaper_0909.tiff
The script overwrites existing files without asking.
The scripts logs successful conversions, warnings and errors to different log files located in a folder named Logs, which is created in the parent folder of
the script itself. epsotiff does not display any alerts or dialogs to the user.
Successfully created TIFF files do get a green Finder label.
If the pstopdf command generated a log file (e.g. missing fonts), the created TIFF file does get a yellow Finder label.
Happy tiffing
Important: Opening and saving the below script code in Script Editor won’t result in a usable AppleScript! That is because epsotiff internally relies on foundation tools, which are located inside its bundle. Therefor please download the complete script here.
-- author: Martin Michel
-- created: 06.09.09
-- version: 0.5
-- tested on:
-- + Intel and PowerPC based Macs
-- + Mac OS X 10.4, 10.5 & 10.6
-- requires:
-- + Mac OS X 10.4 or higher
-- uses:
-- + bgpdf
-- + scalepdf
property mytitle : "epsotiff"
-- This AppleScript droplet converts dropped EPS files to TIFF files.
-- The conversion settings can be adjusted with the script properties below.
--
-- The conversion process comprises the following steps:
--
-- 1) Converting the EPS file into a PDF file with the built-in pstopdf command
-- 2) Setting a background color for each page of the PDF file with bgpdf (foundation tool)
-- 3) Scaling the PDF file with scalepdf (foundation tool)
-- 4) Converting the modified PDF to TIFF with the built-in sips command
--
-- EPS2TIFF saves the generated TIFF file in the same folder as the original EPS file
-- and uses the following naming scheme:
-- EPS file name: newspaper_0909.eps
-- TIFF file name: newspaper_0909.tiff
--
-- The script overwrites existing files without asking.
-- The scripts logs successful conversions, warnings and errors to different log files
-- located in a folder named Logs, which is created in the parent folder of
-- the script itself. EPS2TIFF does not display any alerts or dialogs to the user.
--
-- Successfully created TIFF files do get a green Finder label.
-- If the pstopdf command generated a log file (e.g. missing fonts), the
-- created TIFF file does get a yellow Finder label.
-- Adjust the values below to your own requirements:
-- PDF background color values
-- 0.0 - 1.0
property redcolval : "1.0"
property bluecolval : "1.0"
property greencolval : "1.0"
property alphachannelval : "1.0"
-- PDF scale factors
property scalefactorwidth : "2.031"
property scalefactorheight : "2.031"
-- DPI values
property dpiheight : "304.8"
property dpiwidth : "304.8"
-- Color profile
-- POSIX PATH!
property colorprofilepath : "/System/Library/ColorSync/Profiles/Generic CMYK Profile.icc"
-- Log files folder path
-- leave this as it is
property logsfolderpath : missing value
-- I am called when the user opens the script with a double click
on run
tell me
activate
display dialog "I am an AppleScript droplet." & return & return & "Drop EPS files onto my icon to convert them into TIFF files." & return & return & "Open me in AppleScript Editor to adjust the conversion settings." buttons {"OK"} default button 1 with icon note with title mytitle
end tell
end run
-- I am called when the user drops Finder items onto the script's icon
on open finderitems
-- initializing the script
set {initsuccess, errmsg} to my initialize()
if initsuccess is false then
my logmsg(errmsg, "--", "error")
return
end if
-- scanning dropped Finder items for EPS files
set epsfilepaths to {}
repeat with finderitem in finderitems
set finderiteminfo to info for finderitem
if not folder of finderiteminfo and name of finderiteminfo ends with ".eps" then
set epsfilepaths to epsfilepaths & (finderitem as Unicode text)
end if
end repeat
-- no EPS files found :-(
if epsfilepaths is {} then
set errmsg to "You did not drop any EPS files (*.eps) onto the script"
my logmsg(errmsg, "--", "error")
return
end if
-- processing found EPS files
repeat with epsfilepath in epsfilepaths
try
set starttime to current date
-- converting from EPS to PDF
set pstopdfwarning to "No"
set qtdepsfilepath to quoted form of POSIX path of epsfilepath
set tmppdffilepath to my gettmpfilepath("pdf")
set qtdtmppdffilepath to quoted form of POSIX path of tmppdffilepath
set logfilepath to tmppdffilepath & ".log"
set command to "/usr/bin/pstopdf " & qtdepsfilepath & " -o " & qtdtmppdffilepath & " -l"
do shell script command
-- did the pstopdf command generate a log (e.g. missing/replaced fonts)?
if my itempathexists(logfilepath) then
set pstopdfwarning to "Yes"
set filecont to read (logfilepath as alias)
set warnmsg to "File: " & epsfilepath & return & filecont
my logmsg(warnmsg, "--", "warning")
end if
-- setting the background color of the PDF
set mypath to (path to me) as Unicode text
set ftoolpath to mypath & "Contents:Resources:bgpdf"
set qtdftoolpath to quoted form of POSIX path of ftoolpath
set bgcpdffilepath to my gettmpfilepath("pdf")
set qtdbgcpdffilepath to quoted form of POSIX path of bgcpdffilepath
set command to qtdftoolpath & " -red " & redcolval & " -green " & greencolval & " -blue " & bluecolval & " -alpha " & alphachannelval & " -in " & qtdtmppdffilepath & " -out " & qtdbgcpdffilepath
set shelloutput to do shell script command
-- catching errors
if shelloutput is not "" then
do shell script "rm " & qtdtmppdffilepath
error shelloutput number "42999"
end if
-- scaling the PDF
set ftoolpath to mypath & "Contents:Resources:scalepdf"
set qtdftoolpath to quoted form of POSIX path of ftoolpath
set sclpdffilepath to my gettmpfilepath("pdf")
set qtdsclpdffilepath to quoted form of POSIX path of sclpdffilepath
set command to qtdftoolpath & " -sfw " & scalefactorwidth & " -sfh " & scalefactorheight & " -in " & qtdbgcpdffilepath & " -out " & qtdsclpdffilepath
set shelloutput to do shell script command
-- catching errors
if shelloutput is not "" then
do shell script "rm " & qtdbgcpdffilepath
error shelloutput number "42999"
end if
-- converting from PDF to TIFF
set tifffilepath to ((characters 1 through -5 of epsfilepath) as Unicode text) & ".tiff"
set qtdtifffilepath to quoted form of POSIX path of tifffilepath
set command to "/usr/bin/sips -s format tiff -s formatOptions best -s dpiHeight " & dpiheight & " -s dpiWidth " & dpiwidth & " -m " & quoted form of colorprofilepath & " --out " & qtdtifffilepath & " " & qtdsclpdffilepath
set shelloutput to do shell script command
-- how long did the conversion take?
set endtime to current date
set processtime to endtime - starttime
delay 1
-- removing temporary files
set command to "rm " & qtdtmppdffilepath & space & qtdbgcpdffilepath & space & qtdsclpdffilepath
do shell script command
-- coloring TIFF files
if pstopdfwarning is equal to "Yes" then
-- pstopdf warnings? -> yellow
tell application "Finder"
set label index of (tifffilepath as alias) to 3
end tell
else
-- no warnings? -> green
tell application "Finder"
set label index of (tifffilepath as alias) to 4
end tell
end if
-- logging successful conversion (plus parameters)
set convmsg to "Converted: " & return & epsfilepath & return & ">>>" & return & tifffilepath & return & "BACKGROUND" & return & "Red: " & redcolval & return & "Green: " & greencolval & return & "Blue: " & bluecolval & return & "Alpha: " & alphachannelval & return & "SCALING" & return & "Width scale factor: " & scalefactorwidth & return & "Height scale factor: " & scalefactorheight & return & "TIFF" & return & "dpi height: " & dpiheight & return & "dpi width: " & dpiwidth & return & "Color profile: " & colorprofilepath & return & "WARNINGS" & return & "pstopdf: " & pstopdfwarning & return & "TIME" & return & processtime & " seconds"
my logmsg(convmsg, "--", "converted")
on error errmsg number errnum
-- something went wrong
set errmsg to "File: " & epsfilepath & return & errmsg
my logmsg(errmsg, errnum, "error")
end try
end repeat
end open
-- I am indicating if a given item path exists
on itempathexists(itempath)
try
set itemalias to itempath as alias
return true
on error
return false
end try
end itempathexists
-- I am returning a path to an unused temporary file
on gettmpfilepath(suffix)
set tmpfolderpath to (path to temporary items folder) as text
repeat
set randnum to random number from 10000 to 99999
set tmpfilepath to tmpfolderpath & randnum & "." & suffix
try
set tmpfilealias to tmpfilepath as alias
on error
exit repeat
end try
end repeat
return tmpfilepath
end gettmpfilepath
-- I am returning the parent folder path of a given item path
on getparentfolderpath(itempath)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set itemcount to (count text items of itempath)
set lastitem to the last text item of itempath
if lastitem = "" then
set itemcount to itemcount - 2 -- folder path
else
set itemcount to itemcount - 1 -- file path
end if
set parentfolderpath to text 1 thru text item itemcount of itempath & ":"
set AppleScript's text item delimiters to oldDelims
return parentfolderpath
end getparentfolderpath
-- I am initializing the script
on initialize()
set mypath to ((path to me) as text)
set parentfolderpath to my getparentfolderpath(mypath)
set logsfolderpath to parentfolderpath & "Logs:"
set folderpaths to {logsfolderpath}
repeat with folderpath in folderpaths
if not my itempathexists(folderpath) then
try
do shell script "mkdir -p " & (quoted form of POSIX path of folderpath)
on error
set errmsg to "The following folder could not be created:" & return & folderpath
return {false, errmsg}
end try
end if
end repeat
return {true, missing value}
end initialize
-- I am logging messages to a file
on logmsg(msg, num, type)
set curdatestr to ((current date) as string)
set logentry to "[" & curdatestr & "]" & tab & msg & " (" & num & ")" & return
set logfilepath to logsfolderpath & mytitle & "_" & type & ".log"
if my itempathexists(logfilepath) then
set logfileinfo to info for (logfilepath as alias)
set logfilesize to size of logfileinfo
-- verschieben von zu grossen Logdateien ins Archivverzeichnis
if logfilesize is greater than 1048576 then
try
set timestamp to do shell script "date \"+%Y%m%d_%H%M%S\""
do shell script "mkdir -p " & quoted form of POSIX path of (logsfolderpath & "old:")
set oldlogfilepath to logsfolderpath & "old:" & timestamp & "_" & mytitle & "_" & type & ".log"
set command to "mv " & quoted form of POSIX path of logfilepath & " " & quoted form of POSIX path of oldlogfilepath
do shell script command
end try
end if
end if
try
set openlogfile to open for access logfilepath with write permission
write logentry to openlogfile starting at eof
close access openlogfile
on error
try
close access openlogfile
end try
return false
end try
return true
end logmsg