Better way to create a list from selected items of a table?

I have a table where I allow the user to select multiple rows. I’d like to build a list from the contents of a cell for each selected item. I thought this would work but it does not…

if (count of selected_items) ≥ 1 then
	set theIndexList to contents of data cell "myCell" of every data row in selected_items of theDataSource
end if

instead I have to loop through each selected item…

if (count of selected_items) ≥ 1 then
	repeat with theRow in selected_items
		set theIndexList to theIndexList & "," & contents of data cell "myCell" of data row theRow of theDataSource
	end repeat
end if

this should work without repeating:


tell (table view "tv" of scroll view "sv" of window "main")
	set allItems to (content of data cell "myCell" of data rows of it's data source)
	set theIndexList to ( call method "objectsAtIndexes:" of allItems ¬
		with parameter (call method "selectedRowIndexes" of it) )
end tell
if (length of theIndexList ≠ 0) then -- testing for an empty selection
	-- ....
end if

D.

Well that works perfectly. Now if I just knew what “call method” was all about. Guess I’ll have to read up. Thanks for the information.

It’s quite simple … ‘call method’ is AppleScript’s direct access to Cocoa methods and objects.

(call method “selectedRowIndexes” of it)

  • is a method of NSTableView - it results an NSIndexSet (some kind of list of the indexes …) of all selected rows

the result is forwarded as parameter to the other ‘call method’:

set theIndexList to ( call method “objectsAtIndexes:” of allItems with parameter …

  • this is a method of NSArray (the Cocoa term for a List in AppleScript). It returns a list of all array items from the given (-> parameter) NSIndexSet.