Sunday, November 29, 2020

#1 2020-11-18 07:01:08 pm

KevS
Member
Registered: 2017-05-23
Posts: 10

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:

Applescript:

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:
buttons-identify4.png

Could someone please help me out?

Offline

 

#2 2020-11-18 08:23:13 pm

robertfern
Member
Registered: 2011-11-29
Posts: 82

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

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

Applescript:


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

Last edited by robertfern (2020-11-18 08:29:20 pm)

Offline

 

#3 2020-11-19 03:14:06 am

KevS
Member
Registered: 2017-05-23
Posts: 10

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

robertfern wrote:

[...]

Hi Robert,

I get this error:

error "System Events got an error: Can’t get application process \"System Preferences\"." number -1728 from application process "System Preferences"

(My Network pane isn't locked.)

Offline

 

#4 2020-11-19 07:24:20 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

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

This works in Mojave and I test it on my Wi-Fi

Ps. You still need to 'authorize' pane id "com.apple.preference.network"

Applescript:

tell application "System Preferences"
   reveal anchor "Wi-Fi" of pane id "com.apple.preference.network"
   -- reveal anchor "TCP/IP" of pane id "com.apple.preference.network"
   -- reveal anchor "DNS" of pane id "com.apple.preference.network"
   -- reveal anchor "WINS" of pane id "com.apple.preference.network"
   -- reveal anchor "802.1X" of pane id "com.apple.preference.network"
   -- reveal anchor "Proxies" of pane id "com.apple.preference.network"
   -- reveal anchor "VPN" of pane id "com.apple.preference.network"
   -- reveal anchor "PPP" of pane id "com.apple.preference.network"
   -- reveal anchor "Bluetooth" of pane id "com.apple.preference.network"
   -- reveal anchor "Advanced Ethernet" of pane id "com.apple.preference.network"
   -- reveal anchor "Advanced VPN" of pane id "com.apple.preference.network"
   -- reveal anchor "6to4" of pane id "com.apple.preference.network"
   -- reveal anchor "Bond" of pane id "com.apple.preference.network"
   -- reveal anchor "Ethernet" of pane id "com.apple.preference.network"
   -- reveal anchor "WWAN" of pane id "com.apple.preference.network"
   -- reveal anchor "Advanced Modem" of pane id "com.apple.preference.network"
   -- reveal anchor "Advanced Wi-Fi" of pane id "com.apple.preference.network"
   -- reveal anchor "Modem" of pane id "com.apple.preference.network"
   -- reveal anchor "VLAN" of pane id "com.apple.preference.network"
   -- reveal anchor "PPPoE" of pane id "com.apple.preference.network"
   -- reveal anchor "TCP/IP" of pane id "com.apple.preference.network"
   -- reveal anchor "FireWire" of pane id "com.apple.preference.network"
   -- reveal anchor "VPN on Demand" of pane id "com.apple.preference.network"
   activate
end tell

-- If authorize need it.
delay 0.2

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

tell application "System Events" to tell process "System Preferences"
   set frontmost to true
   tell window 1
       click menu button 1
       tell menu button 1 to tell menu 1
           click menu item "Make Service Inactive"
       end tell
   end tell
end tell

Last edited by Fredrik71 (2020-11-19 06:01:36 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#5 2020-11-19 07:25:42 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

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

KevS wrote:

(My Network pane isn't locked.)


On the Catalina you can try this:

Applescript:


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


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#6 2020-11-19 08:07:20 am

robertfern
Member
Registered: 2011-11-29
Posts: 82

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

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?

Last edited by robertfern (2020-11-19 08:09:04 am)

Offline

 

#7 2020-11-19 08:49:43 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

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

robertfern wrote:

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


Your AS script only work if:
1. System Preferences is open

If you use this code it doesn't matter if the user have System Preferences open or have it
open but wrong pane. It will still work and change to correct pane or open System Preferences
with correct pane.

Applescript:

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

Last edited by Fredrik71 (2020-11-19 08:56:55 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#8 2020-11-19 09:59:33 am

KevS
Member
Registered: 2017-05-23
Posts: 10

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

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! smile

Applescript:

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

Offline

 

#9 2020-11-19 10:19:50 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

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

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

Applescript:


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


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#10 2020-11-19 02:15:13 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

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

This version do not store any password in the AS Script.

Applescript:

property AdminUserName : "Admin"

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

-- If authorize need it.
delay 0.2

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
   delay 0.5
   tell text field 2 of sheet 1 to keystroke AdminUserName -- Username
   delay 0.5
   set AdminUserPassword to (display dialog "Type Admin Password: " default answer "12345" with hidden answer)'s text returned
   
   tell application "System Preferences" to tell window 1 to activate
   delay 0.5
   key code 48 -- tab
   delay 0.5
   tell text field 1 of sheet 1 to keystroke AdminUserPassword & return
   
   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

We could also do it like this.

1. The variable AdminUserPassword ask for input with hidden answer.
2. We tell text field 1 to set focused to true
3. We keystroke the string from variable AdminUserPassword and return

Applescript:

set AdminUserPassword to (display dialog "Type Admin Password: " default answer "12345" with hidden answer)'s text returned
   
   tell application "System Preferences" to tell window 1 to activate
   delay 0.5
   -- Set focus to text field 1 -- Password:
   tell process "System Preferences" to tell sheet 1 of window 1
       tell text field 1 to set focused to true
   end tell
   delay 0.5
   tell text field 1 of sheet 1 to keystroke AdminUserPassword & return

Last edited by Fredrik71 (2020-11-19 06:00:54 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#11 2020-11-19 03:35:03 pm

robertfern
Member
Registered: 2011-11-29
Posts: 82

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

Here is my shortened version

Applescript:

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

Last edited by robertfern (2020-11-19 03:47:32 pm)

Offline

 

#12 2020-11-19 04:28:29 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

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

Robert..
I think this is a better approach of your code.

1. You do not need 'System Events' to open the right pane of System Preferences
2. Same for authorize

Good approach is: GUI Scripting is the last thing to do if everything else do not work.

ex.
Your code use window "Network" but Apple could in next OS release change it and your
code do not work. Its more likely they will not change pane id "com.apple.preference.network".

Applescript:

tell application "System Preferences"
   reveal anchor "Wi-Fi" of pane id "com.apple.preference.network"
   activate
-- We ask for the name of current pane.
   set thePane to its name of current pane
end tell

-- If authorize need it.
delay 0.2

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

tell application "System Events"
   set ap to window thePane 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

Last edited by Fredrik71 (2020-11-19 06:00:10 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#13 2020-11-19 05:11:33 pm

robertfern
Member
Registered: 2011-11-29
Posts: 82

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

Fredrik71

The reason I put tell application "System Preferences" where it is

Applescript:

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

Applescript:

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

Applescript:

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

Last edited by robertfern (2020-11-19 05:11:47 pm)

Offline

 

#14 2020-11-19 05:33:53 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 469

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

Its because all your code are inside tell block of System Events.

EDIT: If System Preferences are already open we maybe need a delay before 'authorize'

tell block 1 -> open system preference Network pane

tell block 2 -> open authorize

tell block 3 -> activate

Run this code on your system

Applescript:

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

I did some more test...

Its looks like the authorize is not activated, after I test from other pane.
All my test have always been to open a new System Preferences window.

So I test this code with delay and I think that works better and the above.

Applescript:

tell application "System Preferences" to reveal pane id "com.apple.preference.network"
tell application "System Preferences" to activate
delay 0.5
tell application "System Preferences" to authorize of pane id "com.apple.preference.network"

I will change my code to include this

Applescript:

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

delay 0.2

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

Last edited by Fredrik71 (2020-11-19 06:03:55 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)