0

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.

0
2

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.

0
1

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .