Constructing POSIX File with Variables in AppleScript

applescript

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.

Best Answer

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