OK Gentlemen, here we go…
tell application "Illustrator CS"
activate
--Inits
set docExists to false -- Assume document does not exist
set {defaultButton, initialSaveButtons, saveDialog, dummyname} to {"Save", {"Save", "Cancel"}, "Press Save to continue", ""} -- Inital dialog setup
-------
try
set fpath to file path of document 1 -- Get the file path of the document
tell application "Finder" to set {ftype, saveLocation} to {name extension, container} of fpath -- get the extension and container of the file for the save location
set fname to my processFilename(fpath, ftype)
on error -- It's an unsaved NEW document, not an opened file (jpg/pdf/etc.) Could occur if someone does a copy and paste to a new document or creates a new document from scratch.
set fname to (text returned of (display dialog "Unsaved document. Please enter a filename." & return & "The .EPS extension will be added automatically." default answer "")) & ".eps" -- Provide a name for the new document
set saveLocation to choose folder with prompt "Choose a destination folder:" -- Provide a destination folder
set ftype to "" -- Null value since the file does not exist
end try
set dummyname to fname
repeat until docExists is true -- This will loop until a unique filename is provided or the user cancels.
set saveChoice to (display dialog saveDialog buttons initialSaveButtons default answer dummyname default button defaultButton) -- Give the operator the option of Renaming the file on save. Since it's an extra dialog to deal with, the "Save" option is default so they can just hit "Enter".
if button returned of saveChoice = "Rename" then
set fname to ((text returned of saveChoice) & ".eps") as string
else if button returned of saveChoice = "Save" and (text returned of saveChoice) ≠dummyname then
set fname to ((text returned of saveChoice) & ".eps") as string
end if
set savePath to ((saveLocation as string) & fname) as string
if button returned of saveChoice ≠"Overwrite" then -- If "Overwrite" is not chosen then check for the doc's existence.
set {docExists, initialSaveButtons, defaultButton, saveDialog, dummyname} to my checkForFile(savePath, initialSaveButtons, defaultButton, saveDialog, dummyname)
else
set docExists to true -- If Overwrite is chosen then exit the loop and save over the existing file.
end if
end repeat
-- This is a hack to get around a bug in Illustrator. saving an .ai to a .ai, .eps to .eps doesn't work. (error -43 File not found)
if ftype = "eps" then
set savePath to my createFile(savePath)
end if
-- And finally! the save!
save current document in file savePath as eps with options {class:EPS save options, preview:color Macintosh, embed all fonts:true, embed linked files:false, include document thumbnails:true, compatible gradient printing:true, CMYK PostScript:true, overprint:discard, PostScript:level 3}
end tell
on processFilename(fpath, ftype)
tell application "Finder"
if name of fpath ends with ftype then
set fname to (characters 1 through (-(((count of ftype) + 2))) of (name of fpath as string) & ".eps") as string -- Need an extra 1 because AS doesn't count the "."
else
set fname to ((name of fpath) & ".eps") as string -- If it doesn't have the extension, then add it.
end if
end tell
end processFilename
on checkForFile(savePath, initialSaveButtons, defaultButton, saveDialog, dummyname)
tell application "Finder"
if exists (file savePath) then
return {false, {"Overwrite", "Rename", "Cancel"}, "Rename", "This File already exists!" & return & "Overwrite or Provide a filename", ""}
else
return {true, {"Save", "Cancel"}, "Save", "Press Save to continue", dummyname}
end if
end tell
end checkForFile
on createFile(savePath)
tell application "Finder"
open for access alias savePath
close access alias savePath
end tell
return (savePath as string)
end createFile
Jeff: I did a lot of validation on this. I didn’t use every trick in the book, but you’ve got some dynamic dialogs, repeat until flags, etc. I tried to comment pretty liberally so you could (hopefully!) follow what’s going on and what I was thiinking.
It will work on an unsaved document, a file opened in AI (like when you drop a jpg on the AI icon in the dock) and native .ai and .eps files. It checks for the existence of the files and prompts you to rename them or overwrite. It makes coffee and shines your shoes… uh… Anyways, there is one little GUI thing you might find odd… if you have an unsaved document you are presented with a dialog to name the file, then a dialog to choose the destination, then you are prompted with another dialog (which is the main dialog seen with saved files). In this dialog you can rename the initial name you gave it or just hit “Enter” to save. (You’ll notice it shows you the proper name with the .eps extension added.) You could also change the choose folder option to point to the “work_in_progress” folder if you wanted. (And it WILL work on a JPG file!)
Have fun with it and et me know if you have any problems. (This kind of stuff is part of my job so I’m used to hearing “Yeah, but what if it did THIS instead of THAT!”)
Cheers,
Jim Neumann
BLUEFROG