Applescript question

I am trying to get the URL of the current browser window I want to support Safari, Brave, and Google Chrome. I have the following code:

tell application "System Events"
	set activeApp to name of first application process whose frontmost is true
	display dialog activeApp
	if activeApp is in "Brave Browser" or activeApp is in "Google Chrome" or activeApp is in "Safari" then
		tell application activeApp to activate
		delay 1
		
		tell application "System Events"
			-- shift to Addresss bar
			key code 37 using {command down}
			-- Copy URL 
			key code 8 using {command down}
			-- Escape
			key code 53
		end tell
	else
		display dialog ("Sorry, not a supported browser.")
	end if
end tell


which works if I hard code the line with

tell application activeApp to activate

to have the name of the particular browser like so

tell application "Google Chrome" to activate


Is there a better way to do this?

Mac mini, macOS 10.15

Posted on Nov 7, 2019 1:09 PM

Reply

Similar questions

5 replies

Nov 7, 2019 1:24 PM in response to michaelbierman

I realized this seems to solve the problem but it seems clunky. Is there a better way?


tell application "System Events"
	set activeApp to name of first application process whose frontmost is true
	
	if activeApp is in "Brave Browser" or activeApp is in "Google Chrome" or activeApp is in "Safari" then
		display dialog activeApp
		
		if activeApp is in "Brave Browser" then
			tell application "Brave Browser" to activate
		end if
		if activeApp is in "Google Chrome" then
			tell application "Google Chrome" to activate
		end if
		if activeApp is in "Safari" then
			tell application "Safari" to activate
		end if

		delay 1
		
		tell application "System Events"
			-- shift to Addresss bar
			key code 37 using {command down}
			-- Copy URL 
			key code 8 using {command down}
			-- Escape
			key code 53
		end tell
	else
		display dialog ("Sorry, not a supported browser.")
	end if
end tell

Nov 8, 2019 3:39 AM in response to michaelbierman

I have not tested this with the commented System Events line, but it does work where I have purposely set activeApp to "Safari" and Safari is running in the background. Eliminates the large if block.


use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property NSArray : a reference to current application's NSArray

set B to {"Brave Browser", "Safari", "Google Chrome"} as list
set activeApp to "Safari"

-- tell application "System Events" to set activeApp to name of first application process whose frontmost is true

if B contains activeApp then
	set Ba to NSArray's arrayWithArray:B
	-- because NSArray is zero based, and AppleScript is 1 based
	set ndx to ((Ba's indexOfObject:activeApp) + 1) as integer
	tell application (item ndx of B) to activate
else
	display dialog ("Sorry, not a supported browser")
end if

tell application "System Events"
	tell application process activeApp
		key code 37 using {command down}
		-- Copy URL 
		key code 8 using {command down}
		-- Escape
		key code 53
	end tell
	if ((the clipboard) as text) begins with "http" then
		display dialog (the clipboard) as text
	end if
end tell
return



Nov 9, 2019 4:45 AM in response to michaelbierman

Using list lookups, and indices is more efficient than multiple if branches. However, I had not considered the valid situation where you may have an active browser with multiple windows. I don't have the Brave Browser, but unless they have provided rich AppleScript support (and it would be different for each browser) you may not have the means to control the behavior of n-tuple browser windows when you run this script.

Nov 11, 2019 9:08 AM in response to michaelbierman

I confess I'm a little confused about your script.


Your concern stems around being able to activate a variable-based application. Why do you need to do this?


In your script you specifically:


set activeApp to name of first application process whose frontmost is true


Therefore, the app in question is *already* activated, so there's no need to re-activate it, no?


Secondly, I would err on the side of direct manipulation rather than UI events to control the apps. Since you can identify the app that's frontmost, I'd just query it directly for the URL:


set theURL to ""


tell application "System Events"

set activeApp to name of first application process whose frontmost is true

end tell


if activeApp = "Safari" then

tell application "Safari" to set theURL to URL of current tab of window 1

else if activeApp = "Google Chrome" then

tell application "Google Chrome" to set theURL to URL of active tab of window 1

else if activeApp is "Brave Browser" then

-- I have no idea about Brave Browser's AppleScript support, so use UI hacking, but could be improved

tell application "System Events"

-- shift to Addresss bar

key code 37 using {command down}

-- Copy URL 

key code 8 using {command down}

-- Escape

key code 53

end tell

else

display dialog ("Sorry, not a supported browser.")

end if


if theURL ≠ "" then display dialog theURL



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript question

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.