Search code examples
applescript

AppleScript - What's wrong with my quotes?


Hello and thanks for your help,

This is driving me crazy! I keep having syntax errors for simple stuff like just opening a file.

I get > Syntax Error - Expected end of line, etc. but found “"”.

What am I doing wrong? It's the same quotes as the ones for Finder...

tell application "Finder"
    set theFile to selection
    open theFile with application "QuickTime Player"
end tell

Solution

  • Here is the corrected script[1]:

    tell application "Finder"
      set theFile to selection
      open theFile using application file "QuickTime Player" of folder "Applications" of startup disk
    end tell
    

    This can be simplified by specifying the application by bundle ID instead of location, in which case Finder will find it for you:

      open theFile using application file id "com.apple.QuickTimePlayerX"
    

    Or, if you want to do stuff with the file(s) once they’re opened:

    tell application "Finder"
      set theFiles to selection as alias list
    end tell
    if theFiles is {} then error "No files selected."
    
    tell application "QuickTime Player"
      activate -- (ensures any "can't open file" error dialogs are visible)
      open theFiles
      -- do other stuff here
    end tell
    

    --

    [1] The syntax error is due to the special behavior of with/without parameter labels, which expect to be followed by a single keyword (or comma-separated keywords). AppleScript reads the command as open theFile using application—and since a command can’t be immediately followed by an expression, in this case a literal string, it reports a (frustratingly unhelpful) syntax error.