Skip to main content
Solved

AppleScript Question - Open Browser to Website

  • September 14, 2023
  • 4 replies
  • 63 views

stutz
Forum|alt.badge.img+5

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""

 

 

 

 

Best answer by mm2270

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.

4 replies

jamf-42
Forum|alt.badge.img+17
  • Esteemed Contributor
  • September 14, 2023

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

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

will open the site in the default browser..


stutz
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 14, 2023

yeah

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

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


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • Answer
  • September 14, 2023

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.


stutz
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 15, 2023

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

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