Im trying to use following Apple Script to display a choose from list and perform certain actions based on the choice of selection:
on run {input, parameters}
set selT to "42"
set selectedOption to choose from list {"Safari", "Chrome", "Firefox"} with prompt "Search with:" without multiple selections allowed and empty selection allowed
global urlToOpen
set urlToOpen to "na"
if selectedOption is not false and length of selectedOption is not 0 then
set urlToOpen to "https://duckduckgo.com/?q=" & selT
end if
if ((urlToOpen as string) is not equal to "na") then
tell application "System Events"
tell (first application process whose frontmost is true)
set theActiveApp to name of it
end tell
end tell
activate me
display dialog theActiveApp
if theActiveApp is "Safari" then
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:urlToOpen})
end tell
end tell
else if theActiveApp is "firefox" then
tell application "firefox"
open location urlToOpen
end tell
else if theActiveApp is "Google Chrome" then
tell application "Google Chrome"
open location urlToOpen
end tell
end if
end if
end run
But, whats happening is; when the Automator service runs, it opens Firefox to display the list of options. It would be a great help to understand this behavior, because Firefox not being the default browser and it is waste of CPU resource to open it.
Best Answer
Firefox is likely opening because the script includes:
Based on your description, these lines require AppleScript to launch Firefox to determine if the script is correct – even if the involved lines are never run. Firefox needs to be launched for AppleScript to obtain the application's scripting language support.
The other browsers implement AppleScript using a more modern approach. They provide their language support through an embedded Scripting Definition file (
SDEF
) within the application bundle.open
a URL on macOSYou can avoid this behaviour by using the macOS
open
command line tool and passing the desired browser's bundle identifier with the URL to open.This removes the need to write browser specific sections of AppleScript.
Be sure to quote the arguments:
Calling
open
without a bundle ID will open the URL in the default browser.Open URL with Safari
Open URL with Chrome
Open URL with Firefox
|
and Error CheckingYou could surround the application names with
|
to defer script error checking from compile time to runtime. See user3439894's answer for this approach.