Help streamlining an existing script

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).

Hi,

You should use block using terms from one of 3 additional browsers, as they have common terms:


tell application "System Events"
	set frontApp to name of first process whose frontmost is true
	if frontApp is in {"Script Editor", "Script Debugger"} then set visible of process frontApp to false
	set frontApp to name of first process whose frontmost is true
end tell

if frontApp is "Safari" then
	tell application "Safari" to display dialog "Safari is already at the front" giving up after 3
else if frontApp is in {"Brave Browser", "Google Chrome", "Microsoft Edge"} then
	using terms from application "Google Chrome" -- THIS
		tell application frontApp to set theURL to URL of active tab of first window
	end using terms from -- THIS
	tell application "Safari"
		open location theURL
		activate
	end tell
end if

Thats perfect, thanks a lot!

And this helps me understand how to write scripts to do similar things :slight_smile: