I cobbled together this script to grab the current URL in any browser I’m using other than Safari, then open it in Safari.
tell application "System Events"
set frontApp to displayed name of first process whose frontmost is true
set scriptName to displayed name of (path to me)
if frontApp is scriptName then
set visible of process scriptName to false
set frontApp to displayed name of first process whose frontmost is true
end if
end tell
if frontApp is "Brave Browser" then
tell application "Brave Browser"
set theURL to URL of active tab of first window
end tell
else
if frontApp is "Google Chrome" then
tell application "Google Chrome"
set theURL to URL of active tab of first window
end tell
else
if frontApp is "Microsoft Edge" then
tell application "Microsoft Edge"
set theURL to URL of active tab of first window
end tell
else
if frontApp is "Safari" then
display dialog "Safari is already at the front" giving up after 3
end if
end if
end if
end if
tell application "Safari"
open location theURL
activate
end tell
This works, but I thought (hoped) as all of the browsers (other than Safari) use the same AppleScript it would be possible to use a variable for the app name (Chrome, Brave, or Edge) and not repeat the script for each individual app.
I tried this:
tell application "System Events"
set frontApp to displayed name of first process whose frontmost is true
set scriptName to displayed name of (path to me)
if frontApp is scriptName then
set visible of process scriptName to false
set frontApp to displayed name of first process whose frontmost is true
end if
end tell
if frontApp is "Safari" then
display dialog "Safari is already at the front" giving up after 3
else
tell application frontApp
set theURL to URL of active tab of first window
end tell
end if
tell application "Safari"
open location theURL
activate
end tell
This doesn’t work as it throws an error when trying to compile it (the ‘set theURL to URL of active tab of first window’ part).