Created
September 11, 2025 14:50
-
-
Save johnlindquist/308f9724fa40a33fdb60f18971bef99e to your computer and use it in GitHub Desktop.
AppleScript to open URLs in browser tabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- open-tab.scpt | |
| -- AppleScript to always open a URL in a new tab in a browser | |
| -- Usage: osascript open-tab.scpt <url> [browser] | |
| on run argv | |
| if (count of argv) < 1 then | |
| error "Usage: osascript open-tab.scpt <url> [browser]" | |
| end if | |
| set targetURL to item 1 of argv | |
| set browserName to "Google Chrome" -- Default browser | |
| if (count of argv) > 1 then | |
| set browserName to item 2 of argv | |
| end if | |
| -- Make sure the URL has a scheme | |
| if targetURL does not start with "http://" and targetURL does not start with "https://" then | |
| -- Prepend https:// as a default scheme | |
| set targetURL to "https://" & targetURL | |
| end if | |
| try | |
| tell application browserName | |
| activate | |
| open location targetURL | |
| end tell | |
| -- Ensure the browser is frontmost after opening | |
| delay 0.2 -- Small delay to allow activation | |
| tell application "System Events" | |
| tell process browserName | |
| set frontmost to true | |
| end tell | |
| end tell | |
| return "Opened URL in " & browserName & ": " & targetURL | |
| on error errMsg number errNum | |
| return "Error opening URL in " & browserName & ": " & errMsg & " (Error " & errNum & ")" | |
| end try | |
| end run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment