Skip to content

Instantly share code, notes, and snippets.

@shotasenga
Last active July 31, 2022 06:58
Show Gist options
  • Save shotasenga/663c52593a43f97db9ae177ccf47452e to your computer and use it in GitHub Desktop.
Save shotasenga/663c52593a43f97db9ae177ccf47452e to your computer and use it in GitHub Desktop.
Custom URL scheme handler with AppleScript
  1. Create new AppleScript with ScriptEditor.app
  2. Copy & Paste open.applescript into the script you create
  3. Save the script as an application
  4. Open Info.plist which in the application you create (you can find it under Open.app/Contents directory)
  5. Add thoes keys to specify the URL scheme
<key>CFBundleIdentifier</key>
<string>me.senta.AppleScript.Open</string>
<key>CFBundleURLTypes</key>
<array>
	<dict>
  	<key>CFBundleURLName</key>
		<string>Open</string>
		<key>CFBundleURLSchemes</key>
		<array>
			<string>open</string>
		</array>
	</dict>
</array>
<?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>
<key>CFBundleAllowMixedLocalizations</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>applet</string>
<key>CFBundleIconFile</key>
<string>applet</string>
<key>CFBundleIdentifier</key>
<string>me.senta.AppleScript.Open</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Open</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>aplt</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>Open</string>
<key>CFBundleURLSchemes</key>
<array>
<string>open</string>
</array>
</dict>
</array>
<key>LSMinimumSystemVersionByArchitecture</key>
<dict>
<key>x86_64</key>
<string>10.6</string>
</dict>
<key>LSRequiresCarbon</key>
<true/>
<key>WindowState</key>
<dict>
<key>bundleDividerCollapsed</key>
<true/>
<key>bundlePositionOfDivider</key>
<real>0.0</real>
<key>dividerCollapsed</key>
<false/>
<key>eventLogLevel</key>
<integer>2</integer>
<key>name</key>
<string>ScriptWindowState</string>
<key>positionOfDivider</key>
<real>388</real>
<key>savedFrame</key>
<string>725 122 700 672 0 0 1920 1058 </string>
<key>selectedTab</key>
<string>log</string>
</dict>
</dict>
</plist>
-- Open application by the `open` command wit `open://` url scheme
-- open:///path/to/file
-- You can give an argument to specify application whch open the file with
-- open:///path/to/file?CotEditor
on open location _url
set _url to urlDecode(_url)
set x to the offset of "?" in _url
if x is 0 then
set the _path to text 8 thru -1 of _url
openIn(_path, "")
else
set the _path to text 8 thru (x - 1) of _url
set the _app to text (x + 1) thru (-1) of _url
openIn(_path, _app)
end if
end open location
on urlDecode(_url)
set _script to "php -r 'echo urldecode(" & quote & _url & quote & ");'"
return (do shell script _script) as string
end urlDecode
on openIn(_path, _app)
set _script to "open " & quote & _path & quote
if _app is not "" then
set _script to _script & " -a " & quote & _app & quote
end if
try
do shell script _script
on error _msg
display dialog "Error: " & _msg
end try
end openIn
open location "open:///Users/senta/Desktop/hey.md?CotEdixtor"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment