Watched Folder to Convert EPS to JPG using GraphicConverter

The Folder Action script below is supposed to convert each EPS found in the watched folder to a High and Low res version (in separate folders) but doesn’t do anything. If I remove the wrapping ‘on adding’ and replace with

tell application "Finder" to set theFiles to every file of folder "EPS" of desktop

then it works fine.

Anyone know where I’m going wrong? I would use the Batch Convert option in GraphicConverter if I could understand how to configure it for this use, but I’m more used to 'scripting tasks like this using Photoshop (but this isn’t an option in this case unfortunately).

Any help will be very much appreciated!

GraphicConverter v6.1.2 running on Mac OS X 10.5.4 (destined for Leopard Server once complete).


on adding folder items to theFolder after receiving theFiles
    try
        repeat with oneItem in theFiles
            set JPG_Hi to "Macintosh HD:Users:studio9:Desktop:Inv_JPG:"
            set JPG_Web to "Macintosh HD:Users:studio9:Desktop:Inv_Web_JPG:"
            set thePath to {JPG_Hi, JPG_Web}
            set JPGquality to {100, 50}
            set JPGDPI to {150, 72}
            set theFile to name of oneItem as string
            set aliasFile to oneItem as string
            set the theName to text 1 thru ((length of theFile) - 3) of the theFile & "jpg"
            repeat with i from 1 to 2
                tell application "GraphicConverter"
                    activate
                    open {alias aliasFile}
                    tell window 1
                        set thisPath to item i of thePath
                        set thisQuality to item i of JPGquality
                        set thisDPI to item i of JPGDPI
                        set JPEG quality to thisQuality
                        set JPEG progressive to true
                        --change resolution to {thisDPI, thisDPI}
                        save in (thisPath & theName) as JPEG with wwwready
                        close
                    end tell
                end tell
            end repeat
        end repeat
        tell application "Finder" to delete theFiles
    end try
end adding folder items to

Browser: Firefox 3.0.1
Operating System: Mac OS X (10.5)

Hi,

to debug a script it’s recommended to comment out all try blocks to get any error messages.
Probably this lines causes the error


set theFile to name of oneItem as string

outside a Finder or System Events tell block name of alias generates an error
you can use alternatively

set theFile to name of (info for oneItem)

Thank you Stefan, this has done the trick! Don’t know how I missed that.

OK, Stefan you may be able to help me again with this. We have permission problems on our server (they don’t stick as they ought) so I’ve amended the 'script to correct the permissions of the files before GC gets to work on them (else it fails). However, in doing this your earlier fix now fails. Perhaps it is the order in which I now have the command lines.

You’ll see the commented-out Finder tell at the start - this was in place to check the failure, which is the Finder saying that it can’t find the file… even though the path in Event Log reads true.


on adding folder items to theFolder after receiving theFiles
	
	--tell application "Finder" to set theFiles to every file of folder "Macintosh HD:Users:studio9:Desktop:EPS"
	
	repeat with oneItem in theFiles
		
		set JPG_Hi to "Macintosh HD:Users:studio9:Desktop:Inv_JPG:"
		set JPG_Web to "Macintosh HD:Users:studio9:Desktop:Inv_Web_JPG:"
		set thePath to {JPG_Hi, JPG_Web}
		set JPGquality to {100, 50}
		set JPGDPI to {150, 72}
		set aliasFile to oneItem as string
		set theFile to name of (info for oneItem)
		set the theName to text 1 thru ((length of theFile) - 3) of the theFile & "jpg"
		
		do shell script "sudo chown admin:studio \"/Volumes/Macintosh HD/Users/studio9/Desktop/EPS/" & theFile & "\"" password "xxx" with administrator privileges
		do shell script "sudo chmod 777 \"/Volumes/Macintosh HD/Users/studio9/Desktop/EPS/" & theFile & "\"" password "xxx" with administrator privileges
		do shell script "sudo -k"
		
		tell application "Finder"
			repeat with y from 1 to 2
				set thisPath to item i of thePath
				if exists (thePath & theName) then delete (thePath & theName)
			end repeat
		end tell
		
		repeat with i from 1 to 2
			tell application "GraphicConverter"
				activate
				open {alias aliasFile}
				tell window 1
					set thisPath to item i of thePath
					set thisQuality to item i of JPGquality
					set thisDPI to item i of JPGDPI
					set JPEG quality to thisQuality
					set JPEG progressive to true
					--change resolution to {thisDPI, thisDPI}
					save in (thisPath & theName) as JPEG with wwwready
					close
				end tell
			end tell
		end repeat
	end repeat
	tell application "Finder" to delete theFiles
end adding folder items to

sudo and administrator privileges is the same thing,
so according to Apple’s Technical Note 2065 remove sudo

try this


do shell script "/usr/sbin/chown admin:studio " & quoted form of POSIX path of oneItem password "xxx" with administrator privileges
do shell script "/bin/chmod 777 " & quoted form of POSIX path of oneItem password "xxx" with administrator privileges

I hope, that changing attributes of the files won’t cause an infinite loop by triggering the folder action again

Hmm… well that didn’t break or fix anything (although I’ll take your advice, of course, and keep ‘sudo’ out of the do shell script). Turns out that I had a differing repeat counter. However, I now have to work out why it still didn’t bother deleting the found JPG images before converting the EPS files again (and overwriting the existing JPGs, which is fine but I’d rather they weren’t there at all if the script should fail).

Hi Steve,

If your script is targeted to run on a server, you could also consider to rely on built-in tools (without using GraphicConverter at all, even if it is an excellent piece of software):


on adding folder items to watchfolder after receiving newfiles
	repeat with newfile in newfiles
		try
			set folderpathjpghi to "/Users/martin/studio9/Inv_JPG/"
			set folderpathjpgweb to "/Users/martin/studio9/Inv_Web_JPG/"
			set fileinfo to (info for newfile)
			set filename to name of fileinfo
			set filename to text 1 thru ((length of filename) - 3) of filename & "jpg"
			set epspath to quoted form of (POSIX path of (newfile as Unicode text))
			set pdfpath to quoted form of (POSIX path of (my gettmpfilepath()))
			set jpghipath to quoted form of (folderpathjpghi & filename)
			set jpgwebpath to quoted form of (folderpathjpgweb & filename)
			set command to "pstopdf " & epspath & " -o " & pdfpath & "; sips -s format jpeg -s formatOptions 100 -s dpiHeight 150 -s dpiWidth 150 " & pdfpath & " --out " & jpghipath & "; sips -s format jpeg -s formatOptions 50 -s dpiHeight 72 -s dpiWidth 72 " & pdfpath & " --out " & jpgwebpath & "; rm " & pdfpath
			do shell script command
		end try
	end repeat
end adding folder items to

on gettmpfilepath()
	set tmpfolderpath to ((path to temporary items folder from user domain) as Unicode text)
	repeat
		set randnum to random number from 10000 to 99999
		set tmpfilepath to tmpfolderpath & randnum & ".pdf"
		try
			set tmpfilealias to tmpfilepath as alias
		on error
			exit repeat
		end try
	end repeat
	return tmpfilepath
end gettmpfilepath

Just an idea.

Hi Martin,

could not resist being nitpick with useless coercions :wink:

an alias has also a POSIX path property

set epspath to quoted form of POSIX path of newfile

as is a parameter of path to, consider the parentheses


set tmpfolderpath to (path to temporary items folder from user domain as Unicode text)