The evil Access violation and the Do shell script. Once upon a time...

A little history:

I have been having a problem with my folder actions in that they would often disappear (become unlinked?) when I powered my Mac down. I have since discovered that it is only the folder actions that reside on my external HD (USB 2.0 Seagate) that disappear and that the folder actions on my Macintosh HD are all intact - odd that. Unfortunately all of my FTP folders are on the external drive because it has a far greater capacity (500 gb) than the main (80gb) so it isn’t at all feasible to just up and move the FTP folders onto the main disk to fix this problem, plus this is a Mini so upgrading the HD is probably not possible either.

So I wrote an apple script to turn into an app that runs every time the computer boots up just to test a folder, whose only reason to exist is to delete any file placed inside it, to make sure it is working properly OR it is supposed to delete all the files from the “Macintosh HD:Library:Caches:” folder and reboot, for some reason this does fix the problem and all of the folders on the Seagate drive will work properly - until the NEXT time.

The problem now is Unix, or rather my incomplete understanding of it. Please see the script below:

delay 30 -- Wait for drives to finish mounting
		set lScriptsFolder to ((path to applications folder) & "Design-Mo-Tron:Components:Scripts:") as Unicode text
		set lTapperFolder to ("Macintosh HD:Volumes:ESD MAC:Tapper:") as Unicode text -- Path to a folder on the external drive
		set lTapText to (lTapperFolder & "Tapped.txt") as Unicode text
		tell application "Finder" to make new file at lTapperFolder with properties {kind:"Plain text document", name:"Tapped.txt"} -- Must create empty text file
		delay 5 -- Wait for folder action
		tell application "System Events" to if exists lTapText then -- Problem with the folder scripts again
			try
				tell application "Finder" to delete lTapText -- Remove it because the folder actions didn't
				do shell script "sudo rm -r /Library/Caches/*" with administrator privileges -- Run tasks with admin priveleges
				display dialog "Deletion of cache files completed." & return & "Restarting in 30 Seconds" buttons {"Ok"} giving up after 5 with icon caution
				delay 25 -- Wait 30 seconds total
				tell application "System Events" to restart -- Restart the computer
			on error number -1728 -- Can't get file
				quit application "Restart - Auto"
			end try
			
		else
			ignoring application responses
				tell application "Design-Mo-Tron" to activate -- Launch the Queue Manager
			end ignoring
		end if

The line do shell script “sudo rm -r /Library/Caches/*” with administrator privileges or even do shell script “sudo rm -r /Library/Caches/*” password “MYPASSWORD” with administrator privileges results in an ‘Access violation occurred’ error. How much more access is there than SUDO?

Try removing sudo and doing it like so

do shell script "rm -r /Library/Caches/*"  user name "a admin account" password "the password" with administrator privileges

Hi,

the error ‘Access violation occurred’ happens, if the line including “administrator privileges” is within an application tell block.

Note: Using sudo(8) with with administrator privileges is generally unnecessary and creates security holes; simply remove the “sudo”.
see Technical Note TN2065: do shell script in AppleScript

Interesting can you tell me where that was found at?

[Edit] - Well it says that you can’t in the link you provided, it just made no mention of the actual error you would receive.

Also to work around it, assuming you don’t want to move the statement elsewhere, this appears to work.

tell application "System Events"
	-- other stuff
	tell me to do shell script "rm -r /Library/Caches/*" user name "ADMIN ACCOUNT" password "PASSWORD" with administrator privileges
	-- more stuff
end tell

[Edit 2] - And sure enough it mentions to do this in the link LOL - Perhaps I should have read that thing before running off to find a way around it and asking silly questions, even if I did digure out the appropriate way around. =)

I don’t know, I’ve just tested it a few months ago and it only works reliably without a tell block

Interesting. So keep it out of a tell application block… I suspected that might be the case because of some of the other posts on this board that referenced problems like this, so I copied the only tell application blocks I used at the end of this post. I figured that:

tell application "Whatever.app"
      delete "somefile.txt"
end tell

might be the wrong but figured that:

tell application "Whatever.app" to delete "somefile.txt"

would fix it. Apparently not. Anyhow thanks for the help and I will try it tonight and see if it works tomorrow.