AppleScript, Photoshop & the Original Folder (again)

I have a simple Photoshop droplet on my desktop that, whenever I drag an image or a folder onto it, calls the relative action in PS (just converting in CMYK, 300 dpi, and save as Eps). The point is exactly this: I can’t tell PS thru Automation → Batch to save in the original folder.
I’ve downloaded the Photoshop Scripting plug in, I think that should help, but it means writing code, which I can’t… Well just a bit, it is.
Can I write a script that in batch from the Finder calls the correct PS Action and then sends the files again to the Finder for the saving procedure?
thx,
Guido

I’m not expert on PS, in fact I essentially don’t know how to use it at all! However, I did create an automation droplet in it once to convert some files to PICT, and it was able to save the output in the folder of my choice with no problem. Can you be any more specific?

The droplets you create in PS give you two options, regarding the “save to” procedure:

  1. save to the destination specified in the action itself
  2. override the destination in the action and save in the folder you specify in the Batch window, when you save the droplet. Thus, the droplet has a destination path already set, ALWAYS the same.

What I would like is to drop a folder on the droplet and let it process and save in the same folder in a different format; I don’t want to get thru dialog windows to manually set the path for the files…

Alright, this will do it:

property srcFolder : (choose folder)

on open eachFile
	tell application "Adobe® Photoshop® 6.0.1"
		repeat with thisFile in eachFile
			open thisFile
			do script "saveaspict"
		end repeat
	end tell
	tell application "Finder"
		move (every file of folder (my srcFolder)) to (get container of (path to me))
	end tell
end open

Save this script as a droplet. Replace ‘saveaspict’ with your own action. Make sure the action closes the document when it’s done with the file. Have the action save the file in temporary location like “/tmp/”. When you compile the droplet above, the ‘choose folder’ dialog will ask for the temp location where your action saves files.

I edited the action to save in .eps to the “Temp_img” folder, in my Documents directory.
Then, in the script, I changed the name of the PS action to execute, according to the actual action name.
But it gives me an “Invalid file path” error. What do you think is going wrong? I don’t have debug tools to understand.
Thx

Hmm, that’s an odd error. It doesn’t sound like it comes from Finder. Are you sure you didn’t delete the ‘Temp_img’ folder after you created the action? Is that the exact error you’re getting, pretty much? It could be that the path you wrote in is wrong. It has to be an alias, like:

property srcFolder: alias “Mac HD:folder:folder:”

Does yours look like that? You can easily set it up properly by highlighting the words ‘choose folder’ in this line:

property srcFolder: choose folder

and then pressing shift-command-8. If you have Script Editor 2, this will activate a service to run the selected text and replace the selection with the result.

It has to be an alias to work or to give the error? I didn’t write the path, BTW.
I set the destination in PS just choosing the right folder at the Save As stage of the action, and I choose the same destination at the Compile stage of the AppleScript.
It’s in my Documents directory, so it is : “Mac OsX:Users:me:Documents:”
Hey, pal, thx again

Did you make sure to change the version of PS in the tell to the one you actually have? Note that the version I tested on is running in Classic. Try running this test script from a script editor and report what part of the script is highlighted when it errors (make sure to make change the action again, pick the temp file location when you compile, and change the application name to the version you’re using):

property srcFolder : (choose folder)

on run
	open {choose file with prompt "Pick a PS file:"}
end run

on open eachFile
	tell application "Adobe® Photoshop® 6.0.1"
		repeat with thisFile in eachFile
			open thisFile
			do script "saveaspict"
		end repeat
	end tell
	tell application "Finder"
		move (every file of folder (my srcFolder)) to (get container of (path to me))
	end tell
end open

If the test script runs without errors, the transformed files would show up in your script editor’s folder (“Mac HD:Applications:AppleScript:” on my machine, for example). But I hope the test does raise an error, in this case.

it works. In the sense that it saves the file in “Mac HD:Applications:AppleScript:”.
I haven’t the fanciest idea what this means, but I’m reporting, as ordered…

I’m fresh out of ideas, then. If that script works, then the first one should work too. Did you save the first script as an application from a script editor so that it creates a droplet? If yes, where is the droplet located when you try to drop image files on it? Maybe there’s a problem with the folder’s permissions? I don’t know.

In case something went wrong the first time you tried it, try saving as an application the script you got to work from the script editor, then drop a file on it–it should work just as well as it did from the script editor. If not, I’m baffled.

Micheal, it seems that you are testing under Photoshop 6 in Classic. I didn’t think that version was even scriptable (without thrid party plugins)… Hmm.

Anyway, you need to use:
do action “youractionname” from “youractionset”
to invoke an action, at least in Photoshop CS.

You may also want to consider removing the “save” command from your action. Your applescript can handle the saving just as well. That way you won’t need to use the finder to move the file(s). As far as I can tell, in the current script fragment the action is still handling the save so the save destination is “hardwired” into the action, it will not always be the same as the folder that is dropped. The AS library for Photoshop CS is pretty good, FWIW.

Here’s a slimmed down version of a script I use (using an action that does NOT have a “save” step in it). If anyone has any suggestions to simplfy the open handler, they would be appreciated (I’m a fairly new coder…)


on open folderDropped
	set folderPath to folderDropped as string
	set folderDropped to list folder folderDropped without invisibles
	repeat with thisFile in folderDropped
		set thisFile to (folderPath & thisFile) as alias
		tell application "Adobe Photoshop CS"
			activate
			open thisFile showing dialogs never
			set myOptions to {class:EPS save options, embed color profile:true, preview type:eight bit Mac OS}
			if mode of current document is RGB then
				--  Run Photoshop action to USM, convert to CMYK
				do action "your action" from "set your action is saved in"
				save current document as Photoshop EPS ¬
					with options myOptions in file (thisFile as string) appending lowercase extension
				close current document saving no
				
			else
				display dialog "This file is not RGB!" & return & "It will not be converted to CMYK."
				close current document saving no
				
			end if
			
		end tell
	end repeat
end open

Of course you should add error handlers, especially in case a file with the same name your are saving exists already in that directory. I also use some checks for file types (you could make it not process EPS files for instance).
By the way, this could be done even faster if you are only converting modes(ie no USM or other processing). Take a look at the ColorSyncScripting library and the ColorSync scripts included with OS X.

Hope this works for you,
Tom

Right, I mentioned that I’m testing with a version of Photoshop in Classic. It does indeed appear to be meagerly scriptable: It has a ‘do script’ command. :slight_smile: If gratico has Photoshop CS, then that’s going to be a better choice, but I didn’t (don’t) have it to offer advice on. However, this problem still should be solvable with the bare bones scripting support built into Photoshop, I think, and revisiting this thread makes me realize my mistake. I didn’t notice that gratico also wanted to drop folders of files on the droplet. I bet that’s what you tested with, huh gratico, a folder? Here’s a fixed version that one can drop files and folders on:

property srcFolder : (choose folder) <-- pick temp file

on open eachItem
	repeat with thisItem in eachItem
		tell application "Finder"
			if kind of item thisItem is "folder" then
				set countEachFile to (count thisItem each file)
				if countEachFile is greater than 1 then
					set eachFile to get every file of thisItem as alias list
				else if countEachFile is equal to 1 then
					set eachFile to get every file of thisItem as alias as list
				else -- countEachFile is 0
					exit repeat
				end if
			else
				set eachFile to thisItem as list
			end if
		end tell
		repeat with thisFile in eachFile
			tell application "Adobe® Photoshop® 6.0.1" <-- make sure points to correct version of PS
				open thisFile
				do script "saveaspict" <-- change action name
			end tell
		end repeat
	end repeat
	tell application "Finder"
		move (every file of folder (my srcFolder)) to (get container of (path to me))
	end tell
end open

I have to say…
tfbrice wins the first prize!
Sorry, Mike. I tried again the last script and it returns me always the same error “Invalid path”, God knows why…
I’m running tfbrice script on PS 7 in Mac Os X with a little tweaking (no rgb check, it’s included in the action) and it works perfectly.
I will try this later, but does it work with more than one folder?
Does it remember the correct destination for each file in each folder?
Thx again, great save of time,
:smiley:

Heh. No problem. I’m glad it worked out. It’d be nice to know what’s going on with my script, but since I can’t reproduce it, life’s tough.

But does it work on your system?

Totally, of course it does! If I ever post an untested script I mention the fact. It even works on your system, at least from the script editor. Why not from a droplet?!

Maybe running it from Panther on PS 7 changes the scenario…

grafico,
I’m glad it worked. As far as multiple folders, I don’t think so. I think it would need some tests for folders, etc (an if statement). A more complete implementation would process single files, folders, etc… As I said, I’m new to this, so working things like that out takes me quite some time. I’m always reading through the main site as well as other AS sites to try to learn. Its pretty fun, but I never seem to have enough time!

Anyway, glad it worked.

Micheal, I tried to use your logic of “as alias list” but it does not seem to work properly in Panther. Any thoughts?

I spoke too soon. My problem with “alias list” was that I was trying to use it without putting it in a “Tell “Finder”” block. Michaels logic for processing items dropped on the script works flawlessly! Here is it tacked onto the front of the PS script:


on open stuffDropped
	repeat with thisItem in stuffDropped
		tell application "Finder"
			if kind of item thisItem is "folder" then
				set countEachFile to (count thisItem each file)
				if countEachFile is greater than 1 then
					set stuffDropped to get every file of thisItem as alias list
				else if countEachFile is equal to 1 then
					set stuffDropped to get every file of thisItem as alias as list
				else -- countEachFile is 0 
					exit repeat
				end if
			else
				set stuffDropped to thisItem as list
			end if
		end tell
		
		repeat with thisFile in stuffDropped
			tell application "Adobe Photoshop CS"
				activate
				open thisFile showing dialogs never
				set myOptions to {class:EPS save options, embed color profile:true, preview type:eight bit Mac OS}
					--  Run Photoshop action to USM, convert to CMYK 
					do action "your action" from "set your action is saved in"
					save current document as Photoshop EPS ¬
						with options myOptions in file (thisFile as string) appending lowercase extension
					close current document saving no
										
				end if
			end tell
		end repeat
	end repeat
end open

I tested this and it does process multiple folders, saving in the converted files in the original folder. It will also handle individual files! Thanks Michael!

If your action is just changing out the color mode, you could just use this line of code instead of running your action…

	change mode of current document to CMYK 

I mean if you’ve gone this far with the script, why require an action to run if it’s not necessary.