Search code examples
applescript

Applescript: How to open a file with file path stored in clipboard


I have read these two posts Copy pure text from clipboard using AppleScript and Applescript: How to open a file with the default program?.

The following code works. I can open file "file.pdf".

tell application "Finder"
    open POSIX file "/Users/myName/file.pdf"
end tell

And so I come up with the following code.

tell application "Finder"
    set filePath to (the clipboard as text)
    open POSIX file filePath
end tell

But surprisingly it does not work. It returns an error (Finder got an error: Can’t get POSIX file "/Users/myName/file.pdf")


Solution

  • As red_menace said, open filePath as POSIX file works. The following code works.

    tell application "Finder"
        set filePath to (the clipboard as text)
        open filePath as POSIX file
    end tell