Folder actions when folder (rather than file) is added

Hi all,

I have a folder action script that I’m developing to submit avi files for video conversion when added to a folder (ultimate with the aim to publish them to AppleTV). This is all working basically as intended, but I realised this morning that the target avi file will sometimes be within a folder (ie. a folder will be added containing the file within). On to the question…

Is there a way via Applescript to tell if the object added is a folder, then to tell the script to look for .avi files within the subfolder rather than the parent?

Thanks,

Tom

Thinking further on this, I think there are two possible approaches:

  1. Is there a simple way to just make a folder action recursive, so that rather than processing files added to the original folder all files added to it and any subfolder within will be processed?

  2. Failing the solution above, I’ve found another post where someone creates a list of all the folders within a folder. It sounds like I’d need to have something like:

(pseudo-code)
on adding folder items…
repeat with each item added
if folder of item is true then process_folder
else process_file
end if
end repeat

on process_folder

(not sure how to code this bit)

on process_file
… (existing script goes here)

end adding folder items to

Would this approach work? And if so any pointers on how to code the process_folder section?

Thanks,

Tom

You don’t make it clear what you want to do with the dropped folder, but dropping anything triggers the “hot” folder and returns a list of the items (files or folders) dropped. You could process that list one at a time and if the item is a folder, tell the Finder to get it’s entire contents and call your handler in a repeat loop operating on the contained files.


-- given tFolder
tell application "Finder"
	try
		set tFiles to files of entire contents of tFolder as alias list
	on error -- fails on one contained file
		set tFiles to files of entire contents of tFolder as alias as list
	end try
end tell
-- etc.

Thanks Adam,

The ultimate objective is that any “avi” file dropped in the folder, or in any folder dropped in the folder, needs to be passed to VisualHub for conversion.

I have the ‘conversion’ part of the script working for files dropped in a single ‘root’ folder. I’m just trying to work out the best approach for scenario where a folder is added containing the files that need processing.

Is that any help?

Thanks again,

Tom

Hi there,
this folder action should allow you to drop an avi file onto the folder or subfolders that contain avi files and allow you to do whatever you want with the file.

on adding folder items to this_folder after receiving these_items
	set item_list to every item of these_items
	repeat with i from 1 to the number of items of these_items
		set this_item to item i of these_items as alias
		set am_i_a_folder to folder of (info for this_item)
		if am_i_a_folder = true then
			process_this_folder(this_item)
		else
			process_this_file(this_item)
		end if
		
	end repeat
end adding folder items to

on process_this_file(this_item)
	set file_extension to name extension of (info for this_item)
	if file_extension = "avi" then
		display dialog "I'm an avi file" --> replace this with whatever you want to do with the avi files
	end if
end process_this_file

on process_this_folder(this_item)
	tell application "Finder"
		try
			set folderlist to every folder in entire contents of this_item
			repeat with i from 1 to the number of items of folderlist
				set this_folder to item i of folderlist as alias
				set item_list to every file of this_folder
				repeat with i from 1 to the number of items of item_list
					set this_item to item i of item_list as alias
					set file_extension to name extension of (info for this_item)
					if file_extension = "avi" then
						display dialog "I'm an avi file" --> replace this with whatever you want to do with the avi files
					end if
				end repeat
			end repeat
		on error
			set item_list to every item of this_item
			
			repeat with i from 1 to the number of items of item_list
				set this_new_item to item i of item_list as alias
				set file_extension to name extension of (info for this_new_item)
				if file_extension = "avi" then
					display dialog "I'm an avi file" --> replace this with whatever you want to do with the avi files
				end if
			end repeat
		end try
	end tell
end process_this_folder

That’s excellent - more or less the process I described in the post above so at least my logic was sound. :smiley:

Sadly, after a few modifications it doesn’t work, and I’m not sure why. This is the completed version:

on adding folder items to this_folder after receiving these_items
	tell application "VisualHub" to set VisualHub to load script (scripts path of main bundle & "/automation.scpt" as POSIX file)
	--check to see if VisualHub is 'busy', if so wait 30 secs and retry
	tell VisualHub
		repeat while item 1 of CheckStatus() is not equal to 1
			delay 30
		end repeat
		ClearAll()
		LoadSettings("Mac Home Server:Users:tomwynne:Desktop:settings.vhub")
		SetSaveLocation("Media:Movies")
		display dialog "VisualHub setup complete"
	end tell
	set item_list to every item of these_items
	repeat with i from 1 to the number of items of these_items
		set this_item to item i of these_items as alias
		set am_i_a_folder to folder of (info for this_item)
		if am_i_a_folder = true then
			display dialog "Going to add the folder items"
			add_this_folder(this_item)
		else
			display dialog "Going to add the file"
			add_this_file(this_item)
		end if
	end repeat
	tell application "GrowlHelperApp"
		set the allNotificationsList to ¬
			{"VisualHub Status"}
		
		set the enabledNotificationsList to ¬
			{"VisualHub Status"}
		
		register as application ¬
			"VisualHub Automation" all notifications allNotificationsList ¬
			default notifications enabledNotificationsList ¬
			icon of application "VisualHub"
		
		notify with name ¬
			"VisualHub Status" title ¬
			"VisualHub Status" description ¬
			"File(s) have been added for processing." application name "VisualHub Automation"
	end tell
	delay 20 --delay to allow file movement to complete
	tell VisualHub to StartConversion()
	-- notify via Growl when conversion is complete
	tell application "GrowlHelperApp"
		
		notify with name ¬
			"VisualHub Status" title ¬
			"VisualHub Status" description ¬
			"Conversion complete." application name "VisualHub Automation"
	end tell
	tell VisualHub to QuitApp()
end adding folder items to

on add_this_file(this_item)
	tell application "VisualHub" to set VisualHub to load script (scripts path of main bundle & "/automation.scpt" as POSIX file)
	set file_extension to name extension of (info for this_item)
	if file_extension = "avi" then
		tell VisualHub to AddFiles(this_item)
	end if
end add_this_file

on add_this_folder(this_item)
	tell application "VisualHub" to set VisualHub to load script (scripts path of main bundle & "/automation.scpt" as POSIX file)
	tell application "Finder"
		try
			set folderlist to every folder in entire contents of this_item
			repeat with i from 1 to the number of items of folderlist
				set this_folder to item i of folderlist as alias
				set item_list to every file of this_folder
				repeat with i from 1 to the number of items of item_list
					set this_item to item i of item_list as alias
					set file_extension to name extension of (info for this_item)
					if file_extension = "avi" then
						tell VisualHub to AddFiles(this_item)
					end if
				end repeat
			end repeat
		on error
			set item_list to every item of this_item
			
			repeat with i from 1 to the number of items of item_list
				set this_new_item to item i of item_list as alias
				set file_extension to name extension of (info for this_new_item)
				if file_extension = "avi" then
					tell VisualHub to AddFiles(this_new_item)
				end if
			end repeat
		end try
	end tell
end add_this_folder

VisualHub is a bit unusual and for the moment requires line 2 to be there. I tried adding it to the subroutines thinking that maybe it was getting lost somewhere when we branched away to add the files, still no luck.

I get both confirmation dialogs saying that VH has been set up, and the script is going off to add the file (just testing with a single file at this stage). VisualHub launches and all the configuration stuff happens, but the file never makes it into the list.

Any thoughts? Have I done something stupid above??

Hi there,
glad to hear that the script was what you where looking for! Did you attach the script to a folder and test it on you machine before adapting to you script? The folder action works fine on my machine but only tested it on my OS (10.4.9), as for VisualHub it’s not an app I’ve used before so sorry I can’t be much help I’m afraid. I’m sure you’ve already done this but the only thing I can suggest is to break each section of the script down and test it with display dialog. Good Luck, please let me know how you get on.

Good suggestion - and no, I hadn’t. I’ve just done that and sadly it doesn’t work. I can’t see why though as reading through it, it seems as though it should work perfectly.

It definitely worked on your system? I’m also on 10.4.9, so that shouldn’t be a problem.

Stumped…

Yeah, I saved the script into the folder actions folder, created a new folder and attached the script. I tested it by dropping an avi file into the folder and it notified me, I put the avi file in a folder and dropped the folder on my testing folder and it worked ans lastly I buried the avi file in about 5 sub-folders and dropped it on the test folder and it worked. Maybe it would be worth making sure the info is giving the correct results.

set this_item to choose file --> choose one of your avi files
set file_extension to name extension of (info for this_item)
display dialog the result

As long as the dialog displays “avi” then I can’t see what else is different from your machine to mine:(

Yep, tested with the file I’ve been using to test and it returned ‘avi’ as expected. I’m at a loss…

Well I’ve just got a bit further. If I create a folder on my desktop and attach your script, it works. If I create a folder on the USB drive I’ve been using (FAT-formatted by the way) the script does nothing. Even stranger as the previous version of my script (before merging with yours) was running fine from the same drive.

Is there anything unusual that has to be done to make folder actions work on external drives???

V Strange, can you give this a go? just create a new folder and attach this folder action

on adding folder items to this_folder after receiving these_items
	set item_list to every item of these_items
	repeat with i from 1 to the number of items of these_items
		set this_item to item i of these_items as alias
		set file_extension to name extension of (info for this_item)
		if file_extension = "avi" then
			display dialog "I'm an avi file"
		end if
	end repeat
end adding folder items to

Sorry, forgot to mention to test it with just files not folders for now.
Cheers

Works perfectly on the folder created on the desktop…

It’s definitely the ‘add files’ bit where it fails. I’m getting the dialogs displaying properly right up to the point where it goes to add the files to VH’s queue, but they never appear.

There must be something minor i’m missing. I’ll keep experimenting but if you have any thoughts let me know.

Thanks for all your help!!! :slight_smile:

One other thought… is it possible that the result of the add_this_file section doesn’t have the same filename format as if it were added directly from the ‘on adding folder items’ command? The specific command that isn’t working is the command to VisualHub to AddFiles(this_item). I’m wonderig if this_item has different content than these_items at the beginning of the script.

I think this might be the issue. I suspect the AddFiles command is expecting a list (even if it only contains one item) and fails when a single item is added in the way I’ve done above. How do you convert the type back to a list??

please forgive my ignorance but is AddFiles a VisualHub command? perhaps you could replace the

 if file_extension = "avi" then
                       tell VisualHub to AddFiles(this_item)
                   end if

section of code with something like

 if file_extension = "avi" then
                       tell application "Finder" to display dialog "I'm an avi file"
                   end if

just to test that it’s finding the avi files?

Yes, AddFiles() is a VisualHub command. I’ve tested that it is recognising the avi files by adding a dialog with this_item appended to the dialog message. It displays as it should containing the avi file with full path.

I suppoe another alternative to the approach above would be to build a list by appending each “avi” item, then just calling AddFiles once with the newly created list.

Thoughts?

does the VisualHub need to reference an alias in the line

tell VisualHub to AddFiles(this_item)

maybe remove the parenthesis or change this_item back to a string, just clutching at straws now! but maybe, as you suggested a change of approach would work, after all there’s more than one way to skin a cat!