⬆️ ⬇️

Software button click with Applescript

Task: click a button in a specific window. Well, if the program has built-in functions and pressing a button can be a team through AppleScript. But if we need to press a button in an unknown window, for example in the system settings? Below will be shown how to do it.



In order to mimic a button click through AppleScript, you need to check whether user interface elements are included, in other words, whether the system will allow you to do this.

MacOS is very interesting in calling various controls, namely Button 1, Text Field 2, and so on. That is, the window is represented by some array of elements of the graphical interface, and each class has its own numbering.



Now let's proceed directly to clicking. Guinea pig for us will be the Bluetooth enable button in the system settings. Below is the source code of the script. It is quite simple, taking into account the comments above.



on turnBluetooth()

tell application "System Preferences"

activate

set current pane to pane "Bluetooth"

--enabling bluetooth

tell application "System Events"

if UI elements enabled then

tell tab group 1 of window "Bluetooth" of process "System Preferences"

click button 1

end tell

end if

end tell

end tell



tell application "System Preferences" to quit

end turnBluetooth




This function (turnBluetooth ()) can be called anywhere in your script by writing:

')

my turnBluetooth()

Source: https://habr.com/ru/post/58478/



All Articles