Deleting a script/application on exit

Hi I have wrote an apple script, which installs various files and changes system settings on a mac,
All has gone well the script does the job well. The problem I have is that I have to login to around 300 Macs using ARD and excecute the script/application, its a bit of a long procces so to speed it up a little, I want the script to delete all files and folders including the script itself once the script has finished copying and doing its job.

So far I can move all folders/files/and even the script to the trash, but I cant empty the trash, because the Script is in use! Does anyone know how I can get the script to empty the trash when it ends…I dont know like “on exit empy trash” Any Ideas? I dont want to create a second script to delete the first that would just be stupid!

Thanks in advance

hi santos,

what i would do here is try to use the Unix ‘at’ command. this is very similar to the ‘cron’ facility that we all know and love, but the job you specify is a ‘one time’ shot. here is an example of how i would script an ‘at’ command with AppleScript:


set normalCommand to "/usr/bin/at now + 1 minutes <<EOF
open /Applications/Safari.app
EOF"

set displayCommand to "/usr/bin/atq"

do shell script normalCommand
set displayVar to (do shell script displayCommand)

display dialog displayVar

obviously you want to do some kind of ‘rm -rf’ instead of opening Safari, but it’s a good example.

CAVEAT: read the man page for ‘at’ (type “man at” in the terminal). you must ‘enable’ the service and reboot (i think) to enable the ‘at’ facility. understandably, this may make the command unusable for you, but this is the best suggestion i have.

here’s how i’d script the enabling of ‘at’:


set enableAt to "launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist"
do shell script enableAt

tell application "Finder" to restart

i believe you can schedule a job before the ‘at’ is enabled, so you could do that at the end of your script and then run the above. this should be tested before deployment. here’s more:

http://www.macosxhints.com/article.php?story=20050921214411382

FINAL NOTE: the ‘at’ facility may be configured differently on older versions of OS X. this is for 10.4. also, be patient with ‘at’. i’ve noticed that it takes about a minute or so to actually do the command.

EDITED: to show how to AppleScript the enabling of ‘at’.

Great, thanks for that I will take a closer look.

Many Thanks :smiley:

Late to the party, but it should still be possible to use Finder for this - simply with something like:

IMPORTANT: this script deletes the application that runs it. Save as an application before running.

set my_file to path to me
if my_file is (path to application "Script Editor") then error number -128 (* precaution *)
tell application "Finder"
	delete my_file
	ignoring application responses
		empty
	end ignoring
end tell

(The statement marked ‘precaution’ can be removed before saving as an application - but best close the editor window immediately.)

as usual, kai’s solution seems much easier than mine :slight_smile:

Don’t feel bad waltr - you’ve lots of company -happens to me every time.

The good thing about sharing knowledge and ideas here is that we all bring something to the party - and can all learn something from the discussions. I found your presentation of the ‘at’ command very interesting, waltr - and, whichever route santos decides to take, I’ve no doubt the info will come in very handy. :slight_smile:

And I should add, Kai, that I never resent your elegant alternatives to my solutions - I learn something every time, and that’s what this forum is about in my view - a learning experience for us all.

Hear, hear, Adam. As my dear ol’ Mum used to say: “Make every effort to get to your destination - but make the most of your journey along the way.” :slight_smile:

hello kai, adam,

i certainly second adam’s sentiment. it doesn’t bother me if my solutions are not the best, or are improved upon by others. i learn way to much here to get caught up in that. that’s why i used the smiley face :smiley:

Kia, Excellent,

Thank you, :smiley:

Hi again Kia,
Once again thank you for the above script it worked a treat with a few minor adjustments.
I am interested in the statment below and would like to ask a few questions about it,

set my_path to path to me

Is there a similar way that will grab the path to the containing folder? i.e the folder in which the script is located?
or is there a way of backing up one level once the path has been grabbed.

Thanks

Ah its ok, I seem to have found the answer to my question.
If anyone is interested this is the final segment of code that I shall be using as a result of this topic.
Be WARNED it will delete the parent folder from which it is ran!!!

(* This script will move the folder that contains this script to the trash and then empty the trash without any warning or second chance.....be warned!!!!!!!!!! *)

tell application "Finder"
	set this_folder to container of (path to me)
	delete this_folder
	ignoring application responses
		empty
	end ignoring
end tell

Thanks everyone for your help