Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AXIdentifier in UI Scripting for Applescript

10.7.4 OSX Lion Applescript

I am working with an application (built in house and has no Applescript dictionary) that has a static text element I want to copy to the clipboard and send to another app but I'm having a hard time getting it to work.

The script I was using for targeting the element looked like this:

Tell application "System Events" to set frontmost of process "*application*" to true
Tell application "System Events"
    Tell process "*application*" 
        Tell static text 1 of tab view 1 scroll area 1 of splitter group 1 of splitter group 1 of splitter group 1 of window 1
            keystroke "a" using command down
            delay 0.1
            keystroke "c" using command down
            delay 0.1
        end tell
    end tell
end tell
end tell

What would happen was that the wrong text from the wrong element was copied to the clipboard every time I clicked in a different spot on the application (there are numerous text fields).

I noticed in UI Accessor/Accessibility Accessor that each UI element in the application has a unique AXIdentifier value when you mouse over them.

Is there anyway to accomplishing what I am trying to do, using AXIdentifier values to target that element and copy the text from it?

Thanks for all the help this is my first post and I hope it was worthy! ~TheLarkInn

like image 265
Sean Larkin Avatar asked Sep 21 '12 04:09

Sean Larkin


2 Answers

You can do this by using AppleScript's filtering. For example, to get the From: pop-up menu in a message composition window in Apple Mail, there is no accessibility description you can match on, however there is a unique AXIdentifier which you can match as follows:

tell application "System Events"
    tell application process "Mail"
        tell window 1
            get first pop up button whose value of attribute "AXIdentifier" is "popup_from"
        end tell
    end tell
end tell

This is more efficient than looping in AppleScript as it only involves sending one Apple Event to System Events.

like image 139
Nicholas Riley Avatar answered Oct 12 '22 23:10

Nicholas Riley


I don't think there is a way to directly do what you're trying to do. It seems like you can only access attributes once you have a handle on an element via selector. Here is a very ugly solution that does what you're asking by iterating over all UI elements, but it is really slow with bigger UIs and probably not ideal for any production level code.

tell application "System Events"
  tell process "Some Process"
    set tElements to entire contents of window "Some Window"
    repeat with tElement in tElements
      if (exists attribute "AXIdentifier" of tElement) then
        if value of attribute "AXIdentifier" of tElement = "Some AXIdentifier" then set tText to value of tElement
      end if
    end repeat
  end tell
end tell
tText

I think using UIElementInspector or Accessibility Inspector from Xcode to build a selector string is the way to go!

like image 40
casaram Avatar answered Oct 12 '22 22:10

casaram