Skip to content

Instantly share code, notes, and snippets.

@jellea
Last active June 2, 2020 14:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jellea/95fbff7e8fd820b78f18250d57ace1b9 to your computer and use it in GitHub Desktop.
Save jellea/95fbff7e8fd820b78f18250d57ace1b9 to your computer and use it in GitHub Desktop.
Say, a colleague passes you a Figma link, you click it and surprisingly the browser opens, while you have the desktop app installed! You *sigh* and install this script script which adds a question if you want to open web or desktop.

Install

  1. Install Hammerspoon
  2. Install SpoonInstall (unzip and click to install)
  3. Move the contents of appOrWebDialog.lua (question dialog version) or alwaysApp.lua (default to app version) to ~/.hammerspoon/init.lua
  4. Reload Hammerspoon configuration if you already started it.
  5. Set Hammerspoon as default web browser in macOS preferences -> General

Add more apps

  1. Figure out the App Bundle Identifier. Right click "Show Package Contents" on Application and open Contents/Info.plist inside. Find the Bundle Identifier in the plist file.
  2. Add a new line to url_patterns: { "https?://www.website.com", nil, hs.fnutils.partial(webOrDesktop, "Bundle Identifier here", "App name here") },
  3. Reload Hammerspoon configuration
defaultBrowser = "org.mozilla.Firefox"
--defaultBrowser = "com.google.Chrome"
--defaultBrowser = "com.apple.Safari"
hs.loadSpoon("SpoonInstall")
Install=spoon.SpoonInstall
Install:andUse("URLDispatcher",
{
config = {
url_patterns = {
{ "https?://www.figma.com", "com.figma.Desktop" },
{ "https?://www.notion.so", "notion.id" },
},
default_handler = defaultBrowser
},
start = true
}
)
defaultBrowser = "org.mozilla.Firefox"
--Remove hypens below to set other browser as a default
--defaultBrowser = "com.google.Chrome"
--defaultBrowser = "com.apple.Safari"
defaultBrowserName = string.match(defaultBrowser, '%a+$')
hs.loadSpoon("SpoonInstall")
Install=spoon.SpoonInstall
function openUrl(appBundleID,url,choice)
if choice==defaultBrowserName then
appBundleID = defaultBrowser
-- Workaround: Notion share links can contain the company name, which the desktop app can't open.
elseif string.find(url, "notion.so/") then
url = string.gsub(url, "/pitch", "")
end
hs.application.launchOrFocusByBundleID(appBundleID)
hs.urlevent.openURLWithBundle(url, appBundleID)
end
function webOrDesktop(appBundleID,appName,url)
openFn = hs.fnutils.partial(openUrl, appBundleID, url)
mousePos = hs.mouse.getAbsolutePosition()
hs.dialog.alert(mousePos.x,mousePos.y,openFn,"How to open this "..appName.." link?", "", appName.." App", defaultBrowserName)
end
Install:andUse("URLDispatcher",
{
config = {
url_patterns = {
{ "https?://www.figma.com", nil, hs.fnutils.partial(webOrDesktop, "com.figma.Desktop", "Figma") },
{ "https?://www.notion.so", nil, hs.fnutils.partial(webOrDesktop, "notion.id", "Notion")},
},
default_handler = defaultBrowser
},
start = true
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment