Working on BBS

Let me start by saying that I’m a total newbie to scripting. With that said, I need help with this folder action script that I am working on which contains script that I copied from another one and then modified to suit my purposes. This folder action script imports any images added to a folder into my iPhoto library, which works fine. Then I added the tell statement at the end to delete the files after the import. What ends up happening is that the folder containing the photos gets deleted, not just the photos inside. I obviously don’t want that. Here’s the script:

on adding folder items to this_folder after receiving these_items
	set this_item to this_folder
	
	-- convert the file reference to UNIX style
	set this_path to the POSIX path of this_item
	-- bring the target application to the front
	tell application "iPhoto"
		activate
		try
			tell application "System Events"
				tell process "iPhoto"
					-- summon the import dialog
					click menu item "Import…" of menu "File" of menu bar 1
					-- enter the path to the image in the dialog input
					set the value of text field 1 of group 1 of group 2 of window "Import Photos" to this_path
					delay 1
					-- click to start the import
					click button "Import" of window "Import Photos"
					if the last character of (this_item as string) is ":" then
						-- it's a folder so click the import button again
						click button "Import" of window "Import Photos"
					end if
				end tell
			end tell
		on error error_message
			display dialog error_message buttons {"OK"} default button 1
		end try
	end tell
	tell application "Finder"
		activate
		select contents of this_folder
		delete selection
		close container window of this_folder
	end tell
end adding folder items to

BTW, in case you’re wondering why I would want to automatically delete photos that I just added to the folder, this script is intended to work in conjunction with a “do Photoshop action” script in iPhoto. Basically, that script takes pictures I have selected in iPhoto, performs the desired Photoshop actions on them, and then saves them to a temporary folder, to which the folder action script above is attached. The author of the Photoshop action scripts for iPhoto did not know how to automatically import the files back into iPhoto in the script, which is why I am going this route. So if anyone here knows how to do that instead, that would be a much cleaner and more efficient way to perform this task.
Thanks in advance for any help.

Does it work if you replace this:

tell application "Finder"
		activate
		select contents of this_folder
		delete selection
		close container window of this_folder
	end tell

With this?

tell application "Finder" to delete items of this_folder

– Rob

Rob,
Yes, it achieves the desired results as far as the file deletion goes, but now it happens so quickly that it’s deleting the files before the import process in iPhoto starts. I suspect that this is because once the last step of the GUI scripting part is done (clicking “Import”), the deletion tell statement starts before iPhoto has a chance to get the import process started. I inserted a “delay” but this is a cheap band-aid. Do you or anyone else know if there’s a way the script can hold off on the deletion until the import is complete? The reason for this is, sometimes, I run a batch of anywhere from 1-20 pictures through the PS action script at a time. Since the amounts vary so much, the delay statement must be long enough for the import to run, but short enough so that I can run consecutive batches without reimporting pics that were processed in the previous run. (I have multiple PS action scripts). I guess for now, I’ll use the delay statement and keep my batches small.
Thanks.

This is the type of issue that one runs into when using UI scripting - the script doesn’t get the feedback that it needs from the application.

I don’t use iPhoto so I can only guess on ways to overcome the timing issue. Can the script determine the number of photos that have been imported and compare it to the number of items in this_folder? If so, this might allow you to cut the delay down to a more reasonable amount of time.

– Rob

I don’t do a lot of iPhoto work, but I can’t help but think you’re going about this whole process in the wrong way.

You’re trying to use UI scripting to do things the way you’d do them manually (select this menu item, click this button, select these files, etc., etc., etc.)

Looking at iPhoto’s dictionary it looks like you should be able to do something like (untested):


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

This code doesn’t require activating iPhoto (it isn’t necessary to activate the app to import a picture), and it deletes each file as it goes - however since the delete command follows the add command it won’t be run until the add command finishes, so you shouldn’t have any race conditions walking over the files.

Note there’s no error checking in the script to make sure the add worked, or that the file format is something that iPhoto can read - you should considering adding that for a production script.

BTW, you never have to select a file in the Finder to delete it - in fact you rarely have to select anything in the Finder with AppleScript. Deleting the selection is an inherently dangerous thing to do.

Camelot,
Thanks, I’ll give that a try. I do have one question though: In your snippet, aFile is never defined. Aren’t the green words in Applescript markup variables or arguments that must be set or defined? Just askin’ cuz I don’t` know. Is aFile a generic identifier?
Thanks.

In my script, aFile is defined in the line:

repeat with aFile in theAddedItems 

This is a very powerful feature of AppleScript. If you want to iterate through all the items in a list, this format will assign the variable name (in this case ‘aFile’) to the first item in the list, run the repeat code, then change the variable to point to the next item, and so on until all the items are processed. You don’t need to keep a counter to where you are in the list (unless you want to) since AppleScript takes care of that for you.

So in my code, the repeat loop is run once for each item in the list and each time through ‘aFile’ points to the different items as it progresses.

Camelot,
No dice. Not only did the pictures not get imported into iPhoto, but the files in the folder did not get deleted.
I looked at the iPhoto dictionary, and the definition for the “add” item seemed pretty ambiguous to me. I’m not positive, but I’m pretty sure that to get photos into an iPhoto library from a folder outside of that library, they have to me imported into it. As far as I could tell, the add command doesn’t do that.
Any more ideas?
Thanks again!