I hope I am asking the question in the right way:
I am writing a script that will be saved as a script application so that I can attach a shortcut keys to it either by BTT or through Automator services. One handler within the script is to extract the URL address of the frontmost browser if I am calling the script while I am using the browser.
The problem: since the script itself is already an app, so the frontmost application will always be the script instead of the browser while I am using the browser. BTT offers an option to run the script application in background (so the handler will work) but it seems that automator>quick action doesn’t have such option?
I hope someone can suggest a solution for this?
Thank you very much!
on getWebLink()
-- this line will always return the name of the script application
tell application "System Events" to set theApp to name of first application process whose frontmost is true
if "Safari" is in theApp then
tell application "Safari"
set theWebURL to URL of front document
set theWebName to name of front document
return "[Source: " & theWebName & "](" & theWebURL & ")"
end tell
else if "Chrome" is in theApp then
tell application "Google Chrome"
set theWebURL to URL of active tab of front window
set theWebName to title of active tab of front window
return "[Source: " & theWebName & "](" & theWebURL & ")" & return & return
end tell
else
return ""
end if
end getWebLink
The script app is not calling from anywhere but a standalone script attached to a shortcut key. The handler attempts to do something like “if the active window is a browser then get the URL address of the site else do not do it”. I think this is a bad design and I probably will change the function spec of the script.
A further search shows that the code in post#10 of this MacScripter link will probably do the trick. Thanks again.
Based on the above-mentioned post, I simplify the code to:
tell application "System Events"
tell (first application process whose frontmost is true)
set visible of it to false
set activeApp to name of it
end tell
end tell
activate me
When “me” (the original “it”) is not visible, the “it” change from “me” to the previous active app. The name can now be used to check whether the active window is a browser or not. This block works in SD, I will check if it also works for my script.
This is the final form of the handler. I had problem with this final form initially: the handler ran perfectly in SD but didn’t work when the script was ran as an application. Then I found out that the magic is to add a delay!
on getWebLink()
tell application "System Events"
tell (first application process whose frontmost is true)
-- the "it" is this script
set visible of it to false
delay 0.2
-- now, the "it" becomes the previous frontmost app
set theActiveApp to name of it
end tell
end tell
activate me
if theActiveApp is "Safari" then
tell application "Safari"
set theWebURL to URL of front document
set theWebName to name of front document
return "[Source: " & theWebName & "](" & theWebURL & ")"
end tell
else if theActiveApp is "Google Chrome" then
tell application "Google Chrome"
set theWebURL to URL of active tab of front window
set theWebName to title of active tab of front window
return "[Source: " & theWebName & "](" & theWebURL & ")"
end tell
else
return ""
end if
end getWebLink