Choose from list apple script service execution from Safari opens Firefox to display the dialog with list
Firefox is likely opening because the script includes:
tell application "firefox"
…
end tell
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 macOS
You 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:
try
set myURL to "https://duckduckgo.com/?q=" & "42"
set bundleID to "com.apple.safari"
do shell script "/usr/bin/open -b " & quoted form of bundleID & " " & quoted form of myURL
on error (e)
display dialog e
end try
Calling open
without a bundle ID will open the URL in the default browser.
Open URL with Safari
/usr/bin/open -b com.apple.Safari 'https://dssw.co.uk'
Open URL with Chrome
/usr/bin/open -b com.google.chrome 'https://miln.eu/apps'
Open URL with Firefox
/usr/bin/open -b org.mozilla.firefox 'https://indie.miln.eu'
|
and Error Checking
You could surround the application names with |
to defer script error checking from compile time to runtime. See user3439894's answer for this approach.
If you want to stop Firefox from opening when not necessary, then change:
else if theActiveApp is "firefox" then
tell application "firefox"
open location urlToOpen
end tell
To:
else if theActiveApp is "firefox" then
set |Firefox| to "Firefox"
tell application |Firefox|
open location urlToOpen
end tell
Firefox does not have an AppleScript dictionary file (.sdef) and sans some of the basic commands is not really considered AppleScript scriptable.
Note that while the trick used in this answer works for Firefox in this particular use case, it may not work for other applications that contain an AppleScript dictionary file and calling commands specific to that given application. In such cases the tell
block must contain the actual name of the application or its bundle id.