Looping through a Matrix

I’m new to AppleScript (and new to the Macs to be honest) but I do quite a bit of scripting for Windows systems administration. I’m trying to build an application that has a matrix of radio buttons. The selected button is saved to a plist. When the application is launched again, that value is retrieved from the plist. I want to loop through a matrix and select the correct item in the matrix for display to the user.

I have the loop code working, but I’m stumped on something that I would think would be fairly simple.

How can I get the total number of items (rows) in the matrix (as defined by the rows property in Interface Builder) to control my repeat loop?

My repeat loop is fairly simple:

repeat with i from 1 to imtxPCTypeNum
  if the title of the cell i of the matrix "x" of the window "w" is equal to "value" then
    set current row of matrix "x" of the window "w" to i
  end if
end repeat

But how do I get imtxPCTypeNum?


Hi danshap,

Something like this:

tell matrix “radio” of window “main”
set c to count cells
end tell

But, you don’t need your repeat loop. By default, the tags are set in a matrix 0, 1, 2, etc… So you can do something like this:

tell matrix “radio” of window “main”
set i to (tag of first cell whose title is “Radio2”)
set current row to (i + 1)
end tell


Thanks kel. That worked great. I do appreciate the response.

Looks like I spoke too soon.

The code above works wonderfully if I choose either of the first 2 options in the matrix. However, if I select any other option, the tag always returns as 0. Therefore, I always end up selecting the first entry in the matrix.

Using “count cells” with the logic in the repeat loop works however.

Hi dnashap,

I was thinking that might happen when you add new buttons. You can set the tags in IB or by script.