Software Button Press Using Applescript

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

    In order to simulate a button click through AppleScript, you need to check whether the user interface elements are enabled, in other words, whether the system allows you to do this.
    MacOS very interestingly names various controls, namely Button 1, Text Field 2, and so on. That is, the window is represented by some array of graphical interface elements, and each class has its own numbering.

    Now proceed directly to pressing. An experimental rabbit for us will be the Bluetooth enable button in the system settings. The following is the source code of the script. It is quite simple taking into account the comments above. This function (turnBluetooth ()) can be called anywhere in your script by writing:

    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




    my turnBluetooth()

    Also popular now: