[Xcode] Enabled state of table row

I would like to set the enabled state of a table row in Xcode, depending on the content of one of its cells.
Is it possible?
:wink:

Yes, it is possible :wink:

Ok. Here is the situation:
I need to make each row which first cell starts with ā€˜[ā€˜ disabled and not selectable.

How to achieve this?

I like where youā€™re going with this script. Iā€™m doing something similar with Myriad Tables.

An appleScript palette could be very cool.

Hi Ed,

Itā€™s not a script but a Xcode app.
The window is a NSPanel that stays above documents.
It does not dismiss when a button is clicked.

Itā€™s almost a real paletteā€¦
:slight_smile:

You need to provide more information. Is it a cell-based table or view-based? Are you using bindings or a data-source? Is the info you show in an array of dictionaries?

The table is cell-based.
It uses bindings and the info is in a list of records.

In fact, I started with your Ā« File table Ā» from AppleScriptObjC Explored as a reference.

OK, you need to implement the delegate method -tableView:shouldSelectRow:, which should return true or false depending on whether you want the selection to go ahead. Assuming the key for the relevant value in your records is theName, you would use something like this:

on tableView:theTable shouldSelectRow:theRow
	set rowObject to thearrayController's arrangedObjects()'s objectAtIndex:theRow
	return not (((rowObject's objectForKey:"theName")'s hasPrefix:"[") as boolean)
end tableView:shouldSelectRow:

It might be cleaner to add and extra value to your records when you build the array ā€” say, a boolean value for the key canSelect. Then the handler would be:

on tableView:theTable shouldSelectRow:theRow
	set rowObject to thearrayController's arrangedObjects()'s objectAtIndex:theRow
	return (rowObject's objectForKey:"canSelect") as boolean
end tableView:shouldSelectRow:

Canā€™t make it work.
Do I have to bind something to the delegate or just copy/paste your snippet in the AppDelegate script?

You need to make your app delegate also the delegate of the table. Drag from delegate in that HUD view to the icon for your app delegate.

Iā€™m not luckyā€¦
Every single row is now unselectable.
I get these errors in the log console:

2019-04-16 12:48:31.342485+0200 Script Palette[47924:7428374] *** -[AppDelegate tableView:shouldSelectRow:]: -[_NSControllerArrayProxy objectAtindex:]: unrecognized selector sent to instance 0x620000003a90 (error -10000)
2019-04-16 12:48:31.342683+0200 Script Palette[47924:7428374] -[_NSControllerArrayProxy objectAtindex:]: unrecognized selector sent to instance 0x620000003a90
2019-04-16 12:48:31.342798+0200 Script Palette[47924:7428374] *** -[AppDelegate tableView:shouldSelectRow:]: -[_NSControllerArrayProxy objectAtindex:]: unrecognized selector sent to instance 0x620000003a90 (error -10000)

The binding:

aaa

Oops: objectAtIndex:. Iā€™ll amend the post for future lurkers.

Ahhhhhh! Iā€™m loosing my eyes!
ā€¦

May I ask for a last thing?
How to disable the same rows that are not selectable?
Or at least their 1st cell (those whoā€™s text content is wrapped in brackets).
I thought about binding, butā€¦
:wink:

Assuming you add the canSelect value, you could probably bind the Enabled property of the column to the array controller using that key.

Otherwise you need to implement another delegate method, -tableView:willDisplayCell:forTableColumn:row:, and set enabled or not accordingly.

Like this?

capture%20003

It has no effect.

The panel has a dark gray box in background, the view and the table does not draw background, each window item has an ā€œDivide Blend Modeā€ Core Animation filter.
I guess itā€™s the reason why the binding seems to have no effect.
So Iā€™m using the tableView:willDisplayCell:forTableColumn:row: method to change the text color.

Hereā€™s the snippet:

	on tableView:theTable willDisplayCell:theCell forTableColumn:theCol row:theRow
		set {grey1, grey2} to {0.7, 0.95}
		set rowObject to theArrayController's arrangedObjects()'s objectAtIndex:theRow
		if (not (rowObject's objectForKey:"theSelect") as boolean) then theCell's setTextColor:(current application's NSColor's colorWithRed:grey1 green:grey1 blue:grey1 alpha:1) 
		if ((rowObject's objectForKey:"theSelect") as boolean) then theCell's setTextColor:(current application's NSColor's colorWithRed:grey2 green:grey2 blue:grey2 alpha:1) 
	end tableView:willDisplayCell:forTableColumn:row:

Shane, I want to thank you again for your willingness to help.
Without your books, examples and answers here, I could not achieve this project.
:wink:

I suspect the reason is even simpler: disabling a text field doesnā€™t change its appearance.

Yes, it does.
In an earlier version I thought that, with the good predicate, I could disable the rows.
Here is how it looks like:

The entire column was disable because the predicate was badly written and always returning false.
[I donā€™t know if a predicate can be used hereā€¦]