Adjusting / Changing Table Data via array controller

I am not sure If I am going about this the wrong way… Let me “try” to explain…

I have a table with 2 columns.
Upon launch it populates the table with numbers from a “master product list”

So At this point I have something simple as such:
masterProduct | theProduct
0001
0002
0003
0004

The entire theProduct column is just missing values.
I then take the masterProduct and loop through it to test the masterProduct against another list to see if there is a match…


repeat with x from 1 to count of myProducts
       set aProduct to item x of myProducts

            repeat with i from 1 to count of masterProduct
                    set theRow to item i of mainProductList
                    set theColumn to (masterProduct of theRow) as string 
                        if theColumn is equal to aProduct 
                        log "MATCH"
           
                        tell theArrayController to insertObject:({theProduct:"IN STOCK"}) atArrangedObjectIndex: ((i as integer) + 1)
                   
                        end if
                end repeat



So say it finds a match on 0002… instead of inserting “IN STOCK” in theProduct column next to masterProduct It creates a new row…
I am looking for a way to replace the value rather than add//insert a new one however I am not able to figure it out in the Documenation which makes me think I might be doing it all wrong?

Any advice would be awesome!

Hi,

imagine the data structure of the table, the content of the array controller looks like

object 1 : {masterProduct: "0001", theProduct : ""} object 2 : {masterProduct: "0002", theProduct : ""} object 3 : {masterProduct: "0003", theProduct : ""} object 4 : {masterProduct: "0004", theProduct : ""}
So you have to change the value of the theProduct property.

I recommend to do it the object oriented way

¢ create a new AppleScript file and name it ASProduct.applescript
¢ replace the content of the script file with


script ASProduct
    property parent : class "NSObject"
    
    property masterProduct : ""
    property theProduct : ""
    
end script

¢ bind the table columns to the array controller key paths masterProduct and theProduct
¢ populate the table this way, the example write the digits 1 - 4 into the masterProduct column


repeat with i from 1 to 4
    set newProduct to current application's ASProduct's alloc()'s init()
    newProduct's setMasterProduct:(i as string)
    arrayController's addObject:newProduct
end repeat

¢ Now comes the magic: To change a value of the theProduct column just change the property in the ASProduct instance.
You need only the line index


set indexOfLineTwo to 1 -- Cocoa's indexes are zero-based
-- get the second object from the array controller
set productAtLineTwo to arrayController's arrangedObjects's objectAtIndex:indexOfLineTwo
-- change the property in the object. The binding updates the table
set productAtLineTwo's theProduct to "IN STOCK"

THANK YOU StefanK!

I actually have it built as you suggested :wink:

I was just trying to adjust the values completely wrong!

The following has saved my life!

set productAtLineTwo to arrayController’s arrangedObjects’s objectAtIndex:indexOfLineTwo
set productAtLineTwo’s theProduct to “IN STOCK”

Taking this a step further I am attempting to now integrate images in, instead of “In Stock”

Following along with ShaneStanelys AppleScriptObjc Explored “loading images from the bundle”
I am having trouble getting it to work within my class ASProduct.
I was hoping to initially set all of theProduct column to the redCircle.png
Then instead of marking something with text as “IN STOCK” having that show a greenCircle.jpg

Within my ASProduct


repeat with i from 1 to 4
   set newProduct to current application's ASProduct's alloc()'s init()
   newProduct's setMasterProduct:(i as string)
   newProduct's setTheProduct:redCircle
   arrayController's addObject:newProduct
end repeat

Shanes Example has it all in the main app delegate and calls

set my redCircle to current application's NSImage's imageNamed:"RedCircle.png"

within the applicationWillFinishLaunching handler.

How do I combine the two functionally?

very easy, add this handler to the ASProduct script and bind the value of the image cell to key path productImage


    on productImage()
        return current application's NSImage's imageNamed:theProduct
    end

the property theProduct must contain the literal string of the image name,
maybe you could even omit the file extension


newProduct's setTheProduct:"RedCircle.png"

I agree with Stefan that the OO approach is the nicest, but I would caution that it is fragile if you have a lot of entries. More than a couple of hundred can easily generate an AppleScript Internal table overflow error.