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?
Yes, it is possible
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ā¦
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:
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ā¦
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?
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.
I suspect the reason is even simpler: disabling a text field doesnāt change its appearance.