views:

1158

answers:

1

For reasons that only the developers can understand, Firefox will create and open .url files on Windows and .webloc files on OS X but won't allow the Windows version of Firefox to open .webloc files or the OS X version of Firefox to open .url files. (.url files open in Safari but that's not good enough for reasons that aren't worth going into here.) As part of my efforts to use either filetype on either system, I'm writing an applescript to open .url files on OS X Firefox.

on open the_droppings
    set filePath to the_droppings

    set fp to open for access filePath
    set fileContents to read fp
    close access fp

    set secondLine to paragraph 2 of fileContents

    set tid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "="
    set URLstring to last text item of secondLine
    set AppleScript's text item delimiters to tid

    tell application "Firefox"
        activate
        OpenURL URLstring
    end tell
end open

I thought this would work but in the 3rd to last line it says "Expected end of line, etc. but found identifier." Why is this?

EDIT sakra's answer below mostly works but breaks on urls containing "=" such as: http://example.com?foo=a&bar=z

+1  A: 

Firefox does not seem to feature an AppleScript dictionary at all. Therefore the term OpenURL within the tell app "Firefox" statement is interpreted as an AppleScript identifier and not as an AppleScript command. Two AppleScript identifiers in a row result in a syntax error.

As a work-around you can use the shell command open in combination with the standard AppleScript command do shell script:

on open the_droppings

    set filePath to the_droppings

    set fileContents to read filePath

    set theOffset to offset of "URL=" in fileContents
    set URLstring to text (theOffset + 4) through -1 of fileContents

    do shell script "/usr/bin/open -a Firefox.app " & quoted form of URLstring
end open
sakra