Folder actions when folder (rather than file) is added

From the VisualHub documentation:

I’m guessing this means it needs to be converted back to a ‘list’ type. It would probably be cleaner anyway if (given my modified script above) in the repeat loop, whenever a folder is encountered and add_this_folder is called, the resulting files are just added to the end of a list. Also in the repeat loop, when a file is encountered just add it to the end of the list, rather than calling add_this_file.

Trouble is, I’m not at all familiar with working with lists. I know there are loads of examples on the forum, just a matter of locating and interpreting. If you have any simple suggestions I’m open to ideas.

Thanks again,

Tom

Morning, just guessing again but how about if we turn this_item back to a string and try adding it

if file_extension = "avi" then
			set this_item_string to this_item as string
			display dialog this_item_string --> put your add code in here
		end if

maybe just try this with a file and not a folder first.

Think I found an easier fix than my last post above. Just adding the brackets {} into the AddFiles() command appears to have sorted it. Off to do more testing… :smiley:

It works!!! And it works really well. I’d still like to (eventually) change the script to build a single list, then submit the list once rather than adding each item. For now - a huge thanks to everyone - especially blend3 - for all the advice.

I’ll post back the final script tonight for anyone happening on this thread in future.

Cheers,

Tom :cool:

Great news, really pleased it’s working :smiley:

Well I may have spoken too soon… although it does work I think the flow needs some work. The problem with the current process is that it ends up launching VHub, setting it up, etc. even if an ‘avi’ file wasn’t added.

I need to rearrange the script as follows:

[i]on adding items
for each item
check if file or folder
if folder, recurse until a file is reached
add each “avi” file to a list ← need help with this bit

if the resulting list contains 1 or more items ← and not sure on this bit
do the VisualHub ‘stuff’[/i]

I’ve got all the components above except the list manipulation and checking. I’m digging through the Applescript docs but it’s not the best. If anyone has any pointers I’d be grateful.

Thanks all,

Tom

Well I moved everything around as planned above, and studied the Applescript reference on lists, but obviously I’m missing something. The script below launches fine, and testing with a single file I get the dialog displaying the file name, followed by a dialog with the correct file count. VisualHub launches but fails to add the files for processing. It has to be something to do with the ‘list’ submitted to the AddFiles() command not being the correct type.

Can anyone see a problem with what I’ve done?

on adding folder items to this_folder after receiving these_items
	set files_to_process to {}
	
	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
			add_this_folder(this_item)
		else
			copy this_item to end of files_to_process
		end if
	end repeat
	display dialog "Result: " & files_to_process
	
	-- only  call VisualHub if one or more valid avi files were found
	set number_of_files to the number of items of files_to_process
	display dialog "Files: " & number_of_files
	if number_of_files > 0 then
		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
			LoadSettings("Mac Home Server:Users:tomwynne:Desktop:settings.vhub")
			SetSaveLocation("Media:Movies")
		end tell
		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 ¬
				number_of_files & " File(s) have been added for processing." application name "VisualHub Automation"
		end tell
		delay 10 --delay to allow file movement to complete
		tell VisualHub
			AddFiles(files_to_process as list)
			StartConversion()
		end tell
		-- 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 if
end adding folder items to

(*on add_this_file(this_item)
	set file_extension to name extension of (info for this_item)
	if file_extension = "avi" then
		if number_of_files = 0 then
			tell VisualHub to ClearAll()
		end if
		set number_of_files to number_of_files + 1
		tell application "VisualHub" to set VisualHub to load script (scripts path of main bundle & "/automation.scpt" as POSIX file)
		tell VisualHub
			AddFiles({this_item})
		end tell
	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
						copy this_item to end of files_to_process
					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
					copy this_new_item to end of files_to_process
				end if
			end repeat
		end try
	end tell
end add_this_folder

Hi Tom,
damn it, I thought you had it! take a look at this example of adding to a list. You should be able to incorporate this into what you already have.

set files_to_process to {}
tell application "Finder" to set startup_disk_items to every item of startup disk
repeat with this_item in startup_disk_items
	set this_item_string to this_item as string
	set files_to_process to files_to_process & this_item_string
	choose from list mylist
end repeat

Thanks - but I’m a bit confused. I know the AddFiles() command requires a list as input. Are you saying in order to have a ‘list’ of ‘files’ I have to first convert each item to a string?

The script we ended up with earlier did work - ie. files did get added to VisualHub and processed. I realised though that it would be a problem as there would be some situations in which other files would get added to the watched folder (not ‘avi’ files) and although these wouldn’t have been procssed they still would have triggered VisualHub.

When I created the revised script I just removed the AddFiles command from the subroutines and changed it to add to the ‘list’. The compiled list then gets submitted to VisualHub only if it contains one or more items.

The logic seems sound to me, but clearly I’m missing something.

I was just using a string so that you had a visual of adding to a list. The code below shows how a list aliases would work. Once you’ve got a list of aliases then can you pass this on to the next part of the script?

set mylist to {}
repeat 5 times
	set thisfile to choose file
	set mylist to mylist & thisfile
end repeat
tell application "Finder"
	open every item of mylist
end tell

Here’s an example of how this would apply to your code, I think :expressionless:

set files_to_process to {}
if file_extension = "avi" then
	set files_to_process to files_to_process & this_item
end if

I think that’s exactly what I’ve done above. I created the list files_to_process at the beginning. I then used your code to iterate through the items added to the folder. If this item is a folder it goes off to your subroutine to recurse until it finds a file, then appends the file to files_to_process. If a file is found rather than a folder, it just appends this directly to the list.

Once finished with all items added, it launches VisualHub, configures, and adds the files. Everything is working as it should, but the files just don’t add. When this was occurring before, adding brackets {} around the items in AddFiles fixed it, but that was when we were adding individual items. No we’re adding a whole list of items at once. I just can’t see the problem with it.

Frustrating! :confused:

Mine was using

copy this_item to end of files_to_process

but I’ve also tried your format

set files_to_process to files_to_process & this_item 

Neither works. :frowning:

lookin at your earlier post

AddFiles(theFiles) - Adds files to VisualHub's File List. Accepts AppleScript lists only. AppleScript-style colon-paths
preferred, POSIX paths also allowed.
Example commands:
AddFiles({"Macintosh HD:Users:scripter:Movies:awesomevid.mpg", "Macintosh HD:Users:scripter:Movies:stupidvid.mpg"})
AddFiles({"/Users/scripter/Movies/awesomevid.mpg"})

should the AddFiles bit not be something like

 AddFiles (files_to_process)--> files_to_process being the list

That’s exactly what i’ve got…

I’m afraid I’m at a blank then. The only other thing is to convert the path of the alias to POSIX path
:cool:

       set this_item to item i of these_items as alias
set this_item_POSIX_path to POSIX path of this_item

Hi Tom,
maybe you could, if you havn’t already trying to pass the list as strings or POSIX paths if passing a list of aliases doesn’t work, failing that maybe posting this problem with a new title would grab somebody elses attention. If you get it to work please let me know as I don’t like to be beaten by a machine!:o

Hi again,

Thanks for the suggestions. As I’m certain its the VisualHub ‘stuff’ where it’s failing, I’m going to post it over in their automation forum and hopefully someone will spot my mistake. I’ve gone through it so many times now - I may be missing the obvious.

I’ll definitely post back any findings.

Thanks,

Tom

Hi everyone,

Sorry to hijack this thread, but my query does utilise a lot of what has been discussed here. If you feel this is inappropriate and should be moved to a new thread, please say so and I will happily oblige.

I would also like to process subfolders containing avi files when they are dropped into a parent folder. What I would like the script to do, instead of passing the files to VisualHub (altho that is a modification I may build in later!!), is to move all the avi files from the subfolder back into the parent folder and to then delete the subfolder if it is empty (disregarding any DS_Store files etc). A dialog box giving a notification that the subfolder could not be deleted because it is not empty (of non-hidden files) would be a useful, if trivial, addition.

I have had a couple of shots at making this work and seem to be running out of ideas, so would appreciate any help that could be given on this matter. All I have managed to get working at this point is a very simple script that moves any subfolder contents to the parent folder. Every time I try to incorporate recursive checks into the subfolder to look specifically for avi files, it goes wrong.

Many thanks for any assistance you can offer on this.

Dan