Kei Minagawa
Kei Minagawa

Reputation: 4521

How to access UI which applescript can not access?

I'm looking for the way to access UI which applescript can not access. I know applescript can access UI element. But sometimes I meet the case I can't access UI element by applescript. Like below ...

tell application "System Preferences" to activate
delay 1
tell application "System Events"
    tell process "System Preferences"
        set frontmost to true
        click button 22 of scroll area 1 of window 1
        delay 1
        UI elements of last group of list 1 of window 1
    end tell
end tell

This just returns 2 UI elements image and static text.

{
image 1 of group 3 of list 1 of window "Users & Groups" of application process "System Preferences" of application "System Events",
static text "Login Options" of group 3 of list 1 of window "Users & Groups" of application process "System Preferences" of application "System Events"
}

Apparently there is a clickable UI element which above 2 elements belonging. Because I can click "Login Options".

Fortunately by sending key stroke and key code commands were received properly.

set DownArrow to 125
set UpArrow to 126
tell application "System Preferences" to activate
delay 1
tell application "System Events"
    tell process "System Preferences"
        set frontmost to true
        click button 22 of scroll area 1 of window 1
        delay 1
        keystroke tab
        repeat 3 times
            delay 1
            key code DownArrow
            delay 1
            key code UpArrow
        end repeat
    end tell
end tell
tell application "System Preferences" to quit

My question is ...

Any hint is appreciated. Any code(python, c, objective-c, etc) is more appreciated.:)

Thanks.

Upvotes: 0

Views: 1271

Answers (2)

CRGreen
CRGreen

Reputation: 3429

To solve many of these problems you do not need System Events. For example, System Preferences is script-able enough to at least do this, which solves a few of your problems:

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preferences.users"
    reveal anchor "loginOptionsPref" of current pane
end tell

So,

  1. AppleScript isn't "ignoring" the Login Options control;
  2. You don't need to click, you can use "reveal" (do you mean to type "can't" instead of "can"? -- I've edited your post to format your questions more correctly);
  3. I think this makes the 3rd question moot;
  4. My example shows the way to do it w/o clicking
  5. How to access "non-accessable" items -- much bigger question:

While it is true that System Preferences is script-able, there are times when you may need System Events. For example, I have a script for the Sound pane that uses the following handler (subroutine):

on doSysEvSelect(whatToSelect, whatWindow)
    tell application "System Events"
        tell process "System Preferences"
            select (row 1 of table 1 of scroll area 1 of tab group 1 of window whatWindow whose value of text field 1 is whatToSelect)
        end tell
    end tell
end doSysEvSelect

, which I can use by doing, for example:

doSysEvSelect("Internal Microphone", "Sound")

Using System Events can be frustrating to figure out. But of course there are many examples around to find. And if you're really in a bind, you can use Sikuli (http://sikuli.org/) to automate just about anything, but which relies totally on the UI and clicking.

[edit]

And here's an example of what to do with an item in Login Options once you're there (this is where you need to use System Events):

tell application "System Events"
    tell process "System Preferences"
        if value of checkbox "Show password hints" of group 1 of window "Users & Groups" is 0 then
            click checkbox "Show password hints" of group 1 of window "Users & Groups"
        end if
    end tell
end tell

Upvotes: 1

pbell
pbell

Reputation: 3105

You can simulate clics like you do directly when using system preferences, but you need to know which object to click. Fyi, here is an example of system preferences click and select for Bluetooth. it gives an idea of the way to do it :

tell application "System Preferences"
activate
set current pane to pane "com.apple.preferences.Bluetooth"
tell application "System Events"
    tell process "System Preferences"
        -- select row containing the word "mouse"
        repeat with I from 1 to count of row of table 1 of scroll area 2 of group 1 of front window
            set Nom_Perif to (description of static text 1 of row I of table 1 of scroll area 2 of group 1 of front window) as string
            if Nom_Perif contains "Mouse" then
                set selected of row I of table 1 of scroll area 2 of group 1 of front window to true
                if Nom_Perif contains "not connected" then
                    -- sert connection  via item 1 of menu
                    click menu button of group 1 of front window
                    delay 1
                    click menu item "Connect" of menu of menu button of group 1 of front window
                    delay 3
                end if
            end if
        end repeat
    end tell
end tell
end tell
quit application "System Preferences"

Upvotes: 1

Related Questions