Using "System Events" to select multiple items in AXOutline

I have a need to select multiple items in a non-scriptable app. Luckily System Events can be used to help with that. However I'm finding that I can't seem to select multiple items.

My first approach was:

-- Close all but one window in Finder, and click on toolbar button (top-right) to hide extra panes
tell application "System Events"
set myOutline to outline 1 of scroll area 1 of splitter group 1 of window 1 of process "Finder"
set value of attribute "AXSelected" of row 1 of myOutline to true
set value of attribute "AXSelected" of row 2 of myOutline to true
end tell

What this does is select row 1, then row 2 but de-selecting row 1.

Then I noticed that the "AXSelectedRows" attribute of the outline is writable. So I tried:

tell application "System Events"
set myOutline to outline 1 of scroll area 1 of splitter group 1 of window "PRs" of process "Finder"
set value of attribute "AXSelectedRows" of myOutline to {row 1 of myOutline, row 2 of myOutline}
end tell

But this seems to have no effect whatsoever on the selection. In fact setting it to anything that won't generate an error will have no effect. If an error occurs in referencing then selection is cleared.

Anybody know what the secret to getting multi-selection working with AXOutline?

Thanks,

Matt

MacPro, Mac OS X (10.6.5)

Posted on Mar 4, 2011 7:13 PM

Reply
7 replies

Mar 5, 2011 1:24 AM in response to Matthew Centurión

That certainly seems more like a bug than a feature.

The only way I found to select multiple rows in a Finder window was to use [Cliclick|http://www.bluem.net/en/mac/cliclick>:

--Close all but one window in Finder, and click on toolbar button (top-right) to hide extra panes
*tell application "Finder" to activate*
*tell application "System Events"*
* set myOutline to outline 1 of scroll area 1 of splitter group 1 of window 1 of process "Finder"*
* set selected of rows of myOutline to false* -- unselect all rows
* set {x1, y1} to position of row 1 of myOutline*
* set {x2, y2} to position of row 2 of myOutline*
* set {x5, y5} to position of row 5 of myOutline*
* do shell script "/usr/local/bin/cliclick " & x1 & space & y1*
* key down command*
* do shell script "/usr/local/bin/cliclick " & x2 & space & y2 & space & x5 & space & y5*
* key up command*
*end tell*


That being said, it's still possible that you could select multiple items in your non-scriptable application even if that doesn't seem to be possible with Finder windows.

Mar 7, 2011 10:47 AM in response to Pierre L.

Hi Pierre,

In that case this seems to be a general bug with AXOutline, I used the Finder as an example but this also happens with any app using AXOutline. I was hoping not to have to use "mouse clicks" and "key downs" as this increases unreliability due to unforeseen UI blocking items, but I may have to do this for the time being. I was hoping this was a case of my not using the property correctly.

I'll post a bug with Apple but am interested in hearing any other experiences around this problem.

Thanks,

Matt

Mar 7, 2011 12:59 PM in response to Matthew Centurión

I was hoping not to have to use "mouse clicks" and "key downs"


Try this:

*set theRowsToSelect to {1, 2, 5}* -- for example
*tell application "Finder" to activate*
*tell application "System Events"*
* tell outline 1 of scroll area 1 of splitter group 1 of window 1 of process "Finder"*
* set Y0 to item 2 of (get position of row 1)*
* set H to item 2 of (get size of row 1)* -- the row height
* set theVerticalPositions to {}*
* repeat with k in theRowsToSelect*
* copy Y0 + (k - 1)* * *H to the end of theVerticalPositions*
* end repeat*
* keystroke "a" using command down* -- select all rows
* repeat with thisRow in the rows*
* if item 2 of (get position of thisRow) is not in theVerticalPositions then*
* set selected of thisRow to false*
* end if*
* end repeat*
* end tell*
*end tell*

Mar 7, 2011 2:32 PM in response to Pierre L.

And finally, a much simpler version of the previous solution:

*set theRowsToSelect to {1, 2, 5}* -- for example
*tell application "Finder" to activate*
*tell application "System Events"*
* tell outline 1 of scroll area 1 of splitter group 1 of window 1 of process "Finder"*
* keystroke "a" using command down* -- select all rows
* repeat with k from 1 to (count rows)*
* if k is not in theRowsToSelect then*
* set selected of row k to false*
* end if*
* end repeat*
* end tell*
*end tell*


Hope it can help.

Message was edited by: Pierre L.

Mar 8, 2011 7:48 AM in response to Matthew Centurión

Matthew Centurión wrote:
I have a need to select multiple items in a non-scriptable app...
My first approach was: -- Close all but one window in Finder, and click on toolbar button (top-right) to hide extra panes...


While your app may not be scriptable, the Finder certainly is. For example:


tell application "Finder"
open (choose folder without invisibles)
set current view of window 1 to list view
set toolbar visible of window 1 to false
select {item 1, item 2} of window 1
end tell


Note that the extra panes are hidden using *set toolbar visible of window 1 to false*.

Also, you have flexibility in how the desired items are selected. For example, if the file names are known but their positions on the list are unknown, you could try something like:


tell application "Finder"
open (choose folder without invisibles)
set current view of window 1 to list view
set toolbar visible of window 1 to false
select {item "some file", item "some other file"} of window 1 --> edit with actual file names
end tell


Or you could create a list of items in a variable:


set my_list to {"some file.ext", "some other file.ext"} --> edit with actual file names and include extensions
tell application "Finder"
open (choose folder without invisibles)
set current view of window 1 to list view
set toolbar visible of window 1 to false
select (every item of window 1 whose name is in my_list)
end tell


Good luck.

+Scripts tested in Mac OS 10.4.11+

Mar 8, 2011 5:49 PM in response to Matthew Centurión

Unfortunately the Finder example I used was just an example.


Understood.

As long as list items to be selected are contiguous, you might have options using your keyboard's down arrow key. Using a Finder window as an example:


tell application "Finder"
open (choose folder without invisibles)
set current view of window 1 to list view
set toolbar visible of window 1 to false
end tell
tell application "System Events"
key code 125 --> selects 1st item
key code 125 using shift down --> selects next item; repeat as needed
-- do stuff here; e.g., open the items:
keystroke "o" using command down
end tell


Good luck.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Using "System Events" to select multiple items in AXOutline

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.