Scripting Photoshop
Scripting Photoshop
- Subject: Scripting Photoshop
- From: Eric Schult <email@hidden>
- Date: Tue, 03 Apr 2001 17:16:45 -0500
This is as much a Photoshop question as it is a scripting issue.
I've written a script that converts EPSed ads into PDFs and then PDFs into
GIFs as part of an ad tracking solution for the newspaper I work for. I'm
working around the limitations of Photoshop's "do script" command by making
a "temp.pdf" file out of each PDF, performing an action that GIFs the PDF
into another folder as "temp.gif", and then renames the "temp.gif" back to
the ad's original EPS filename (but with a .gif suffix).
It works nicely, but I occasionally run into a a phenomenon that stumps me.
While the script is looping through all the PDFs to be converted, one or
more of the Photoshop Actions I wrote "forget" the filename or/and path of
the temp.pdf and instead opens a previously GIFed ad. The Action then
proceeds to convert and name that ad with the current ad's ticket number.
Or, the correct PDF is opened and converted, but the Action "forgets" the
fileName or/and path of the temp.gif, and subsequently saves it into the
wrong directory or under the wrong filename.
If either or both of the above errors occur, it will perpetuate until I
re-record the Action in Photoshop. Thereafter, I might not see the problem
again for days or weeks. If I run the Action independently of my script,
(after the script has corrupted the Action), it misbehaves similarly, so I
know it's the Action that is the culprit, not a scripting error. However,
the Actions seem to go bad only when the script has been looping through
multiple PDF conversions in Photoshop. I'm forced to conclude it's my script
that is causing the Actions to lose their paths.
I've saved backups of my Photoshop Actions and preferences, but replacing
them after an Action is corrupted doesn't help. Only re-recording the Action
does the trick. This is very annoying.
I know I could change the script to employ PhotoScripter to do the
conversions, rather than depend on Photoshop Actions and the "do script"
command, but I only have a (limited use) artist's license for PhotoScripter
and haven't really gotten the hang of PhotoScripter anyway. Besides, an
earlier incarnation of this script used to work without a hitch. I'm sure
I've just written in some problematic scripting instruction for which
Photoshop has a profound aversion.
Maybe somebody out there can give me a hint or two where my problem lies.
The script fragment in question follows. There's still a lot of stuff going
on below that I haven't described above. It's the lines immediately before
and after the "do script" commands in Photoshop that I'm thinking are
causing the problem, though.
I should note that the script runs from inside a FileMaker database, thus
the references to objects like "cells" and "records" in that application.
Appreciatively,
wes
------
<...snip>
(* CHECK FOR PRESENCE OF A GIF FOR THIS AD AND SET STATUS ACCORDINGLY. *)
tell application "Finder"
if (exists (item (gifFolder & adNumber & ".gif"))) true then ,
-- there's no GIF present
set uploadNeeded to true
-- indicates GIF will need to be re-uploaded, even if it's a pickup
tell application "FileMaker Pro"
activate
set cell "GIF Status" of record i of document 1 to "Not"
end tell
if (exists (item (pdfOutFolder & adNumber & ".pdf"))) = true then
-- the PDF is present
-- so let's create a GIF from the PDF
set gifDone to false
tell application "FileMaker Pro"
activate
try
display dialog "The PDF for ad \"" & adNumber & ,
"\" is ready, but it has not yet been GIFed." & ,
return & return & "Doing GIF conversion now ..." ,
with icon note giving up after 5
on error
return
end try
set adNumber to cell "Ad Number" of record i of document 1
set colWidth to cell "Column Width" of record i of document 1
tell application "Finder"
if colWidth "" then
set colWidth to colWidth as integer
try -- handles error if residual "temp.pdf" exists
deleteFile (pdfOutFolder & ,
(tempFile & ".pdf" as string)) ,
-- requires Jon's commands
update folder pdfOutFolder
end try
try -- handles error if residual "temp.pdf" exists
deleteFile (gifFolder & (tempFile & ,
".gif" as string))
update folder gifFolder
end try
duplicate file (pdfOutFolder & adNumber & ".pdf")
set name of file (pdfOutFolder & adNumber & ".pdf" & ,
" copy" as string) to ,
((tempFile & ".pdf") as string)
update folder gifFolder
tell application "Adobe. Photoshop. 6.0" activate
if setProps = "primary" then
if colWidth = 1 then
try
do script "PDF to GIF (1 col)"
end try
else if colWidth = 2 then
try
do script "PDF to GIF (2 col)"
end try
else if colWidth = 3 then
try
do script "PDF to GIF (3 col)"
end try
else if colWidth = 4 then
try
do script "PDF to GIF (4 col)"
end try
else if colWidth = 5 then
try
do script "PDF to GIF (5 col)"
end try
else if colWidth > 5 then
try
do script "PDF to GIF (6 col)"
end try
end if
else if setProps = "secondary" then
if colWidth = 1 then
try
do script "PDF to GIF (1 col ad)"
end try
else if colWidth = 2 then
try
do script "PDF to GIF (2 col ad)"
end try
else if colWidth = 3 then
try
do script "PDF to GIF (3 col ad)"
end try
else if colWidth = 4 then
try
do script "PDF to GIF (4 col ad)"
end try
else if colWidth = 5 then
try
do script "PDF to GIF (5 col ad)"
end try
else if colWidth > 5 then
try
do script "PDF to GIF (6 col ad)"
end try
end if
end if
end tell
update folder gifFolder
if (count of (items in folder gifFolder whose ,
((name = (tempFile & ".gif" as string)) and ,
(file type = "GIFf")))) 1 then
-- GIFing has not yet completed
set checkCount to 0
repeat until (((count of ,
(items in folder gifFolder whose ,
((name = (adNumber & ".gif")) and ,
(file type = "GIFf")))) = 1) or ,
((keys pressed) = {"Option"}) or ,
(checkCount > checkLimit) or (gifDone = true))
try
set name of file (gifFolder & (tempFile & ,
".gif" as string)) to (adNumber & ".gif")
update folder gifFolder
set gifDone to true
on error
delay 5
set checkCount to checkCount + 1
if ((checkCount = 5) or (checkCount = 10) ,
or (checkCount = 15) or ,
(checkCount = 20)) then
tell application "FileMaker Pro"
activate
try
display dialog ,
"Waiting for the PDFed ad \"" & ,
adNumber & ".pdf" & ,
"\" to be GIFed ... " & ,
return & return & "Check " & ,
(checkCount as string) & ,
" of " & checkLimit & ,
" complete (on intervals of " & ,
"5 seconds). Hold down \"Option\" key " & ,
"to skip GIF checks." ,
with icon note giving up after 5
on error
return
end try
end tell
end if
end try
end repeat
end if
if (exists (file (gifFolder & (tempFile & ,
".gif" as string)))) = true then
set name of file (gifFolder & (tempFile & ,
".gif" as string)) to (adNumber & ".gif")
update folder gifFolder
set gifDone to true
tell application "FileMaker Pro"
set cell "GIF Status" of record i ,
of document 1 to "Ready"
end tell
end if
(*
if (exists (file (pdfOutFolder & (tempFile & ,
".pdf" as string)))) = true then
set name of file (pdfOutFolder & (tempFile & ,
".pdf" as string)) to (adNumber & ".pdf")
set gifDone to true
end if
*)
else -- this ad has no colwidth
end if
end tell
end tell
tell application "FileMaker Pro"
activate
if gifDone = true then
try
display dialog "GIFing of PDFed ad \"" & adNumber & ,
".pdf" & "\" was successful!" ,
with icon note giving up after 5
on error
return
end try
else
try
display dialog "GIFing of PDFed ad \"" & ,
adNumber & ".pdf" & ,
"\" was unsuccessful!" ,
with icon caution giving up after 5
on error
return
end try
end if
end tell
end if
end if
end tell
<snip...>