Yu Coleman
Yu Coleman

Reputation: 57

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")

Upvotes: 1

Views: 115

Answers (1)

Yu Coleman
Yu Coleman

Reputation: 57

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

Upvotes: 0

Related Questions