AppleScript Question - Open Browser to Website

stutz
Contributor

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

 

 

 

 

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

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.

View solution in original post

4 REPLIES 4

jamf-42
Valued Contributor II

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

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

will open the site in the default browser..

stutz
Contributor

yeah

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

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

mm2270
Legendary Contributor III

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
Contributor

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

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