So i’ve changed back to a single selection, then am appending to the array using the below:
tell theArrayController to addObject:selectedPKGPath
(oh & if you’re looking theData has been changed to additionalPKGs)
on selectPKG_(sender)
try
choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder)
set my selectedPKGPath to POSIX path of result
log selectedPKGPath
tell theArrayController to addObject:selectedPKGPath
log "done"
log additionalPKGs
tell standardUserDefaults() of current application's NSUserDefaults
setObject_forKey_(additionalPKGs, "additionalPKGs")
end tell
log "updated plist"
end try
end selectPKG_
The problem is that, after you delete an item, what gets saved back to theData is a mutable array, not a list, so “theData & selectedPKGPath” makes no sense. You could probably use:
set my theData to (theData as list) & selectedPKGPath
But using addObject: on the array controller is probably a better way to go anyway.
Thanks so much for the help so far with creating TableViews.
But, i’m stumped again…
Basically, i want to run the below to check if a file exists… If it doesn’t then display a dialog advising such (<< this is working) then change the text colour of the row/object which has the path of the missing item.
My attempt below, changes the colour of the whole table… Please enlighten me.
on testButton_(sender)
repeat with theItem in additionalPKGs
try
-- Check for file
do shell script "ls " & quoted form of theItem
log "Found " & theItem
on error
log theItem & " is missing"
display dialog "Cannot Find: " & additionalPKGs & ". Do you wish to proceed?" with icon 2 buttons {"No", "Yes"}
-- Set missing items text to red.
set my selectedPKGsColor to current application's NSColor's redColor()
end try
end repeat
end testButton_
For more context, the whole of this POC script is below:
property parent : class "NSObject"
property additionalPKGs : {} -- the main property; array controller binds its content array to this
-- IBOutlets
property |window| : missing value
property theArrayController : missing value
property selectedPKGPath : missing value
property defaults : missing value
property testValue : missing value
property certsAdded : 0
property certsValue : missing value
property selectedRow : missing value
property selectedObjects : missing value
property textColor : missing value
--property NSColor : class "NSColor" of current application
property selectedPKGsColor : missing value
on applicationWillFinishLaunching:aNotification
-- populate plist file with defaults (will not overwrite non-default settings))
regDefaults_(me)
-- retrieve plist values
retrieveDefaults_(me)
end applicationWillFinishLaunching:
on regDefaults_(sender)
tell current application's NSUserDefaults to set defaults to standardUserDefaults()
tell defaults to registerDefaults_({additionalPKGs:additionalPKGs}) as list
end regDefaults_
-- Get values from plist
on retrieveDefaults_(sender)
tell defaults to set my additionalPKGs to objectForKey_("additionalPKGs") as list
end retrieveDefaults_
on selectPKG_(sender)
try
choose file of type {"com.apple.installer-package-archive"} with prompt "Select a .pkg to add:" default location (path to desktop folder)
set my selectedPKGPath to POSIX path of result
log selectedPKGPath
tell theArrayController to addObject:selectedPKGPath
log "done"
log additionalPKGs
tell standardUserDefaults() of current application's NSUserDefaults
setObject_forKey_(additionalPKGs, "additionalPKGs")
end tell
log "updated plist"
end try
end selectPKG_
on deletePKG_(sender)
tell theArrayController to set my selectedObjects to selectedObjects()
log selectedObjects
tell theArrayController to removeObjects:selectedObjects
log "done"
log additionalPKGs
tell standardUserDefaults() of current application's NSUserDefaults
setObject_forKey_(additionalPKGs, "additionalPKGs")
end tell
log "updated plist"
end deletePKG_
on testButton_(sender)
repeat with theItem in additionalPKGs
try
-- Check for file
do shell script "ls " & quoted form of theItem
log "Found " & theItem
on error
log theItem & " is missing"
display dialog "Cannot Find: " & additionalPKGs & ". Do you wish to proceed?" with icon 2 buttons {"No", "Yes"}
-- Set missing items text to red.
set my selectedPKGsColor to current application's NSColor's redColor()
end try
end repeat
end testButton_
on applicationShouldTerminate:sender
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate:
If you want to do anything other than a simple single-column table with no frills, you have to provide a list of records rather than a simple list of strings. That’s what all the multi-column examples in my book use. Then you can have a value in the records that contains a color, and bind the text color property of the column to it.
So your records might be something like:
{thePath:“/blah/blah/blah”, theColor:textColor}
Once you get the bindings set up, you would change an entry’s color by changing the the value of its theColor property.
Shane… i guess you mean something like (which you had posted earlier):
But, when I bind “theColor” to Text Color of the Text Field Cell in the Table View, i either get uncaught exception errors… or weirdly, the Table Views contents change to sections of my screen.
As an alternative, perhaps I could add a column with icons. Green for ok, Red for not.
Would that be easier than trying to change the texts colour? (which seems to be more of a PITA than it should be).
Does textColor always contain an NSColor? It soundd like it doesn’t.
That’s what I’d do. How many tables do you see that change the color of an entry? I can’t think of any off-hand. It’s not standard UI, and you will often find it’s harder to do non-standard things.
The property was being declared as missing value, so I guess I need to figure out how to declare it as black.
When trying: property selectedPKGsColour :
"NSCalibratedRGBColorSpace 1 0 0 1"
i get the error:
This works:
property selectedPKGsColour : missing value
on applicationWillFinishLaunching:aNotification
set my selectedPKGsColour to current application's NSColor's blackColor()
log selectedPKGsColour
-- populate plist file with defaults (will not overwrite non-default settings))
regDefaults_(me)
-- retrieve plist values
retrieveDefaults_(me)
end applicationWillFinishLaunching:
But when changing the colour later using the below, all text in the tableview changes to red. How do i target a row?
set my selectedPKGsColour to current application's NSColor's redColor()
So you set the theColor property (or whatever you call it) of every item to current application’s NSColor’s blackColor() (or a property containing same. Then when you want one to appear in red, you set it’s theColor property to current application’s NSColor’s redColor().
As Shane mentioned before, the database of a table view is an array/list of dictionaries/records of the same type or an array/list of custom class objects.
A column is bound to a specific property. For example with this array of dictionaries
{
{thePath:"/blah/blah/blah", theColor:blackColor},
{thePath:"/foo/foo/foo", theColor:redColor}
}
if you bind “thePath” to the value and the “theColor” to the text color of the text field of column 1 the table view will look like
/blah/blah/blah /foo/foo/foo
so to change the color of a specific row you have to change the value of the respective “theColor” property in the array