Action on folder which also effects folders in that folder?

I have a script that makes it so when i drag an image into a folder, it automatically loads iphoto and imports it.

Is there anyway to make it so the same thing happens for folders that may be nested within that main folder?
I have another app that puts photos in that folder, but dynamically creates a new folder with the date every time it puts photos in (so obviously It wouldn’t be efficient to attach the script to each new folder that is created every day)

Any ideas?

here’s the script


on adding folder items to this_folder after receiving added_items
	tell application "iPhoto"
		import from added_items
		delay 5 -- wait for import to finish
		tell (the first album whose name is "Last Import") to select photos
		assign keyword string "Eye-fi"
		select (the first album whose name is "Eye-fi")
	end tell
end adding folder items to

Hi Susan,
maybe this could help! If I understand correctly you have a main folder that has a folder script attached but also an app that creates folders inside this and then puts photos in these sub directories? As you mentioned it’s not practical to attach a folder action to every new folder so maybe an app that looks inside every folder of the main directory for photos and imports these items and deletes them?

global main_folder

on run
	set main_folder to choose folder
end run

on idle
	tell application "Finder"
		set folder_list to every folder in entire contents of main_folder
		repeat with this_folder in folder_list
			set file_list to every file of this_folder
			repeat with this_file in file_list
				set this_file to this_file as alias
				tell application "iPhoto"
					import from this_file
					delay 5 -- wait for import to finish
					tell (the first album whose name is "Last Import") to select photos
					assign keyword string "Eye-fi"
					select (the first album whose name is "Eye-fi")
				end tell
				delete this_file
			end repeat
		end repeat
	end tell
end idle

this would have to be saved as a stay open app and would check for photos every 30 seconds or which ever amount of time you choose.
Nik

You’re exactly right in describing what im trying to do.

Im a little confused about your solution though. AppleScript is new to me.

How exactly would i do this : "this would have to be saved as a stay open app ?

Hi Susan,
When you Save As from Script Editor select “Application” from the file format and check the “stay open” box in the save window. When you run the script the first time it runs you’ll be prompted to choose the main folder and then, after that, every 30 seconds the script will run the code between the on idle handler.
Nik

ok gotcha gotcha. getting closer.

got it going as an app.
however, its not actually importing any of the files into iphoto.

i added some dialogs to the script, so that I know its loaded properly…and they’re coming up when they should… but just not loading iphoto.

global main_folder

on run
	set main_folder to choose folder
end run

on idle
	display dialog "on idle"
	tell application "Finder"
		display dialog "tell finder"
		set folder_list to every folder in entire contents of main_folder
		repeat with this_folder in folder_list
			set file_list to every file of this_folder
			repeat with this_file in file_list
				set this_file to this_file as alias
				tell application "iPhoto"
					import from this_file
					delay 5 -- wait for import to finish
					tell (the first album whose name is "Last roll") to select photos
					assign keyword string "Eye-fi"
					select (the first album whose name is "Eye-fi")
				end tell
				delete this_file
			end repeat
		end repeat
	end tell
end idle

Hi Susan,
It works fin for me but try giving this code a try

global main_folder

on run
	set main_folder to choose folder
end run

on idle
	tell application "Finder"
		set folder_list to every folder in entire contents of main_folder
		repeat with this_folder in folder_list
			tell application "Finder" to set folder_name to the name of this_folder
			display dialog folder_name
			set file_list to every file of this_folder
			repeat with this_file in file_list
				tell application "Finder" to set file_name to the name of this_file
				display dialog file_name
				set this_file to this_file as alias
				(*tell application "iPhoto"
					import from this_file
					delay 5 -- wait for import to finish
					tell (the first album whose name is "Last Import") to select photos
					assign keyword string "Eye-fi"
					select (the first album whose name is "Eye-fi")
				end tell
				delete this_file*)
			end repeat
		end repeat
	end tell
end idle

This should display a dialog of the current folders name and then the name of any files that are in that folder, I’ve commented out the IPhoto part for now, I just want to check that the script at your end can see the folders and there contents.
Nik

i made some screencasts just to confirm im doing everything correctly.

I changed the script to what you just suggested — it does list the image … here’s a screencast
http://www.screencast.com/users/susanBuck/folders/Jing/media/8e9dc9ab-bce2-487e-bd06-180d52a0db52
(theres some deadtime at the end where im waiting for the dialogue to popup)

also, here’s a screencast using the original script you gave (with the iphoto functions not commented out)
http://www.screencast.com/users/susanBuck/folders/Jing/media/9ec474c8-91c5-4c93-825f-5cbc37c12c8e

Hi Susan,
It looks as though the scripts finding the files ok so the only problem is the IPhoto part. I must admit it’s the first time I’ve scripted IPhoto but the file is importing at my end. Out of interest if you run the original script does the file get deleted from the nested folder?
Give this a try

set this_file to choose file
tell application "iPhoto"
	import from this_file
	delay 5 -- wait for import to finish
	tell (the first album whose name is "Last Import") to select photos
	assign keyword string "Eye-fi"
	select (the first album whose name is "Eye-fi")
end tell

Does it throw up any errors? Can you see the image in the “Last Roll”?
P.S. I’m using version 6.06 of IPhoto how about you?

First off - thanks for taking the time to help me try to figure this out… : )

I am using an older version of iPhoto - and one difference ive discovered to get it working on different versions (which this needs to do) is that in older versions it should be “Last Roll” instead of “Last Import” They changed the name in the new version…but ive been making those changes when trying to run it on my computer, so that shouldn’t be the bug

And no, it isn’t deleting the file in the folder.
Also, no errors, and the image isn’t showing up “Last roll”

Very curious.

Hi Susan,
lets try this:

global main_folder

on run
	set main_folder to choose folder
end run

on idle
	tell application "Finder"
		set folder_list to every folder in entire contents of main_folder
		repeat with this_folder in folder_list
			set file_list to every file of this_folder
			repeat with this_file in file_list
				set this_file to this_file as alias
				(*tell application "iPhoto"
					import from this_file
					delay 5 -- wait for import to finish
					tell (the first album whose name is "Last Import") to select photos
					assign keyword string "Eye-fi"
					select (the first album whose name is "Eye-fi")
				end tell*)
				delete this_file
			end repeat
		end repeat
	end tell
end idle

Just to test that it’s running through the repeat routines and deletes every file that it finds!
Nik

Its not deleting the files. I put in dialogs after each step and found it is getting hung up right after this repeat “repeat with this_file in file_list” …

Its working for you - and its working for a friend who has an intel mac book.
I have a powerbook g4.

Think it has something to do with that?

Here’s the code showing where the dialog that never displays is.



global main_folder

on run
	display dialog "Select your folder in the next menu" buttons {"Cancel", "Ok"} default button 2
	set main_folder to choose folder
end run

on idle
	
	tell application "Finder"
		set folder_list to every folder in entire contents of main_folder
		
		repeat with this_folder in folder_list
			
			set file_list to every file of this_folder
			repeat with this_file in file_list
				
				-- never gets here to this dialog
				display dialog "in this repeat loop"
				
				set this_file to this_file as alias
				tell application "iPhoto"
					if file type of this_file is "JPG" or file type of this_file is "JPEG" then
						import from this_file
						delay 5 -- wait for import to finish
						-- tell (the first album whose name is "Last Import") to select photos
						-- assign keyword string "Eye-fi"
						-- select (the first album whose name is "Eye-fi")
					end if
				end tell
				delete this_file
			end repeat
		end repeat
	end tell
end idle


If it would help, you could script the attachment of the folder action to the new folders that are created.

That is a good point, Bruce.
However- i’ve grown to like the method blend3 suggested of making it into an app. I think that its more intuitive for users to operate instead of trying to explain to them how to download the script, put it in the right place, attach it to the folder, etc.

Thanks though : )