Nayan Seth
Nayan Seth

Reputation: 83

How to move a file to trash in AppleScript?

I know its a dumb question but somehow the command i type is not working

set deleteFile to srcTemp & "Archive.zip"
--deleteFile path is something like this /Users/home/Desktop/Archive.zip    
tell application "Finder"
    move POSIX file "" & deleteFile & "" to trash
    --move file "\"" & destNoQuote & "Archive.zip\"" to trash
    empty the trash
end tell

But I get an error saying can't find the POSIX file.

Upvotes: 1

Views: 6315

Answers (2)

double_j
double_j

Reputation: 1706

Here's another method of your entire script in one line:

do shell script "rm -Rf " & quoted form of (srcTemp & "Archive.zip")

This will force remove your file and it doesn't just go to your trash, it's gone.

Upvotes: -4

regulus6633
regulus6633

Reputation: 19032

I know that many people use the posix file command inside the Finder tell block of code however that's a mistake. The posix file command is not a Finder command, it's an applescript command, and therefore should not be in the Finder block if possible. This is true for all commands actually. You should only tell an application to perform the commands you can find inside of its applescript dictionary otherwise you will see unexpected behavior... as you are finding.

As such this is how you should write your code...

set deleteFile to srcTemp & "Archive.zip"
set posixFile to POSIX file deleteFile
--deleteFile path is something like this /Users/home/Desktop/Archive.zip    
tell application "Finder"
    move posixFile to trash
    empty the trash
end tell

Upvotes: 10

Related Questions