changing a checkbox in a table

I put this on the ASS Xcode forum but I now realise that my problems may be better addressed here.

I have a four column table in which the third column contains a checkbox.

The data comes a UNIX app and I use a repeat loop to get the data for each molecule in the file and add it to new_TableData

set record_detail to {names:num_uni_rec, tlable:new_name, isSelected:false, fileName:input_file}
copy record_detail to the end of new_TableData

and then

tell theArrayController
removeObjects_(arrangedObjects())
addObjects_(new_TableData)
end tell

to add the data, it then displays in the table.

To get data to set up a webview displaying a structure I used:

set theSel to theArrayController's selectedObjects() as list
    --log theSel
    --returns {{fileName = "/Users/swain/Desktop/the_hits.smiles";isSelected = 1;names = 12;tlable = Molecule12;}}

I now need to be able to select all, select none and invert selection.

With StefanK’s much appreciated help I’ve got to this stage

set theTableItems to theArrayController’s arrangedObjects()
–log theTableItems
repeat with n from 1 to count of theTableItems
set theTableItemsObject to theTableItems’s objectAtIndex_(n - 1)
log theTableItemsObject
theTableItemsObject’s setValue_ForKey_(1, “isSelected”)

   end repeat

But I get this error

12/11/2010 11:12:00 iBabel[9734] *** -[Viewer selectAllButton:]: -[NSCFDictionary setValue:ForKey:]: unrecognized selector sent to instance 0x2008155c0 (error -10000)

Apparently the problem is, NSDictionary is immutable and doesn’t respond to setValue, and the suggestion is to use a custom class.

This is rather more than complicated than I had imagined, any suggestions welcome:)

I’m confused. You have a property in your record called isSelected, and you’re talking about selection. Are you saying you want the records in the table selected in the interface, or do you want the checkbox represented by isSelected checked?

I want want the checkbox represented by isSelected checked for every record.

Then try something like this:

set theTableItems to theArrayController's arrangedObjects()
theTableItems's setValue_forKey_(1, "isSelected")

I think you’ll find the problem is that you’ve typed ForValue rather than forValue – case matters.

I did think that and typed a lower case f but when I save the project XCode converts it to upper case.

That’s an AppleScript problem – compile it wrong once, and it stays that way. Correct it and enclose it in pipes:

theTableItems's |setValue_forKey_|(1, "isSelected")

Again, the array controller holds an array of NSDictionary objects.
NSArray does not respond to a setValue:ForKey: method at all.
NSDictionary does not respond to setValue:ForKey: too, because it’s immutable. (That’s what the error message says)

You have two solutions

¢ define the dictionary objects as mutable (NSMutableDictionary)
¢ use a custom class which appropriate properties

That did it!!

On a scale of intuitive from 1 to 10 that has to be a -20!

Will new scripts be similarly effected or just this script?

It won’t affect new scripts. You could also just open the script directly in something like BBEdit and fix it.

AppleScript has always forced the case a variable is initially compiled in. It never used to matter because case didn’t matter, but AppleScriptObjC has changed all that.

Regarding the ASOC typing problem, I have found that you can also fix the problem by deleting the offending letter, save the changes, close the project, and then close XCode. Reopen the project, and then type in the letter correctly – it will now compile correctly.

Ric

Thanks, very useful to know as my typing is not as accurate as I would like :smiley:

Just thought I’d post a summary

I have a column in the table containing a checkbox (isSelected), users can select entries but I also needed the option to select all check boxes, unselect all or invert the selection.

	--select all entries
	on selectAllButton_(sender)
		
		tell theArrayController's arrangedObjects() to setValue_forKey_(true, "isSelected")
		
	end selectAllButton_
	--To clear selection
	on clearSelection_(sender)
		tell theArrayController's arrangedObjects() to setValue_forKey_(false, "isSelected")
	end clearSelection_
	--To invert selection
	on invertSelection_(sender)
		set theArraysRows to theArrayController's arrangedObjects()
		
		set aPredicate to current application's class "NSPredicate"'s predicateWithFormat_("isSelected = 1")
		set theSelList to theArraysRows's filteredArrayUsingPredicate_(aPredicate)
		
		set bPredicate to current application's class "NSPredicate"'s predicateWithFormat_("isSelected = 0")
		set theUnSelList to theArraysRows's filteredArrayUsingPredicate_(bPredicate)
		--log theList
		--set theCount to count of theList
		tell theSelList to setValue_forKey_(false, "isSelected")
		tell theUnSelList to setValue_forKey_(true, "isSelected")
		
		
	end invertSelection_

All seems to be working fine and is blindingly quick.