Friday, December 15, 2017

#1 2009-09-24 07:41:50 am

robdut
Member
Registered: 2009-09-02
Posts: 301

get value of tableview items from array controller

Hi All,

I have my tableView set up with magic bindings. It works great but how do I access values in the table? 

I set up an array controller "Backupset" and made the script the outlet .

Should I be asking the table or the array controller?

Applescript:

script backupAppDelegate

   property parent : class "NSObject"
   property Backupset : missing value
   property Sourcefile : missing value
   
   -- IBActions ( button clicks )

   on testclick_(sender)
       set mySelectedObjects to Backupset's selectedObjects_()
       set objCount to count of mySelectedObjects
       display dialog "" & objCount & ""
   end testclick_

Rob

Offline

 

#2 2009-09-24 09:04:57 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

robdut wrote:

set mySelectedObjects to Backupset's selectedObjects_()


Sorry. Same old mistake...

It should be: 

set mySelectedObjects to Backupset's selectedObjects()

which works. but this brings me to the more specific question: How to get the value of the table cell:

Applescript:

on testclick_(sender)
set mySelected to Backupset's selectedObjects_()
set theObject to mySelected 's objectAtIndex_(0)
set theValue to theObject's valueForKey_("title")
end testclick_

The Table column id is "title" and the Array Controller has the key "properties.title"

What is the Array Controller's objectAtIndex. An Array?
It doesn't respond to  valueForKey_("title")

Any help appreciated.

Thanks in advance, Rob

sad

Offline

 

#3 2009-09-24 10:04:12 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: get value of tableview items from array controller

What is your data structure? Are you using an NSArray of NSDictionaries, list of records, etc.

Offline

 

#4 2009-09-24 10:22:16 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

Hi Craig,

Here is the initializer for the class file for the Array Controller:

Applescript:


@implementation Backupset

- (id) init
{
if (self = [super init])
{
NSArray * keys = [NSArray arrayWithObjects: @"title", @"nums",@"bums", @"rums",nil];
NSArray * values = [NSArray arrayWithObjects: @"New Backupset",@"numa", @"brms",@"rums",nil];
properties = [[NSMutableDictionary alloc] initWithObjects: values forKeys: keys];
       

sourcefiles = [[NSMutableArray alloc] init];
       
}
return self;
}

rob

Offline

 

#5 2009-09-24 10:33:10 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

The line

Applescript:

set theValue to theObject's valueForKey_(".title")

yields this in console:

2009-09-24 11:28:33.319 backupC[1154:a0f] *** -[backupCAppDelegate testclick:]: [<Backupset 0x20009db80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key properties.title. (error -10000)


The key is defined in the array controller key list as well as the properties instance of NSMutableDictionary.

I wonder if we are accessing the Dictionary object or something else with

Applescript:

set theObject to mySelectedObject's objectAtIndex_(0)

Rob

Offline

 

#6 2009-09-24 10:36:20 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: get value of tableview items from array controller

I haven't had time to play with this but the "." is not part of ".title" so remove it and see if that fixes it.

It should be "title"

Offline

 

#7 2009-09-24 10:54:31 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

No, I saw that just after the last post and removed it.

Still no luck.

I tried "properties.title" as well and that didn't work.

It's perplexing because I can get a count with selectedObjects or arrangedObjects so there is definitely and array or dictionary there representing the table contents but why it doesn't have a key "title". I wonder if I can get it to somehow log the dictionary itself so I can see what is in there.

Rob

Offline

 

#8 2009-09-24 10:58:24 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: get value of tableview items from array controller

Hey Rob,

Zip it up and email me.

Offline

 

#9 2009-09-24 11:54:41 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: get value of tableview items from array controller

Ok, here it is.

Applescript:

   on testclick_(sender)
       set mySelectedObject to Backset's selectedObjects()
       --set mySelectedObject to Backset's arrangedObjects()
       --we can get a count at least
       --display dialog (count of mySelectedObject)
       
       set theObject to mySelectedObject's objectAtIndex_(0)
       set theValue to theObject's |properties|'s valueForKey_("title")
       
       -- no key "title" it says
       display dialog "" & theValue & ""
       
   end testclick_

The line that correctly identifies the object is:

Applescript:

set theValue to theObject's |properties|'s valueForKey_("title")

The selected object in Backset is an object created from the Backupset class. Once you get hold of
the object, you ask its array "properties" for its valueForKey.

HTH,

Craig

Offline

 

#10 2009-09-24 12:07:04 pm

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

I kept trying  "properties" and it just didn't look right because it was an applescript word and didn't belong to the object it was directed at. I forgot the pipes around it!

This clears a lot up.

Thanks so much Craig.

Back on track,

Rob

Offline

 

#11 2009-09-24 07:31:54 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: get value of tableview items from array controller

I suspect you're making things harder for yourself than necessary.

You can use an Array Controller without any code at all. You have your main script containing a variable that you bind the Array Controller to, and then you bind the table to the Array Controller. You also bind another variable to the selection of the Array Controller. No code needed, and your variables change as the table changes.

I'll be posting an example shortly showing this.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#12 2009-09-24 08:21:31 pm

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

Shane Stanley wrote:

I suspect you're making things harder for yourself than necessary.

You can use an Array Controller without any code at all. You have your main script containing a variable that you bind the Array Controller to, and then you bind the table to the Array Controller. You also bind another variable to the selection of the Array Controller. No code needed, and your variables change as the table changes.

I'll be posting an example shortly showing this.


This sounds right - though I have two tables, each with it's own array controller. Table 2's contents are bound to table 1's selection and I also have a bunch of user preferences that are bound to table 1's selection. So changing selection in table 1 changes the list in the other table and also the prefs values in the app.

I can't bind the prefs values, for example, to my script variables because they are already bound to the Array Controller for table 1. And I have to "get"  the values of the list items in table 2 by the lines indicated earlier

This is all set up with minimal code using several class files and the controllers. But"getting" and "setting" are a pain.

So I hope there is a simpler way because now I am trying to  manually "set" the values for a new row in table 2 via the + button  with this example which fails so far:

Applescript:

on addFilesToListTable_(sender)
       set theKeys to NSArray's arrayWithObjects_("address", "subject")
       set theVals to NSArray's arrayWithObjects_("someaddress", "somesubject")
       set newDictinaryObject to NSMutableDictionary's alloc()'s initWithObjects_forKeys_(theVals, theKeys)
       Sourcef's addObject_(newDictinaryObject)
   end addFilesToListTable_

It does enter a new row but it is blank. I tried adding the required "nil" to the end of the lists, putting them in brackets etc. but no luck.

The bindings work great but accessing the tables is tricky without those nice bound variables in the script.

Oh no!

help.

Rob

Offline

 

#13 2009-09-24 08:57:15 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: get value of tableview items from array controller

Take the code out of table 1's Array Controller. Put a property in your AS called tableData, and bind table 1's Array Controller to it. In your applicationWillFinishLaunching_ handler set tableData to a list of records:

set my tableData to {{firstName:"John",lastName:"Smith"},...}

Now when you want to change the contents of table 1, you just change the value of my tableData (and not by using "set end of... to..."). So:

set my tableData to {{firstName:"Jane",lastName:"Doe"}} & tableData

You won't be able to bind to prefs as well, but scripting prefs is is easier than scripting table contents.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#14 2009-09-25 04:42:23 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

Shane Stanley wrote:

Take the code out of table 1's Array Controller. Put a property in your AS called tableData, and bind table 1's Array Controller to it. In your applicationWillFinishLaunching_ handler set tableData to a list of records:

set my tableData to {{firstName:"John",lastName:"Smith"},...}

Now when you want to change the contents of table 1, you just change the value of my tableData (and not by using "set end of... to..."). So:

set my tableData to {{firstName:"Jane",lastName:"Doe"}} & tableData

You won't be able to bind to prefs as well, but scripting prefs is is easier than scripting table contents.


I'll mess around with this. A simple record would be far nicer to use than the array language. . The values I need to change are in table 2 though and that is the one bound to table 1's selection. It seems a bit trickier - because I can't unbind table 2 to make it available to script.

And the prefs (all 24 of them!) could be done with selectionChanged but that means they need their own array somehow linked to the table's array (scary.) 

You can see the code idea I am using at

http://www.cocoadevcentral.com/articles/000080.php

which I have seen in many places as the usual way for this kind of binding of tables..

Now - I could go back to the old way using glue code for the two tables with datasource outlets and then have tables available for binding but that involves way more code and all the datasource handlers etc...

But I really would like to find a simpler way.  in the end if I can get access to set the table values right, it still may be less code for me in the ned. Maybe if you post your example - and if you can do it with two tables linked my way, let me know. I'm no expert here!

Thanks Shane, Rob

Offline

 

#15 2009-09-25 07:25:31 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: get value of tableview items from array controller

Try something like this:

Have a property in your AppDelegate called theData, and set it to a list of records like:

        set my theData to {{mailBox:"Inbox", emails:{{firstName:"Ray", lastName:"Robertson"}, {firstName:"Shane", lastName:"Stanley"}}}, {mailBox:"Outbox", emails:{{firstName:"Jane", lastName:"Smith"}, {firstName:"Paula", lastName:"Ramsay"}}}}

Now add an Array Controller, call it First, and bind its Content Array to the AppDelegate, with Model Key Path of theData.

In your first table, bind the first column's Value to First, arrangedObjects, MKP mailBox.

Now add a second Array Controller, call it Second, and bind its Content Array to First, Controller Key selection, MKP emails.

Finally, bind the Value of the first column of your second table to Second, arrangedObjects, MKP firstName. Bind the second similarly to lastName.

Now you can use the remove: method of the controllers, and you can add by modifying theData.

A tip for bindings stuff: do one at a time and test after each; it makes trouble-shooting so much easier.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#16 2009-09-26 05:27:12 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

Shane,

I gave it a try. I got the first table working Ok though if you edit  rows they won't retain the new values when you remove  an item from the row.

Adding table 2 works also but is more buggy. When adding a row the first table gets deselected and you don't see the  new row. Same issue with editing rows and sometimes the rows get added instead of removed!

Something isn't wired right.

I'll play some more with it.

Rob

Offline

 

#17 2009-09-26 06:39:23 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: get value of tableview items from array controller

robdut wrote:

I gave it a try. I got the first table working Ok though if you edit  rows they won't retain the new values when you remove  an item from the row


Make sure Handles Content as Compound Value is selected in the array controller's binding.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#18 2009-09-26 08:29:10 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

Shane Stanley wrote:
robdut wrote:

I gave it a try. I got the first table working Ok though if you edit  rows they won't retain the new values when you remove  an item from the row


Make sure Handles Content as Compound Value is selected in the array controller's binding.


That seems to have fixed the non-retaining issue though now, when you remove a row from either table, you can no longer add new rows to table 2!

I keep going around with this. It seems close to working but I also had to use

Applescript:

aTableView's selectRow_byExtendingSelection_(theoldRowIndex, false)

to get the table 1 to select the correct row after adding rows to table two.

If it can work in a non buggy way it will be good. I still prefer using the classes I have set up for handling the array controllers since it is very solid and requires no workarounds.

Yet if this can work, it will be a nice simple solution, especially for those with no OBJ-C in their proj.

I'll experiment some more,

Rob

Offline

 

#19 2009-09-26 08:37:09 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

Shane Stanley wrote:
robdut wrote:

I gave it a try. I got the first table working Ok though if you edit  rows they won't retain the new values when you remove  an item from the row


Make sure Handles Content as Compound Value is selected in the array controller's binding.


actually with this selected for table 2, the rows  add after a remove!

With it selected you can add rows but when you delete some and add a row again , it returns to the all rows displayed before the first deletion.

Rob

Offline

 

#20 2009-09-26 11:59:48 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: get value of tableview items from array controller

How are you adding and removing?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#21 2009-09-27 06:42:55 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

Hi Shane,

I use

   

Applescript:


property theData:missing value

on addSet_(sender)

       set my theData to my theData & {{backupset:"newBackupset", sourcefiles:{{fileicon:"",sourcefile:"somefile", filesize:"---"}}}}
       
end addSet_


on awakeFromNib()
       
       set my theData to {{backupset:"newBackupset", sourcefiles:{{fileicon:"a", sourcefile:"somefile", filesize:"---"}}}, {backupset:"newBackupset", sourcefiles:{{fileicon:"b", sourcefile:"anothersomefile", filesize:"---"}}}}

   end awakeFromNib

for the add and the built in "remove" connected through IB.


I started fresh again and copied exactly all the same settings you had in the sample and went back to just one table and it does the same thing: When you edit  rows and then do "remove", the rows return to their original values. It seems better than before otherwsie. Worse trouble begins when I hook up the second table.

Rob

Offline

 

#22 2009-09-27 07:02:28 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: get value of tableview items from array controller

Try reversing that to:

set my theData to {{backupset:"newBackupset", sourcefiles:{{fileicon:"",sourcefile:"somefile", filesize:"---"}}}} & my theData

You'll see I mention that in the sample I posted about. I suspect it forces coercion of theData from being just a pointer to a record.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#23 2009-09-27 07:26:29 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

Shane Stanley wrote:

Try reversing that to:

set my theData to {{backupset:"newBackupset", sourcefiles:{{fileicon:"",sourcefile:"somefile", filesize:"---"}}}} & my theData

You'll see I mention that in the sample I posted about. I suspect it forces coercion of theData from being just a pointer to a record.


I tried that and it has the same problem. I changed it because I wanted the new set to appear at the end of the list.

But still can't edit the fields and retain their values. I must be missing something because it seems to edit and retain in your sample proj.

Rob

Offline

 

#24 2009-09-27 07:31:33 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: get value of tableview items from array controller

All I can suggest is that you play with the bindings settings -- they can be tricky.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#25 2009-09-27 08:19:47 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: get value of tableview items from array controller

I just downloaded a fresh copy of your florida app and it does the same thing!

Edit some of the names and then try to delete a row. The values go back to the original values. Oddly the last time I tried this with your app it seemed to retain values....

Rob

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)