Script installing and attaching folder action script - almost perfect?

Hello,

I’ve written a script that seems to work fairly well to create a folder in the home folder and attach a folder action script to it that prints (or converts to PDF) any PostScript file that drops into it. The one thing that I can’t get right is this:

If I run the script a second time, it re-attaches the folder action script, with the result that a PostScript file dropped into the folder gets printed twice. I thought I had figured out a way to prevent this, but it isn’t working. Can anyone tell me what I’m doing wrong? Here’s the script (of course the script is an Application with the folder action script .scpt file inside it):

property msgTitle : "Install Folder for PrintFiles"
set firstRun to no

-- Running on Leopard or later?
set reqVers to 1050
set verCheck to minOSVers(reqVers)
if verCheck = false then
	set dialogText to "This installer only runs under OS X 10.5 or later, and you seem to have an earlier version of OS X."
	errorQuit(dialogText)
else
	set dialogText to "Proceed with installation of output folder for printfiles?"
	tell me to activate
	display dialog dialogText buttons {"Cancel", "OK"} default button 2 with icon caution with title msgTitle
	set userChoice to button returned of result
end if

-- Set up various paths
tell application "System Events"
	set userPath to path of home folder
	set libPath to path of library folder of user domain
end tell

tell application "Finder"

	set thisApp to (path to me) as string
	set thisApp to thisApp as alias
	set filesFolder to (thisApp & "Contents:Resources:Files") as string
	set filesFolder to filesFolder as alias
	set printAction to (filesFolder & "PrintOrPDFWPMac.scpt") as string
	set printAction to printAction as alias
	
	try
		make new folder at userPath with properties {name:"Folder for PrintFiles"}
	end try
	try
		make new folder at libPath with properties {name:"Scripts"}
	end try
	set userLibScrPath to (libPath & "Scripts") as string
	set userLibScrPath to userLibScrPath as alias
	try
		make new folder at userLibScrPath with properties {name:"Folder Action Scripts"}
		set dialogText to "Folder for PrintFiles created."
		display dialog dialogText
	end try
	set userFldrPath to (userLibScrPath & "Folder Action Scripts") as string
	set userFldrPath to userFldrPath as alias
	duplicate printAction to userFldrPath replacing yes
end tell

-- Process folder action script
tell application "Finder"
	set printPath to (userPath & "Folder for PrintFiles") as string
	set printPath to printPath as alias
	set actionScript to (userFldrPath & "PrintOrPDFWPMac.scpt") as string
	set actionScript to actionScript as alias
end tell
tell application "System Events"
	set curScripts to (attached scripts of printPath) as string
	if curScripts does not contain "PrintOrPDFWPMac.scpt" then
		try
			attach action to printPath using actionScript
			set dialogText to "Folder action attached."
			display dialog dialogText
		on error
			tell me to activate
			display dialog "Error attaching folder action script."
		end try
		set folder actions enabled to true
	end if
end tell

error number -128

-- Subroutines

on errorQuit(dialogText) -- Fatal error, quitting script
	tell me to activate
	display dialog dialogText buttons {"OK"} default button 1 with icon stop with title msgTitle giving up after 5
	error number -128
end errorQuit

on minOSVers(reqVers) -- test for required OS version; reqVers is e.g. 1041 = 10.4.1
	set numList to characters of (reqVers as string)
	set decNum to 0
	repeat with num in numList
		set decNum to (16 * decNum) + num
	end repeat
	set sysv to (system attribute "sysv")
	if sysv < decNum then return false
	return true
end minOSVers

Many thanks for any help with this. Folder action scripts seem to present a LOT of problems for scripters.

Hi,

I changed your script a bit. For the path definitions and folder creations the Finder or System Events is not needed.
The Standard Addition’s command path to has a folder creation parameter, which creates the folder if it doesn’t exist.

attached scripts of System Events returns a list. If more than one script is attached, it will throw an error which coercing to text (by the way: Leopard has only one text class: text)

I changed also the line attach action line to fulfill the required classes


property msgTitle : "Install Folder for PrintFiles"
property reqVers : 5
set firstRun to false

-- Running on Leopard or later?
if ((system attribute "sysv") mod 4096 div 16) < reqVers then
	set dialogText to "This installer only runs under OS X 10.5 or later, and you seem to have an earlier version of OS X."
	errorQuit(dialogText)
else
	set dialogText to "Proceed with installation of output folder for printfiles?"
	tell me to activate
	display dialog dialogText buttons {"Cancel", "OK"} default button 2 with icon caution with title msgTitle
end if

-- Set up various paths

set userPath to path to home folder as text
set libPath to path to library folder from user domain as text
set userLibScrPath to path to scripts folder from user domain with folder creation
set userFldrPath to path to Folder Action scripts with folder creation

set thisApp to path to me as text
set filesFolder to (thisApp & "Contents:Resources:Files:")
set printAction to alias (filesFolder & "PrintOrPDFWPMac.scpt")
set filesFolder to filesFolder as alias

do shell script "/bin/mkdir -p " & quoted form of POSIX path of (userPath & "Folder for PrintFiles")
set printPath to (userPath & "Folder for PrintFiles")
do shell script "/bin/cp " & quoted form of POSIX path of printAction & space & quoted form of POSIX path of userFldrPath
set actionScript to (userFldrPath as text) & "PrintOrPDFWPMac.scpt"

-- Process folder action script
tell application "System Events"
	launch
	set curScripts to (get attached scripts of folder printPath)
	if curScripts is {} or item 1 of curScripts is not (actionScript as alias) then
		try
			attach action to folder printPath using actionScript
			set dialogText to "Folder action attached."
			display dialog dialogText
		on error e
			tell me to activate
			display dialog "Error attaching folder action script:" & return & e
		end try
		set folder actions enabled to true
	end if
end tell

-- Subroutines

on errorQuit(dialogText) -- Fatal error, quitting script
	tell me to activate
	display dialog dialogText buttons {"OK"} default button 1 with icon stop with title msgTitle giving up after 5
	error number -128
end errorQuit

Stefan,

Thank you - both for the improved script, and for helping me to understand the changes in Snow Leopard, and also for improving my understanding of AppleScript! I am deeply grateful.

Now I have one more question:

The script, as you revised it, works perfectly for two minor problems, and I would be grateful if you could help me figure out how to fix them

  1. If the folder action script is installed and attached to the folder, but the folder is not check-marked ON in the left-hand pane of the Folder Actions Setup dialog, how do I force the folder to be check-marked On?

  2. If the folder action script is installed and attached to the folder, but the script is not checked ON in the right-hand pane of the Folder Actions Setup dialog, how do I force the folder to be check-marked ON?

I have tried to search for answers to these questions, but I cannot find them. Any help you can give would be gratefully received.

Thank you again.

I don’t know, but if you use scripts to control the folder actions it actually doesn’t matter.

I ran the script (I’m still on Leopard) and after opening the Folder Action Setup window both sides are checked

Thank you again. But what happens if you now UNcheck the folder or the script, and then run the script again?

On Snow Leopard, if I uncheck both “on” boxes in the Folder Action Setup dialog, and then run the script a second time, both the folder and the script remain UNchecked. Is it possible to test for the on/off state and restore it through AppleScript?

I spent a lot of time searching for the answer a few months ago, and never found it!

What is the purpose of changing the checkboxes manually??

I have no reason to change the checkboxes myself, but I want this script to be able to fix a mistake made by a user who might uncheck the boxes for any reason (testing, stupidity, etc.)

So IF the user has (for some reason) changed the checkboxes by himself - perhaps because he was experimenting with his settings - I want him to be able to run the script a second time, and have the checkboxes be put back to the state that the installer put them when the script was first run.

Is that possible?

replace the Process folder action script with


tell application "System Events"
	launch
	set curScripts to (get attached scripts of folder printPath)
	if curScripts is {} or item 1 of curScripts is not (actionScript as alias) then
		try
			attach action to folder printPath using actionScript
			set dialogText to "Folder action attached."
			display dialog dialogText
		on error e
			tell me to activate
			display dialog "Error attaching folder action script:" & return & e
		end try
		set folder actions enabled to true
	else if curScripts is not {} and item 1 of curScripts is (actionScript as alias) then
		tell folder action "Folder for PrintFiles"
			set enabled to true
			set enabled of script "PrintOrPDFWPMac.scpt" to true
		end tell
	end if
end tell

Stefan,

Beautiful! Thank you!

In case the user also disabled Folder Action Scripts entirely, I moved one line toward the top of the block, like this:

tell application "System Events"
   launch
   set folder actions enabled to true
   set curScripts to (get attached scripts of folder printPath)
   if curScripts is {} or item 1 of curScripts is not (actionScript as alias) then
       try
           attach action to folder printPath using actionScript
           set dialogText to "Folder action attached."
           display dialog dialogText
       on error e
           tell me to activate
           display dialog "Error attaching folder action script:" & return & e
       end try
       -- set folder actions enabled to true
   else if curScripts is not {} and item 1 of curScripts is (actionScript as alias) then
       tell folder action "Folder for PrintFiles"
           set enabled to true
           set enabled of script "PrintOrPDFWPMac.scpt" to true
       end tell
   end if
end tell

Thank you again - I am now going to improve some of my other scripts on the basis of what I have learned from all this.