System preferences to change default browser

With Ventura 13.0, System Settings no longer allows an AppleScript command to reveal its general pane.


tell application "System Settings"
        reveal pane id "com.apple.preference.general"

I am interested in a new method of managing System Settings. My overall goal is to toggle System Settings’ default browser. That current property or method in Ventura, is now displayed in the System Settings’s window “Desktop & Dock”. The pane function appears to be no longer viable in AppleScript.
The following command fails


tell application "System Settings"
    reveal pane "Desktop & Dock"
end tell

System Settings’ pane and reveal pane functions remain in the dictionary for the application “System Settings”, but do not appear functional. Perhaps relevant to this issue is that the dictionary’s version for System Settings is listed as 15.0 and not 16.0.

In any case, I am interested in another method to script this application, although I would much prefer a command line or other method to change the System Setting’s default browser.

Use this:


use framework "Foundation"
use framework "AppKit"
use scripting additions

current application's NSWorkspace's sharedWorkspace()'s openURL:(current application's NSURL's URLWithString:"x-apple.systempreferences:com.apple.systempreferences.GeneralSettings")

You can find the paths in the file: /System/Applications/System Settings.app/Contents/Resources/Sidebar.plist

Using db123’s recommendation, I was able to script Ventura 13.0 to reveal its Desktop & Dock window in System Settings, so that I could toggle the default browser value between Safari and Google Chrome.

(Unrelated to this AppleScript problem, but related to this post, I was unable to style the following script using the AppleScript brackets. I have submitted my script, nonetheless.
I have now corrected the styling issue, thanks to Mockman’s kind input on using only lowercase text designating applescript in opening and closing brackets)


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

#	open System Settings window displaying Desktop & Dock settings, using db123's method
current application's NSWorkspace's sharedWorkspace()'s openURL:(current application's NSURL's URLWithString:"x-apple.systempreferences:com.apple.Desktop-Settings.extension")

tell application "System Settings"
	#	delay until System Settings' window displaying Desktop & Dock actually exists
	repeat until exists window "Desktop & Dock"
		delay 0.2
	end repeat
end tell

tell application "System Events"
	tell process "System Settings"
		tell window "Desktop & Dock"
			
			#	Formulate path to "Default web browser" pop up button
			set AHostingViewGroup to first group
			set AXSplitGroup1 to first splitter group of AHostingViewGroup
			set ScrollArea to scroll area 1 of group 1 of group 2 of AXSplitGroup1
			set DefaultWebBrowserPopUpButton to first pop up button of (group 7 of ScrollArea) whose name is "Default web browser"
			
			#	Set up boolean condition to toggle default web browser values between "Safari" and "Google Chrome
			tell DefaultWebBrowserPopUpButton
				if its value is "Google Chrome" then
					set AlternateBrowserValue to "Safari"
				else
					set AlternateBrowserValue to "Google Chrome"
				end if
				
				click #  to activate drop down menu
				repeat with i from 1 to 20
					#	wait for pop up menu to be displayed
					if exists menu 1 then
						set PopUpMenuExists to true
					end if
					exit repeat
					if i is 10 then
						set PopUpMenuExists to false
					end if
					delay 0.2
				end repeat
				if PopUpMenuExists is true then
					tell menu 1
						click (first menu item whose title is AlternateBrowserValue)
					end tell
				end if
			end tell
			
			click (first button whose description is "close button")
		end tell
		
	end tell
end tell

if PopUpMenuExists is false then
	display dialog "Pop Up Menu failed to display" giving up after 2
else
	display dialog AlternateBrowserValue with title "New Default Browser" giving up after 2
end if

As the UI scripting was tedious, I would enjoy learning whether either a command line or AppleScript- Objective C method exists to toggle between default browser values.

The tags should be all lowercase…

[format]‘applescript’, not ‘AppleScript’[/format]

Thank you, Mockman. I have corrected my original posting with all lower case applescript tags!

In Terminal.app, using Homebrew, I installed the command line utility defaultbrowser using the command [format]brew install defaultbrowser[/format]

Once defaultbrowser has been successfully installed, the command… [format]defaultbrowser safari[/format] Will in fact change your default browser in the System Settings to Safari.
The command… [format]defaultbrowser chrome[/format] Will in fact change your default browser in the System Settings to Google Chrome.

To toggle the default browsers back-and-forth between Safari & Chrome, this following AppleScript code should do the trick on macOS Ventura.

set repeatCount to 0

tell application "System Events"
	my changeDefaultBrowser("chrome")
	repeat until button 2 of window 1 of process "CoreServicesUIAgent" exists
		delay 0.01
		set repeatCount to repeatCount + 1
		if repeatCount = 15 then exit repeat
	end repeat
	try
		click button 2 of window 1 of process "CoreServicesUIAgent"
	on error errMsg number errNum
		my changeDefaultBrowser("safari")
		repeat until button 2 of window 1 of process "CoreServicesUIAgent" exists
			delay 0.01
		end repeat
		click button 2 of window 1 of process "CoreServicesUIAgent"
	end try
end tell

to changeDefaultBrowser(thebrowser)
	do shell script "/usr/local/bin/defaultbrowser " & thebrowser
end changeDefaultBrowser

Just make sure the path to your version of defaultbrowser is /usr/local/bin/defaultbrowser, otherwise you will need to edit the do shell script command with the correct path.

This code also clicks the correct button when System Settings.app pops up the window asking to use whichever as the default browser.

This extended version of the above code will display a dialogue with your new chosen default browser.

property currentBrowser : missing value

set repeatCount to 0

tell application "System Events"
	my changeDefaultBrowser("chrome")
	repeat until button 2 of window 1 of process "CoreServicesUIAgent" exists
		delay 0.01
		set repeatCount to repeatCount + 1
		if repeatCount = 15 then exit repeat
	end repeat
	try
		if button 2 of window 1 of process "CoreServicesUIAgent" = "Use \"Safari\"" then
			set currentBrowser to "Safari"
		else
			set currentBrowser to "Chrome"
		end if
		click button 2 of window 1 of process "CoreServicesUIAgent"
	on error errMsg number errNum
		my changeDefaultBrowser("safari")
		repeat until button 2 of window 1 of process "CoreServicesUIAgent" exists
			delay 0.01
		end repeat
		if button 2 of window 1 of process "CoreServicesUIAgent" = "Use \"Safari\"" then
			set currentBrowser to "Chrome"
		else
			set currentBrowser to "Safari"
		end if
		click button 2 of window 1 of process "CoreServicesUIAgent"
	end try
end tell

ignoring application responses
	activate
	display dialog "Your default web browser is now " & ¬
		currentBrowser buttons {"Cancel", "OK"} ¬
		default button "OK" giving up after 2
end ignoring

to changeDefaultBrowser(thebrowser)
	do shell script "/usr/local/bin/defaultbrowser " & thebrowser
end changeDefaultBrowser