How to get multiple selected rows of table

I’m trying to get multiple selected rows of my table. Using an Array, but even if I select more than one row, all I get back is the last selected… this is the code I use:

set vSelectedRows to plugstable's selectedRow() as integer

already tried with selectedRows but can’t get it to work…
Anyone a solution?!?
Thanks!

Try selectedRowIndexes()

Works like a charm…
Thanks Shane!

hmmm. This returns
[number of indexes: 6 (in 1 ranges), indexes: (1-6)]

Can’t get a repeat like this to work:

set vSelectedRows to plugstable's selectedRowIndexes()
		set vArrangedDS to arrangedObjects of theDParray
		repeat with vRow in vSelectedRows
			set vRow2Move to vArrangedDS's objectAtIndex_(vRow)
			set vPlugName to (vRow2Move's valueForKey_("plugname"))
			log vPlugName
		end repeat

it errors this:
<NSAppleEventDescriptor: 'obj '{ ‘form’:‘indx’, ‘want’:‘cobj’, ‘seld’:1, ‘from’:'obj '{ ‘form’:'ID ', ‘want’:‘ocid’, ‘seld’:‘kptr’($C0A90F0002000000$), ‘from’:‘null’() } }>

Try:

       repeat with vRow in (vSelectedRows as list)

Doesn’t work…

set vSelectedRows to plugstable's selectedRowIndexes()
		set vArrangedDS to arrangedObjects of theDParray
		log vSelectedRows
		repeat with vRow in (vSelectedRows as list)
			log vRow
			--set vRow2Move to vArrangedDS's objectAtIndex_(vRow)
			--set vPlugStatus to (vRow's valueForKey_("plugswitch")) as boolean
			--set vPlugName to (vRow2Move's valueForKey_("plugname"))
			--log vPlugName
		end repeat

vRow logs exactly the same as vSelectedRows… bummer :frowning:

vRow logs exactly the same as vSelectedRows

Which is…?

ohw sorry, this is in the log:

2010-04-15 19:58:39.788 PT Plug-in Manager[1499:a0f] <NSIndexSet: 0x200260000>[number of indexes: 5 (in 1 ranges), indexes: (2-6)]
2010-04-15 19:58:39.790 PT Plug-in Manager[1499:a0f] <NSIndexSet: 0x200260000>[number of indexes: 5 (in 1 ranges), indexes: (2-6)]

So it look likes some sort of c code and I’m afraid I’ll need some other c-code to get the indexes…

Ah, so vSelectedRows is an NSSet, not an NSArray. So to convert it, you’ll need something like:

setRow list to (vSelectedRows's allObjects()) as list

NSIndexSet != NSSet :wink:

You’re right, of course :frowning:

So let’s forget about indexes, and ask the array controller for selectedObjects()…

Aha, that was the trick!

Still confusing al this objective-c… Can’t get used to it after years of applescript studio…
Thank you very much Shane!

Unlike AppleScript (Matt Neuburg calls it the “English-likeness” Monster), Objective-C is a real (programming) language. Personally it took 2 weeks to understand the basics of AppleScript and half a year to understand the basics of ObjC.

Well, this works…:

set vSelectedRows to plugstable's selectedRowIndexes()
		set vArrangedDS to arrangedObjects of theDParray
		log vSelectedRows
		set vRows2Switch to (vSelectedRows's allObjects()) as list
		repeat with vRow in vRows2Switch
			set vRow2Move to vArrangedDS's objectAtIndex_(vRow)
			--set vPlugStatus to (vRow's valueForKey_("plugswitch")) as boolean
			set vPlugName to (vRow2Move's valueForKey_("plugname"))
			log vPlugName
		end repeat

Where plugstable is just the table en the theDParray the array…
But I’ll try to ask the array controller for selectedObjects()…

I know, we’re not the only ones finding this transition a little bit hard to learn.

There are two ways to the user can use the application I’m working on: by clicking on a checkbox column or to select mulitple rows and use a menu item.

The code now i:

set vSelectedRows to theDParray's selectedObjects()
		if (count vSelectedRows) is 1 then
			set vClickedRow to plugstable's clickedRow() as integer
			set vArrangedDS to arrangedObjects of theDParray
			set vRow2Move to vArrangedDS's objectAtIndex_(vClickedRow)
			--set vPlugStatus to (vRow's valueForKey_("plugswitch")) as boolean
			set vPlugName to (vRow2Move's valueForKey_("plugname"))
			log vPlugName
		else if (count vSelectedRows) > 1 then
			log vSelectedRows
		else if (count vSelectedRows) is 0 then
			log "no row selected!"
		end if

This works well, but there will probable be a better way to script this nicely…

But I'll try to ask the array controller for selectedObjects()...

Letting the controller do the work is the way to go.

Hi,

I know this thread goes back some time but I was looking for information on getting an array of objects from a table.

This was not discussed here but seems to do the trick:

		

set tIndexSet to pTransactionTable's selectedRowIndexes()
		
set tArray to pRecordData's objectsAtIndexes_(tIndexSet)
		
log tArray


This returns an array containing the selected objects. It may be useful to someone.

All the best

Terry

Hi,

A follow up to previous code snippet:

This gets the selections from the table.(tIndexSet)

Then it gets a new array containing the objects, in this case from the array pRecordData.

Then it repeats through the array of recs (each tRec is an NSMutableDictionary)

It then changes a key value to the title of a popup menu.

Then it writes the record back into the array and finally reloads the data into the table.


	on chooseCategory_(sender)
		
		set tIndexSet to pTransactionTable's selectedRowIndexes()
		
		set tArray to pRecordData's objectsAtIndexes_(tIndexSet)
		
		repeat with tRec in tArray
			set tIndex to pRecordData's indexOfObject_(tRec)			
			set tValue to pCategoryMenu's title()
			set tKey to "category"
			tRec's setValue_forKey_(tValue,tKey)
			pRecordData's replaceObjectAtIndex_withObject_(tIndex,tRec)
		end repeat
		
		pTransactionTable's reloadData()

	end chooseCategory_


All the best

Terry