Skip to main content

Any AppleScript people out there that can help me figure out why the "open location" part of the script is failing to open to the URL?

This works:
osascript -e "tell application \\"$browser\\" to activate"

This does not work:
osascript -e "tell application \\"$browser\\" to activate open location "https://www.yahoo.com""

 

 

loggedInUser=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name 😕 && ! /loginwindow/ {print $3}')

# identify default browser
defaultBrowser=$(plutil -p /Users/$loggedInUser/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist | grep 'https' -b3 |awk 'NR==3 {split($4, arr, "\\""); print arr[2]}')

if [ "$defaultBrowser" = "com.google.chrome" ]; then
browser="Google Chrome"
elif [ "$defaultBrowser" = "com.apple.safari" ]; then
browser="Safari"
elif [ "$defaultBrowser" = "org.mozilla.firefox" ]; then
browser="Firefox"
elif [ "$defaultBrowser" = "com.microsoft.edgemac" ]; then
browser="Microsoft Edge"
fi

echo "Script result: Browser = $browser"

osascript -e "tell application \\"$browser\\" to activate open location "https://www.yahoo.com""

 

 

 

 

not sure what you need to do.. but.. in terminal.. 

open "https://www.yahoo.com"

will open the site in the default browser..


yeah

open -a $browser "https://www.yahoo.com"

does work, but curious how to get it working in AppleScript.


If you're ok with the site opening in the user's default browser, you should only need to do something like this.

osascript -e 'tell application "Finder" to open location "https://www.yahoo.com"'

But if you need it to open in a specific browser, then I think you're missing an -e in the second command.

osascript -e 'tell application "$browser" to activate' -e 'open location "https://www.yahoo.com"'

Unless you use a HEREDOC style statement to contain an Applescript within the blocks, you have to use -e in front of each separate command.


I was missing the "-e" before the second command

and as usual @mm2270 comes to the rescue.  much appreciated!


Reply