Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Uninstall an AppleScript application from within Itself System
If you make tiny apps that you distribute to friends and family who aren't particularly Mac-literate, you might find this snippet of code helpful:
set theUNIXPath to path to me as alias
set thePosixPath to (POSIX path of theUNIXPath) as string
set thePosixPath to (items 1 thru ((length of thePosixPath) - 1) of thePosixPath)
		
do shell script "sleep 1 &> /dev/null &
mv " & thePosixPath & " ~/.trash/YOURAPPNAMEGOESHERE.app"
return
Basically, any app with that code inserted (and properly activated based on response to a user's action) moves itself to the trash.

How it works: it calls sleep 1 with a bash redirection command &> /dev/null &, which initiates the script, but returns to the app immediately. The effect is that your AppleScript app will exit before the second command moves the app to the trash.
    •    
  • Currently 2.19 / 5
  You rated: 3 / 5 (16 votes cast)
 
[7,037 views]  

Uninstall an AppleScript application from within Itself | 16 comments | Create New Account
Click here to return to the 'Uninstall an AppleScript application from within Itself' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Uninstall an AppleScript application from within Itself
Authored by: davidv on Dec 09, '09 08:04:42AM

I tried this (copy paste from the web page into terminal) on my Macbook Pro mid June 2009 running 10.6.2 and it gives an error "-bash: /defaults: No such file or directory". Any suggestions?

---
davidv



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: Anonymous on Dec 15, '09 10:41:14AM
It's not a shell script. It's an Applescript.

[ Reply to This | # ]
A more reliable version...
Authored by: ChaChi on Dec 09, '09 08:21:05AM
The script in this hint has a couple problems and things that aren't necessary. The following script has been tested in 10.6 only. Don't see why it wouldn't work in 10.5 or earlier though.
set appPath to path to me
set appName to name of (info for appPath)
do shell script "mv " & quoted form of POSIX path of appPath & space & 
	quoted form of POSIX path of ((path to trash) & appName as string)
Hope this helps...

[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: tedw on Dec 09, '09 08:48:09AM
No shell scripting required for this. Here's an all-applescript handler you can call to have your app move itself to the trash.
on applicide()
	set myAppFile to path to me
	tell application "Finder"
		delete myAppFile
	end tell
end applicide


[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: MJCube on Dec 09, '09 04:54:08PM

Truly elegant.



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: metiure on Dec 10, '09 03:02:45AM

applicide? some applescript dialect?



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: D-Man on Dec 10, '09 04:31:59AM

AppleScript Dialect? That depends on your perspective. It's really more of a word play.

applicide = aapl + icide

appl is an abreviated form of the word application
icide is the last part of the word suicide



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: ChaChi on Dec 10, '09 08:20:23AM

No shell scripting required, I just prefer to avoid using another application (in this case the Finder) to achieve anything in AppleScript. I personally don't have the Finder running very often. So, the Finder would have to launch to throw the item in the trash for me, where a shell script silently accomplishes the same task.

Your script worx great as well though!



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: tedw on Dec 10, '09 09:11:25AM
err... you "prefer to avoid using another application [...] to achieve anything in AppleScript"? Applescript was designed for inter-application scripting - I'm not sure what you'd use it for without scripting other apps, and there are any number of superior languages for regular run-of-the-mill programming.

At any rate, applescript is not going to move the file to the trash on its own. Scripting the Finder is easy and convenient for most people, who have the Finder running all the time, but if you don't use the Finder then you're going to have to script PathFinder, or script System Events, or script unix, or script some other app or process that has the capacity to work with the file system. That's just the nature of the applescript beast.

You've actually got me curious, though: I wonder what percentage of os X users don't use the Finder? I've gotta think it's less than 1%, even among the tech-oriented people you find on this site. Maybe I can get Rob to post a survey.

[ Reply to This | # ]

Uninstall an AppleScript application from within Itself
Authored by: ChaChi on Dec 10, '09 09:29:40AM

Ummm, yes I prefer to avoid using another application via AppleScript to achieve any goal I'm trying to achieve. If I have to I will, but otherwise I won't. Ever since "do shell script" came out I've tried to use it exclusively. Yes, most people have the Finder running all the time, I don't. I wrote the script posted above which does not call upon any other actual application to achieve the desired results. I know there are other languages that are better, but this hint was about AppleScript. I find scripting the Finder to be tedious and slower in producing results than using AppleScript's "do shell script" command. That's a personal preference based on over 20 years worth of experience using AppleScript. The true nature of the "AppleScript beast" is options. There are tons of different ways in which an individual can write a script to achieve his/her desired goal. Neither way is necessarily wrong unless the script is failing. Like I said in my reply to your post, "Your script worx great as well though!", meaning exactly that. It was a compliment to using a different approach than I myself used. I was just giving a reason as to why I wrote my script the way I did.

I just personally have something against the Finder in general. It is, and has been, one of the buggiest, most unfortunate, downfalls of OSX since OSX was originally introduced. It gets better with time, but it's still got a long way to go!

I hope this clarifies what I was saying before. AppleScript is actually a really decent programming language due to its flexibility.



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: tedw on Dec 10, '09 11:02:17AM

I'm not suggesting that your approach doesn't work, or that it's a bad way to do it. I'm just thinking that it's needlessly complex for the average user. You can work with your system any way you want to, but the way you use it is not necessarily what I would recommend to everyone else reading this thread.

I script (and use) the Finder frequently, and while it has its irritations (I'm particularly annoyed by how difficult it can be to coerce between Finder path specifications and normal aliases or posix style referents) I almost never run into *actual* bugs. As graphical file system controllers go, it's not half-bad.

just out of curiosity, what do you normally do: quit the Finder and do all of your file/folder referencing through the command line? Or do you run an alternate GUI file system controller like PathFinder?



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: ChaChi on Dec 10, '09 11:58:51AM

Well, I actually wouldn't recommend AppleScript for the average user at all. I would recommend they try to find a solution that already exists or dive into learning Automator first before tackling a programming language of any kind.

The bugs I run into in the Finder are when actually attempting to use the Finder and not while scripting it. When scripting it, I find it endlessly annoying and more time consuming to script. Again, personal preference.

I use the command line.



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: everkleer80 on Dec 11, '09 08:24:51AM

I'd consider myself more skilled than the average user, but I didn't even know you could run OS X without the Finder (or Path Finder or something.) How do you quit the Finder without logging off? It just relaunches when you kill/force quit...

Anyway, just to add my two cents, I can't remember ever running into any bugs in the Finder (except for maybe minor things such as available SMB network shares not showing in the sidebar sometimes or other issues that I could work around too easilly to bother troubleshooting.) I think the Finder is great - definately much better than Windows Explorer and its counterparts on the other OSes I've used.



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: ChaChi on Dec 11, '09 08:56:11AM
I have a "Quit Finder" menu item, just like any other application, that I added to the end of the Finder menu using the following defaults command:
defaults write com.apple.finder QuitMenuItem 1 ; killall Finder
or you can use a basic AppleScript:
tell application "Finder" to quit
You should consider yourself extremely lucky that you don't run into more bugs in the Finder. I have 5 open bug report tickets with Apple at the moment regarding Finder bugs alone! Like I said before, the Finder is decent but has a long way to go still!

[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: tedw on Dec 11, '09 12:07:40PM

interesting. again, out of curiosity, what are the Finder bugs you're seeing? I'd like to see if I can replicate them.



[ Reply to This | # ]
Uninstall an AppleScript application from within Itself
Authored by: trosberg on Dec 15, '09 11:28:25AM
From user point of view, Finder has some bugs, but every version has been better. In Mac OS X 10.4 Finder there is the horrible lack of refresh, when some other app added something to a folder. The Finder can even be AppleScripted to refresh, but it didn't work. From scripter's point of view, the Finder has a learning curve, but it is more understandable and reliable than e.g. System Events scripting. Thanks to tedw for nice Finder scripting. The same:

on applicide()
	set myAppFile to path to me as string
	tell application "System Events"  
		delete disk item myAppFile
	end tell
end applicide
It is valid System Events scripting. It doesn't work in spite of file scripting possibilities with System Events. When saved as an app and then run from AppleScript Editor in 10.6 it shows buggy behavior. Besides, term "me" has changed meaning a little between Mac OS X 10.4 and 10.6. In the latter it means the script app, in the older the Script Editor, when run from the Script Editor. Have your backup ready if trying! The example shows one of AppleScript's flaws, the varying terminology in same task between apps.

[ Reply to This | # ]