I am building a small app that will display a grid of buttons.
I would like to load these buttons with a random values for there names (later to be images).
(In this scenario let’s assume I have 9 buttons. 3 rows of 3.)
I can get the buttons to load individually. However, I need to have the names on the buttons display randomly. Getting the random data is not the issue. I can load these into an array and pass them the “old school” way through delegate, or a this new way (since I just learned this) through an Array Controller.
Approach 1:
These 9 buttons are displayed on a tab. I have created my own custom NSButton that contains its own values and methods for handling displaying the data.
How can I loop through all of the buttons on the tab, and what if there are multiple tabs?
Approach 2:
I also recently placed these 9 buttons onto a NSMatrix. I noticed the buttons now have a class of NSButtonCell? How can loop through all of the buttons in the matrix and access the button class itself.
I can get the count of cells here:
on awakeFromNib()
try
set cellArray to testMatrix's cells
set cellCount to count of cellArray
on error e
display dialog e
end try
end awakeFromNib
Before I had just looped through all of the buttons in applescript, now I am trying to move to ApplescriptObjC
Thanks
What do you mean exactly by “loop through all of the buttons”? What are you doing with them exactly?
To make it easier here was the original code that would load the data into each button on a tab.
on LoadButtons(tabIndex)
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to rowSeparator & return
set buttonCount to count of buttonData ---delimited data Loaded previously
repeat with i from 1 to buttonCount
set b to item i of buttonData
set AppleScript's text item delimiters to columnSeparator
try
set buttonName to "Button" & tabIndex & "_" & (text item 3 of b)
set buttonID to (text item 1 of b)
set currentTab to tabIndex as text
set title of button buttonName of view of tab view item currentTab of tab view "MatchTabs" of window "MatchGame" to (text item 2 of b)
set imageName to WORKINGFOLDER & buttonID & ".png"
set imageFile to POSIX file imageName
if f_exists(imageFile) is true then
set thumbImage to (load image imageName)
set image of button buttonName of view of tab view item currentTab of tab view "MatchTabs" of window "MatchGame" to thumbImage
end if
on error e
display dialog e
end try
set AppleScript's text item delimiters to rowSeparator & return
end repeat
set AppleScript's text item delimiters to oldDelimiters
end LoadButtons
I wanted to avoid creating a delegate (or reference?) to each button in ApplescriptObjC.
In the old code I could generate the buttonName on the fly and then reference it.
Just to be clear, delegates and references are entirely different things. The term you’re looking for is outlets.
If you don’t want an outlet for each button, you can make an outlet for the tab view, use subviews() to get a list of its subviews, and iterate through that list.
Thanks Shane.
Here is what I just tested using the Matrix and it worked perfectly.
repeat with x from 0 to 3
repeat with y from 0 to 3
set myCell to current application's NSButtonCell's alloc()'s init()
set button to (testMatrix's cellAtRow_column_(x, y))
set newTitle to GetTitleRowX_ColumnY_(x,y)
button's setTitle_(newTitle)
end repeat
end repeat
Thanks for the clarity on Outlets. (I have ventured into ObjC and Applescript from the .net realm)