Having script delete itself without giving error

I have a script that I want to delete when it’s done running. I’ve tried using a shell script but we’re using networked home directories so that adds a new level of complication. Right now what I’m using is this.

set f to path to me

tell application “Finder”
move item f to trash
end tell

Which works but I get an error “Could not save changes to the script “hardware.app” because of a disk error.”

So is there any way of telling it to move to the trash without error or something similar? Thanks any info.


it works for me. are you sure you are running it as an app? try making a new script, just copy/pasting the code, save as app, quit script editor, then open the app. that worked for me.

It works when I log in local. It’s a network home problem. :? Thanks for checking for me. I didn’t think to check locally.

Perhaps you don’t have write permissions for your script? What if you try this?

local f --> this instructs AppleScript to NOT store changes in the script -- do not write to it

set f to (path to me)

tell application "Finder" to delete f

Didn’t work, it gives me an error that it can’t delete it because it’s open. I also tried using move to trash but it gives me the same error as before.

You can try this:

--> write shell script-applescript command
set f to (path to me)
set cmd to ("osascript -e 'tell application "Finder" to delete alias "" & f as text) & ""'"
set tmpFile to "/tmp/delme" as POSIX file
set fRef to (open for access tmpFile with write permission)
set eof of fRef to 0
write cmd to fRef
close access fRef

--> chmod and execute file. While it gets executed, we'll quit
do shell script "cd /tmp; chmod 755 delme; ./delme  > /dev/null 2>&1 &"