How do I redirect file:// URLs to a specific browser via Applescript?

435 views Asked by At

I have this really great little applescript (below) that allows me to open links in their default Fluid App. However, it breaks down for local URLS: file:///

on open location this_URL

    if this_URL contains "drive.google.com" then
        tell application "/Applications/Fluids/Google Drive.app"
            activate
            open location this_URL
        end tell
    else if this_URL contains "mail.google.com" then
        tell application "/Applications/Fluids/Gmail.app"
            activate
            open location this_URL
        end tell
    else
        -- default browser here
        tell application "/Applications/Google Chrome.app"
            activate
            open location this_URL
        end tell

    end if

end open location

The big drawback here is that Dropbox's contextual menu sharing links rely on these local files, so the functionality is broken.

How can I update this to also redirect local URLS as well? I've scoured the internet and can't seem to figure it out.

Edit: more info

The issue presents with any URL that is a "file:///". It appears that "this_URL" does not include local URL's since otherwise according to the final "else" statement in the script, Google Chrome should open it. What happens is the default_browser script opens, and then shuts, and repeats - it opens / shuts again. Nothing else happens. Dropbox and other applications use file:/// URLs to trigger contextual menu features. I don't need anything special to happen with these - I just want them to open with Google Chrome like any other URL.

Note: I have tried code like below, and it doesn't work.

if this_URL contains "file:///" then ...
1

There are 1 answers

0
jweaks On

I understand you to say only the default browser section fails with a file:/// URI. If so, you just need to be sure that they are being sent to the browser in the correct format. The file structure format should look something like:

file:///Users/MyHome/Desktop/My%20File.txt

Put a display dialog in to check this_URL and make sure the file structure is in the right format. You can trap for the file:/// URI's inside the default browser section to do some formatting if needed, including encoding the necessary entities. I'm guessing that entities are tripping you up, since you're not getting an error but a blank page. You need to remove things like spaces.

else
    -- default browser here
    if this_URL contains "file:///" then set this_URL to formatURL(this_URL)
    tell application "/Applications/Google Chrome.app"
        activate
        open location this_URL
    end tell
end if

-- at end of script add this routine:
on formatURL(s)
    return (do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of s)
end