11

In an applescript, I receive one filepath that I've to open.

The filepath is in the format "/Users/xxx/my/file/to/open.xyz".

I want to open it with the default program. If it's an AVI, I need to open it with a video program, if it's an xls, with excel, ...

I tried several things without any success:

--dstfile contains the previous path
tell application "Finder"
    activate
    open document dstfile
end tell

-->I'm getting the error 1728, telling me that he wasn't able to get the document

tell application "Finder"
    activate
    open document file dstfile
end tell

--> Same here

tell application "Finder"
    activate
    open document POSIX file dstfile
end tell

--> Same here

I'm sure that the file exists because I do this before this code execution:

if not (exists dstfile) then
    display dialog "File isn't existing"
end if

I cannot use the synthax open.xyz of to of... because I receive this as a parameter.

Please help I'm desperate :'(

Answer: Based on answers, I end up with this:

set command to "open " & quoted form of dsturl
do shell script command
1
  • I strongly recommend you avoid this solution. It is brittle, slower, and completely unnecessary. Use @kopischke’s answer: stackoverflow.com/a/10132641/754997 Commented Jun 20, 2013 at 1:18

2 Answers 2

20

Your problem here is twofold:

  1. your path is in POSIX notation, which AppleScript cannot coerce to an alias or file object acceptable to the Finder, as these will only be implicitly created from path strings in HFS notation (Users:xxx:my:file:to:open.xyz). Expressly declaring your path as POSIX file will solve this. However,
  2. your call to Finder prefixes document to the path, but Finder’s AppleScript dictionary does not contain a document object type (there is a document file object, but it is a child of finder item which cannot be created in this call). Removing that part will solve the issue.

TL;DR: the following line will open a file given through a POSIX path in the default program without recourse to the shell:

tell application "Finder" to open POSIX file "/Users/xxx/my/file/to/open.xyz"

Caveat: this is the simplest solution, but it will only work for qualified POSIX paths (i.e. those beginning with /), like the one in the question. Handling relative ones (i.e. paths starting with ~, . or ..) OTOH needs either the AppleScript-ObjectiveC API (not exactly trivial) or the shell (have fun with your quoting).

Sign up to request clarification or add additional context in comments.

4 Comments

Another way to put it is that you don’t tell applications to open documents, you tell them to open files. “document” and “window” only exist in memory, and are created as a result of opening a file.
@kopischke, how do you use open POSIX file relative to ~?
You don’t, AppleScript POSIX paths only work as qualified paths, as the HFS path coercion is pretty naive (i.e. it converts ~/my/path to :~:my:path). If you need relative POSIX paths, you can either delve into the AppleScript-ObjectiveC API, which is out of the scope of a comment, or shell out, which has been described in other answers. As OP’s question specified a fully qualified path, my answer stands, but I will add a disclaimer.
This answer shows a method that determines the home dir path (~) generically: stackoverflow.com/a/24152885/43615
3

Try:

set dstfile to "~/xxx/my/file/to/open.xyz"
do shell script "open " & dstfile & ""

5 Comments

Down-voted: I strongly recommend you avoid this solution. It is brittle, slower, and completely unnecessary. Use @kopischke’s answer: stackoverflow.com/a/10132641/754997
Up-voted: @ChrisPage I don't think one should downvote a solution that actually works, and do shell script is certainly a useful command for all sorts of tricky tasks.
Up-voted: I needed to open a file in the default app and had relative path like ~/Desktop/test-screenshots.tgz. The accepted answer wouldn't work — as noted in their caveat. This solution does. It is indeed, a good answer even if it's not THE answer.
It's brittle because it's missing proper quoting - if the path contains spaces or other special shell chars, it won't work.
This answer shows a cleaner method: stackoverflow.com/a/24152885/43615

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.