Applescript: How to open a file with the default program?
applescript, scripting
Solution
Your problem here is twofold:
- 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,
- 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).
Problem
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 ```