use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions -- BrowserHereHelper.applescript: -- v2, by -- Instructions and current version: -- -- ---- ---- ---- -- -- Replace the `getDefaultBrowser()` below with this version that simply returns -- a static string, if you wish to set this script as the macOS "default browser" -- (otherwise, it will invoke itself recursively 😅) (* to getDefaultBrowser() return "Firefox" end getDefaultBrowser *) -- ---- ---- ---- -- -- Modify these two functions to add support for a new browser -- This function should map the package-identifier stored in the LaunchServices -- database to an 'application name' that works in AppleScript to getDefaultBrowser() local output, browserApplication set output to do shell script "defaults read \\ ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure \\ | awk -F'\"' '/http;/{print window[(NR)-1]}{window[NR]=$2}'" if output is "" or output contains "safari" then set browserApplication to "Safari" else if output contains "chrome" then set browserApplication to "Google Chrome" else if output contains "firefox" then set browserApplication to "Firefox" else if output contains "ScriptEditor" then error "BrowserHereHelper cannot recursively invoke itself! if you want to use BrowserHereHelper as your 'default browser', read the instructions inside the script and re-create the app." else error "unknown default browser; modify the script to match against the output you get for the inline shell-script" end if return browserApplication end getDefaultBrowser -- This function should handle whatever is necessary to instruct your browser- -- application to create a new, empty window to makeNewWindow(browserApplication) if browserApplication = "Safari" then tell application browserApplication to make new document else if browserApplication = "Google Chrome" then tell application browserApplication to make new window else if browserApplication = "Firefox" then tell application "System Events" tell process browserApplication to click menu item "New Window" of menu "File" of menu bar 1 end tell end if tell application "System Events" repeat until (my countVisibleWindows(browserApplication) > 0) delay 0.05 end repeat end tell end makeNewWindow -- Modify the above two functions to add support for a new browser -- ---- ---- ---- -- to countVisibleWindows(an_app) tell application "System Events" return count (windows of process an_app where value of attribute "AXMinimized" is false) end tell end countVisibleWindows to ensureVisibleWindowInThisSpace(browserApplication) tell application "System Events" if my countVisibleWindows(browserApplication) = 0 then my makeNewWindow(browserApplication) end if end tell end ensureVisibleWindowInThisSpace on open location the_url local defaultBrowser set defaultBrowser to my getDefaultBrowser() if application defaultBrowser is running then my ensureVisibleWindowInThisSpace(defaultBrowser) end if tell application defaultBrowser open location the_url activate end tell end open location on run local defaultBrowser set defaultBrowser to my getDefaultBrowser() if application defaultBrowser is running then my ensureVisibleWindowInThisSpace(defaultBrowser) end if tell application defaultBrowser to activate end run