1

I can construct a file with variables:

set filePath to "Macintosh HD:tmp:test.txt"
tell application "Finder"
    delete file filePath
end tell

However, same thing doesn’t work for POSIX file:

set filePath to "/tmp/test.txt"
tell application "Finder"
    delete POSIX file filePath
end tell

It says “can’t get POSIX file "/tmp/test.txt"” (-1728). The syntax looks correct, because it works when I use string literal instead of variable:

tell application "Finder"
    delete POSIX file "/tmp/test.txt"
end tell

I’m running scripts with osascript foobar.applescript in case that matters.

1

This AppleScript code works for me using the latest version of macOS Big Sur.

set filePath to "/tmp/test.txt"
tell application "Finder"
    delete filePath as POSIX file
end tell

This also works for me…

set filePath to POSIX file "/tmp/test.txt"
tell application "Finder"
    delete filePath
end tell
1
  • Thank you! Constructing the object with as operator indeed works, but do you know why the syntax POSIX file filePath doesn’t work as the parameter to delete command? It even works when I create a variable, like set pFile to POSIX file filePath. – Franklin Yu Feb 26 at 6:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .