Identifying buttons (and coding) for System Preferences "Network" pane

I’ve been trying to get this to work for over 2 hours. Basically, I want to make my Internet inactive, via the “Network” pane in System Preferences. I’m trying to identify the button names, using Accessibility Inspector, but I always get errors.

Here is the correct code I’ve gotten so far:

tell application "System Preferences"
	activate
	reveal pane "Network"
	tell application "System Events"
		[...]
		keystroke "q" using {command down}
	end tell
end tell

Here are the buttons I’m trying to identify:

Could someone please help me out?

Here is a sample to click the menu item you have selected


tell application "System Events"
	set ap to application process "System Preferences"
	tell application "System Preferences"
		activate
		reveal pane "Network"
	end tell
	click menu button "Service Actions" of window 1 of ap -- application process "System Preferences"
	delay 0.2
	click menu item "Make Service Inactive" of menu "Service Actions" of menu button "Service Actions" of window 1 of ap
end tell

BTW this only works if the Network pane is unlocked.
if it has a lock in the bottom left corner, you would have to script the unlock

Hi Robert,

I get this error:

(My Network pane isn’t locked.)

On the Catalina you can try this:


tell application "System Events" to tell process "System Preferences" to tell window "Network"
	click menu button "Service Actions"
	tell menu button "Service Actions"
		repeat until menu 1 exists
			delay 0.02
		end repeat
		try
			click menu item "Make Service Inactive" of menu 1
		on error
			click menu item "Make Service Active" of menu 1
		end try
	end tell
	click UI element "Apply"
end tell

Weird.

Because I’m on Catalina.

I would like to know at what line that error occurs?

EDIT - I just had a thought. Is “System Preferences” already running when you run the script?

I got it to work, by combining the different scripts in the thread.

I don’t know if it is correctly structured, but it works! :slight_smile:

tell application "System Preferences"
	reveal anchor "Wi-Fi" of pane id "com.apple.preference.network"
	activate
end tell
tell application "System Events"
	click menu button "Service Actions" of window 1 of application process "System Preferences"
	delay 0.2
	click menu item "Make Service Inactive" of menu "Service Actions" of menu button "Service Actions" of window 1 of application process "System Preferences"
	delay 0.8
	click button "Apply" of window 1 of application process "System Preferences"
	delay 0.2
	tell application "TextEdit"
		quit
	end tell
end tell

Here is the whole workaround for Catalina, including automated authorization:


tell application "System Preferences"
	reveal anchor "Wi-Fi" of pane id "com.apple.preference.network"
	activate
end tell

tell application "System Preferences" to authorize of pane id "com.apple.preference.network"
tell application "System Events"
	repeat until sheet 1 of window "Network" of application process "System Preferences" exists
		delay 0.02
	end repeat
	keystroke "MyUserPassword" & return -- replace MyUserPassword with your real password
	repeat while sheet 1 of window "Network" of application process "System Preferences" exists
		delay 0.02
	end repeat
end tell

tell application "System Events" to tell process "System Preferences" to tell window "Network"
	click menu button "Service Actions"
	tell menu button "Service Actions"
		repeat until menu 1 exists
			delay 0.02
		end repeat
		try
			click menu item "Make Service Inactive" of menu 1
		on error
			click menu item "Make Service Active" of menu 1
		end try
		repeat while menu 1 exists
			delay 0.02
		end repeat
	end tell
	click UI element "Apply"
end tell

tell application "System Preferences" to quit

Here is my shortened version

tell application "System Events"
	tell application "System Preferences" to reveal pane "Network"
	repeat until window "Network" of application process "System Preferences" exists
		delay 0.2
	end repeat
	tell application "System Preferences" to authorize of pane id "com.apple.preference.network"
	set ap to window "Network" of application process "System Preferences"
	repeat until sheet 1 of ap exists
		delay 0.2
	end repeat
	set sp to sheet 1 of ap
	set value of text field 2 of sp to "username"
	set value of text field 1 of sp to "password"
	click button "Unlock" of sp
	repeat while sheet 1 of ap exists
		delay 0.2
	end repeat
	click menu button "Service Actions" of ap -- application process "System Preferences"
	repeat until menu "Service Actions" of menu button "Service Actions" of ap exists
		delay 0.2
	end repeat
	click menu item "Make Service Inactive" of menu "Service Actions" of menu button "Service Actions" of ap
end tell

I edited it to add better timing loops to wait for redraws to catch-up

Fredrik71

The reason I put tell application “System Preferences” where it is

 tell application "System Preferences" to reveal pane "Network"
   repeat until window "Network" of application process "System Preferences" exists
       delay 0.2
   end repeat
   tell application "System Preferences" to authorize of pane id "com.apple.preference.network"

is because sometimes the line

tell application "System Preferences" to authorize of pane id "com.apple.preference.network"

doesn’t work because it executed to fast before the pane was finished loading.

Hence the GUI scripting lines below between the 2 tell statements

 repeat until window "Network" of application process "System Preferences" exists
       delay 0.2
   end repeat