Automating setting Folder Actions, turning ON/OFF

G’day

Someone might find this script useful. It automates the process of installing, applying, and turning On/Off Folder Action scripts. Set up a folder with this script in it as an application, and in the folder store the script to be applied to the Folder Actions. In this case the script is called ‘ftp Downloader.scpt’, but you can rename the property to match your script name.

You’ll also have to set the path to your parent folder containing the folders to apply the Folder Actions on, if it’s not on the desktop.

Regards

Santa


property TurnOFFindividually : false

# The desktop Folder containing folders that have to be reset
property FolderName : "ftp Downloads"

# The Folder Action script name
property ScriptName : "ftp Downloader.scpt"

on run
	tell application "Finder"
		activate
		display dialog "This app checks for any misplaced '" & ScriptName & "' scripts, and if any are found, removes them, and resets all the Folder Attachments to point to the new script." & return & return & "A copy of the new script is stored in the 'Folder Action Scripts' in the invisible user Library." & return & return & "DO NOT remove the script '" & ScriptName & "' from the folder this Application is in." & return buttons {"Continue", "Cancel"}
	end tell
	my MainLoop()
end run

on MainLoop()
	set p2ftpDownloads to ((path to desktop) & FolderName) as text
	set p2me to path to me
	set p2Scripts to (path to scripts folder) as text
	set p2FAS to (path to Folder Action scripts folder) as text
	tell application "Finder" to set p2cofme to container of (path to me) as text
	set saveFolderAction to (p2cofme & ScriptName) as text
	
	###########################
	# Check User Library for extraneous script
	###########################
	try
		tell application "Finder"
			set temp to every folder of folder p2Scripts
			set FoundFlag to false
			repeat with testFolder in temp
				if name of testFolder ≠ "Folder Action Scripts" then
					if exists file ScriptName in testFolder then
						say "extra f t p downloads script found"
						move file ScriptName of testFolder to trash
						set FoundFlag to true
					end if
				end if
			end repeat
		end tell
	on error errmsg
		display dialog errmsg
	end try
	
	###########################
	# Check Boot Library for extraneous script
	###########################
	set tempRootLevelLibrary to ((path to startup disk) & "Library:Scripts") as text
	try
		tell application "Finder"
			set temp to every folder of folder tempRootLevelLibrary
			repeat with testFolder in temp
				if exists file ScriptName in testFolder then
					say "extraneous f t p downloads script found"
					move file ScriptName of testFolder to trash
					set FoundFlag to true
				end if
			end repeat
		end tell
	on error errmsg
		display dialog errmsg
	end try
	
	###########################
	# Resave new Attachment script
	###########################
	try
		tell application "Finder"
			if exists file saveFolderAction then
				if exists file ((p2FAS & ScriptName) as text) then
					move file ScriptName of folder p2FAS to trash
				end if
				copy file saveFolderAction to folder p2FAS
			end if
		end tell
	on error errmsg
		display dialog errmsg
	end try
	
	###########################
	# Found misplaced script, so reset all Folders
	###########################
	if FoundFlag then # extra script found earlier, so strip any reference to it
		tell application "System Events"
			set ChosenFolders to name of every folder action
			repeat with EachFolder in ChosenFolders
				set FolderActionName to contents of EachFolder
				try
					delete script ScriptName of folder action FolderActionName
				end try
			end repeat
		end tell
	end if
	
	###########################
	# Attach Folder Actions, query those turned off
	###########################
	tell application "Finder"
		set TempList to folders of folder p2ftpDownloads as list
	end tell
	tell application "System Events"
		set folder actions enabled to true
		set theCounter to 0
		set AllreadySetCounter to 0
		set NotSetCounter to 0
		set TurnOnCounter to 0
		set OFFList to {}
		repeat with EachFolder in TempList
			try
				attach action to (EachFolder as alias) using ((p2FAS & ScriptName) as text)
				set theCounter to theCounter + 1
			on error errmsg number errNum
				if errNum = -48 then -- Folder action already exists, but is it active?
					set AllreadySetCounter to AllreadySetCounter + 1
					set tempActionsList to folder actions
					set exitflag to false
					repeat with EachFolderAction in tempActionsList
						if name of EachFolderAction = name of EachFolder then
							try
								set FAScripts to name of every script of EachFolderAction --EachFolder
								if ScriptName is in FAScripts then
									set exitflag to true
									if enabled of EachFolderAction is false then
										display dialog "The following Folder Action '" & ScriptName & "' is turned OFF for folder " & return & return & (name of EachFolder as text) & return & return & "Do you want to turn it ON?" & return buttons {"Turn ON", "Keep OFF"}
										if button returned of the result = "Turn ON" then
											set TurnOnCounter to TurnOnCounter + 1
											set enabled of EachFolderAction to true
										else
											set end of OFFList to "<" & (name of EachFolder as text) & ">" & return
											set NotSetCounter to NotSetCounter + 1
										end if
									else
										if TurnOFFindividually then
											display dialog "The following Folder Action '" & ScriptName & "' is turned ON for folder " & return & return & (name of EachFolder as text) & return & return & "Do you want to turn it OFF?" & return buttons {"Keep ON", "Turn OFF"}
											if button returned of the result = "Turn OFF" then
												set enabled of EachFolderAction to false
												set NotSetCounter to NotSetCounter + 1
												set end of OFFList to "<" & (name of EachFolder as text) & ">" & return
											end if
										end if
									end if
								end if
								if exitflag then exit repeat
							on error errmsg
								display dialog errmsg
							end try
						end if
					end repeat
				end if
			end try
		end repeat
		set temp to "Settings for Folders with Attachment... " & return & return & ScriptName & return & "______________________________________________________" & return & return & "Number of Folder Actions that were automatically set was " & theCounter & "." & return & return & "Number of Folder Actions already set was " & AllreadySetCounter & " out of a total of " & (count of TempList) & " Folders." & return & return & "Number of Folder Actions that were OFF but turned ON is " & TurnOnCounter & "." & return & return & "Number of Folder Actions that are turned OFF is " & NotSetCounter & "." & return & OFFList & return & "However, if you want to turn ALL the Folders OFF, and then reset them individually to OFF or ON, click the 'Reset to OFF' button." & return
		set theButtons to {"Toggle individually", "Reset to OFF", "Finished"}
		if NotSetCounter > 0 then
			set temp to temp & return & "Or, re-cycle this app to individually reset any OFF folders." & return
			set theButtons to {"Re-cycle", "Reset to OFF", "Finished"}
		else
			set temp to temp & return & "Or, re-cycle this app to individually toggle folders." & return
		end if
		tell application "Finder"
			activate
			display dialog temp buttons theButtons giving up after 120
		end tell
		set temp to the result
		if the button returned of temp is "Reset to OFF" then
			repeat with EachFolderAction in tempActionsList
				try
					set FAScripts to name of every script of EachFolderAction
					if ScriptName is in FAScripts then
						set enabled of EachFolderAction to false
					end if
				end try
			end repeat
			set TurnOFFindividually to false
			my MainLoop()
		end if
		if the button returned of temp is "Re-cycle" then
			set TurnOFFindividually to true
			my MainLoop()
		end if
		if the button returned of temp is "Toggle individually" then
			set TurnOFFindividually to true
			my MainLoop()
		end if
	end tell
	
end MainLoop