Complicated Dynamic Table Question

Okay. So I have a table in a window in IB. I want to set the number of columns and rows dynamically so that they can be changed by the user before the window is displayed. I was able to do this, but I think its only possible to create columns that contain just text in AppleScript.

I want the entire table to contain image cells, so I searched MacScripter and found this post which gives an objective-c way to do this (and some other things). I think that I’ve implemented it correctly, the directions were clear.

When I then try to show the window (it was not set to be visible at launch), I get “NSImageCell’s object value must be an NSImage.” in the log and an applescript error “NSInternalScriptError (8)”. The window acts really strangly: Before clicking OK on the error dialog box, if I switch to another program and switch back to my program, then the window will show up with the image loaded correctly. The shadow of the window is missing, and the minimize, maximize, and close buttons are colored but don’t respond, even to mouseovers. If I minimize the window using the menu or the shortcut key, it minimizes fine, but when I try restoring it, it restores but it also stays in the dock. At this point, the window is fully responsive, with a shadow, and the table highlights when clicked and everything. And every time I minimize it and restore it again, it leaves another “copy” in the dock.

Also, I only need that objective-c thing to set the width, and to make the cells image cells (no other types of cells). If somebody could modify it, that would be great.

I also want the image cells to have on click event handlers, so that I can change a cell’s image when it’s clicked on. Would there be a way to tell the difference between right clicking and left clicking? (I’d like it to change to one image when its right clicked and a different one when its left clicked).

I’d really appreciate some help with this. If possible, I’d like an AppleScript only solution, but an objective-c solution is okay too. Also, if there is a simpler/easier way of doing this, that would also be great.

So here’s the code


property Rows : 0
property Cols : 0


on launched theObject
	set content of table view "TheTable" of scroll view "TheScroll" of window "TheWindow" to {}
	
	display dialog "How many rows?" default answer "5"
	set Rows to text returned of result
	display dialog "How many columns?" default answer "5"
	set Cols to text returned of result

Setup()
SetCoords(1, 1)  -- For testing we just try with one cell
show window "TheWindow"
end launched


on Setup()
	tell table view "TheTable" of scroll view "TheScroll" of window "TheWindow"
		delete every table column
	end tell
	
	set tempCol to 1
	repeat until tempCol is greater than Cols
		tell table view "TheTable" of scroll view "TheScroll" of window "TheWindow"
			--make new table column at end in table columns with properties {|width|:"10"}
			call method "addColumnWithIdentifier:width:cellType:" of it with parameters {"", 10.0, 2}
		end tell
		set tempCol to tempCol + 1
	end repeat
	
	set tempRow to 1
	set tempCol to 1
	
	repeat until tempCol is greater than Cols
		tell data source of table view "TheTable" of scroll view "TheScroll" of window "TheWindow"
			set theNewCol to make new data column at the end of the data columns
		end tell
		set tempCol to tempCol + 1
	end repeat
	
	
	repeat until tempRow is greater than Rows
		tell data source of table view "TheTable" of scroll view "TheScroll" of window "TheWindow"
			set theNewRow to make new data row at the end of the data rows			
		end tell
		set tempRow to tempRow + 1
	end repeat
end Setup



on SetCoords(theRow, theCol)
	tell data source of table view "TheTable" of scroll view "TheScroll" of window "TheWindow"
		set theRow to data row theRow
		set contents of data cell theCol of theRow to load image (choose file)
	end tell
end SetCoords


Thanks guys