Script to attach folder actions to new folders

I’m working on a script that generates a bunch of new numbered folders and attaches a particular folder action to each of them. I’ve put much of the code together using snippets from various working example scripts, but the line for attaching a folder action does not work in my script.

Can anyone explain why my use of
“tell application “System Events” to attach action to new_folder using the_script”
works in the Apple example, but not in my script?

Thanks in advance.
Azide.

Below is my patched together script:

on run
set total_folders to 3
set the_script to {alias ((path to Folder Action scripts folder from local domain as Unicode text) & “PicRename.scpt”)} as item
set target_folder to (choose folder with prompt “Where should the folders be created?”) as alias

repeat with folder_count from 0 to (total_folders - 1)
	set the_folder_name to (my add_leading_zeros(folder_count, 3))
	
	tell application "Finder"
		try
			set new_folder to make new folder at target_folder with properties {name:the_folder_name}
		on error the_error
			beep
			display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 10
			return
		end try
	end tell
	
	-- choose file with prompt "pick a script"
	-- set the_script to the result
	
	tell application "Finder"
		open the_script
	end tell
	
	tell application "System Events" to attach action to new_folder using the_script
	
end repeat

end run

on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to “”
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & “0”) as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros


Does this work (only three lines were modified)?

set total_folders to 3
set the_script to alias ((path to Folder Action scripts from local domain as Unicode text) & "PicRename.scpt") -- MODIFIED
set target_folder to (choose folder with prompt "Where should the folders be created?") -- MODIFIED

repeat with folder_count from 0 to (total_folders - 1)
	set the_folder_name to (my add_leading_zeros(folder_count, 3))
	
	tell application "Finder"
		try
			set new_folder to make new folder at target_folder with properties {name:the_folder_name}
		on error the_error
			beep
			display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 10
			return
		end try
	end tell
	
	-- choose file with prompt "pick a script" 
	-- set the_script to the result 
	
	tell application "Finder"
		open the_script
	end tell
	
	tell application "System Events" to attach action to folder (new_folder as text) using the_script) -- MODIFIED
	
end repeat
end run

on add_leading_zeros(this_number, max_leading_zeros)
	set the threshold_number to (10 ^ max_leading_zeros) as integer
	if this_number is less than the threshold_number then
		set the leading_zeros to ""
		set the digit_count to the length of ((this_number div 1) as string)
		set the character_count to (max_leading_zeros + 1) - digit_count
		repeat character_count times
			set the leading_zeros to (the leading_zeros & "0") as string
		end repeat
		return (leading_zeros & (this_number as text)) as string
	else
		return this_number as text
	end if
end add_leading_zeros

– Rob

Thanks Rob.
Your script changes did not work right away, but they did give me the clue I needed. I finally changed the key line to read as below and things worked:

tell application “System Events” to attach action to (new_folder as text) using (the_script as Unicode text)

However, It seems I need to reboot for the system to see the folders as having Folder Actions attached to them. (Possibly I only needed to log out, but I have not tested that yet).

Does anyone know if it does bad things to have thousands of folders with actions attached?

Thanks again.
Azide

I’m glad that the script is working. Maybe if you set the newly attached folder action to “enabled” a logout/restart won’t be required.

tell application "System Events" to ¬
	set enabled of folder action "folder name" to true

I don’t know about the impact of thousands of folder actions. It might cause System Events to become sluggish but I have nothing to base this on. I hope you’ll let us know once you find out. :slight_smile:

– Rob

Making thousands of folders with attached scripts did indeed make System Events sluggish to the point where it did not realy work at all. I’m going to have to try a radically different approach to this problem.

Thanks again for your help.
Azide

Bummer. Thanks for letting us know.

– Rob