Skip to content

Instantly share code, notes, and snippets.

@aarblaster
Created October 22, 2023 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarblaster/eaac1bd10f1787d6bc4d484c10db95b2 to your computer and use it in GitHub Desktop.
Save aarblaster/eaac1bd10f1787d6bc4d484c10db95b2 to your computer and use it in GitHub Desktop.
Open URLs in a New Given Safari Profile
-- Anthony Arblaster 22/10/2023
-- web: https://codebyanthony.com
-- mastodon: https://mastodonapp.uk/@aarblaster
-- An AppleScript to open URLs input as text in a Safari Profile.
-- Requires [Keyboard Maestro](https://www.keyboardmaestro.com/main/)
-- Compiled from a whole bunch of different places in bits and pieces.
-- Particular thanks go to the [KM wiki](https://wiki.keyboardmaestro.com/AppleScript?s[]=applescript&s[]=variable)
-- **Notes**
-- I couldn't get the AppleScript to easily open a menu item in Safari so used a Keyboard Maestro Macro to do it.
-- I set the KM variable `ScriptVar` using an AppleScript in Bunch but you could do it in a variety of ways.
-- The KM Variable can be any string of URLs with delimiters including ` ` or `,`.
-- Open the Safari profile with KM
tell application "Keyboard Maestro Engine" to do script "SelectSafariProfile"
-- Get the value of a KeyboardMaestro variable.
-- Make it available to the AppleScript.
-- set the name of the KM variable to get.
set myKMVar to "ScriptVar"
-- Get the text string of URLs from KM.
tell application "Keyboard Maestro Engine"
set inputString to getvariable myKMVar
end tell
-- Regular expression pattern to match URLs
set urlPattern to "https?://\\S+"
-- Create a list to store the extracted URLs
set extractedURLs to {}
-- Use the "do shell script" command with "grep" and regular expression to extract URLs
try
set extractedURLs to paragraphs of (do shell script "echo " & quoted form of inputString & " | grep -E -o " & quoted form of urlPattern)
on error errMsg
display dialog "An error occurred: " & errMsg
end try
-- Iterate through the extracted urls and open in tabs.
tell application "Safari"
-- Get a reference to the front window
set frontWindow to the front window
repeat with aURL in extractedURLs
-- Create a new tab with the URL
make new tab at frontWindow with properties {URL:aURL}
end repeat
end tell
@aarblaster
Copy link
Author

Usage of the Script

I use this script to help me set up my coding context with Bunch. I normally run my Mac in a Focus Mode that specifies the Personal Profile in Safari. When I'm coding, I want all the other things in that focus mode, but I want to keep a bunch of tabs for coding separate.

Requirements

As it stands, it relies on Keyboard Maestro to open the Profile from the menu and also to set the text URLs dynamically. You could, of course, swap that out!

A Little Note

This took me a little while to work out, so I thought I would share in case anyone else finds it or parts of it useful. I don't know AppleScript very well, so there may be better ways of doing all this.

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