Skip to content

Instantly share code, notes, and snippets.

@georgebrock
Last active December 8, 2023 22:12
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save georgebrock/9ab3d83bf160b7c1c2b0 to your computer and use it in GitHub Desktop.
Save georgebrock/9ab3d83bf160b7c1c2b0 to your computer and use it in GitHub Desktop.
AppleScript to handle URLs

Follow these instructions to create an AppleScript App that can be used to open URLs, e.g. from Choosy.

  1. Open Script Editor (in /Applications/Utilities).
  2. Write a script with an on open location handler (see example.applescript).
  3. Save the script as an application (select "Application" from the file format dropdown in the save dialog in Script Editor).
  4. Edit the application's Info.plist file to add a CFBundleURLTypes section, indicating it can handle HTTP URLs (see Info.plist). If the app is at ~/Applications/MyApp.app then the Info.plist will be at ~/Applications/MyApp.app/Contents/Info.plist.
  5. Test your application from the command line using open -a ~/Applications/MyApp.app http://www.example.com
  6. Update the AppleScript to do something useful with the URL (the do shell script command might be useful if you want to avoid writing more AppleScript).
  7. ?
  8. Profit.
on open location this_URL
display alert this_URL
end open location
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- ... -->
<!-- Add this section: -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>HTTP URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>http</string>
<string>https</string>
</array>
</dict>
</array>
<!-- End of what needs adding -->
</dict>
</plist>
@scoates
Copy link

scoates commented Mar 5, 2016

Thanks again! This was really helpful.

I now have an app with this:

on open location this_URL
    set fixed_url to replace_chars(this_URL, ".png?dl=0", ".png?raw=1")
    do shell script "open " & fixed_url
end open location

on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

And this in Choosy:
screenshot 2016-03-05 10 33 53

So I can now open Dropbox screenshots without waiting for their super-inefficient web page to load when all I want is the image.

(Yes, there's a potential expoit waiting in the open + concat, so I'll be careful. (-: )

@imomaliev
Copy link

imomaliev commented Jun 22, 2020

For anyone wanting to open in private window you could achieve it by changing script from example to

on open location this_URL
	tell application "Safari"
		activate
		delay 0.5
		tell application "System Events"
			keystroke "n" using {command down, shift down}
		end tell
		activate
		set URL of document 1 to this_URL
	end tell
end open location

This trick is taken from https://apple.stackexchange.com/questions/222114/is-it-possible-to-open-a-private-browser-window-in-safari-using-terminal-app

Caveats

  • This may need delay adjustment for slower systems
  • This will always open in new private window

TODO

  • check if current safari window is already incognito

@zhangyoufu
Copy link

I didn't find my applet in the list of known Web browsers. brew install defaultbrowser works for me

@imomaliev
Copy link

To create copy to clipboard browser use

on open location this_URL
	set the clipboard to this_URL
	display notification this_URL with title "Copied to Clipboard"
end open location

Note: without notification it will always show dialog "Press run to run this script..."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment