I’m constructing an app that has a couple of matrices of radio buttons in it, which set variables to certain values depending on the button selected. They seem to work fine, but only if they’re actually clicked; if the radio button that’s already selected on the app’s launch is actually the choice I want, and I execute the script without having actually clicked that button (but leaving it selected), the app will return a “the variable x is not defined” error.
I’m wondering if it has to do with the “on clicked theObject” handler that the buttons are currently governed by.
Here’s the code for one of the matrices, being used to define the name of a printer as text (part of a much longer script):
on clicked theObject
tell window “main”
if the name of theObject = “selectPr1” then
if state of theObject is 1 then
set myPrinter to “Printer 1” as text
end if
end tell
else
if the name of theObject = “selectPr2” then
tell window “main”
set myPrinter to “Printer 2” as text
end tell
else
if the name of theObject = “selectPr3” then
tell window “main”
set myPrinter to “Printer 3” as text
end tell
end if
end if
end tell
end clicked
I tried to use the “if state of theObject is 1” line to deal with the variable myPrinter ending up not being defined, but to no avail.
Any help is, as always, appreciated greatly.
Hi! Perhaps you could set myPrinter as an application property an set it’s value inside the on will finish launch handler asociated with the app… that way you can use that variable everywhere else and it would be defined from the very beginning, avoiding non-definitions. Besides, you could save the printer as a property and have the last selection as a default the next time you launch. You’d have to add another handler, but if your interested, me or somebody surely could post it. Or search for it, I found it that way…
A property should help. Also, looking at your code above, there is no need to use the main window tell blocks (and certainly not nested ones). In fact, your code could be simplified to:
property myPrinter : "Printer 1"
on clicked theObject
set object_name to name of theObject as Unicode text
if object_name starts with "selectPr" then
set myPrinter to "Printer " & (character -1 of object_name)
end if
end clicked
This assumes that you have named each individual radio button inside your matrix and attached it to your script. However, it’s easier to not do that but to simply name the matrix itself and then either get the currently selected row or column, depending on how you have setup the matrix (row if the items are listed up and down, column if they are left and right):
property myPrinter : "Printer 1"
on clicked theObject
set object_name to name of theObject as Unicode text
if object_name = "selectPr" then
set myPrinter to "Printer " & (current row of theObject)
end if
end clicked
Thanks very much for the comments. It seems as thought simply setting up a property for the variable “myPrinter” did the trick. However, I will try rewriting the code using your suggestions, as it definitely looks cleaner than the way I wrote it! (I’m not an expert scripter by any means, and seeing how scripters more advanced than I am deal with a problem like this is very helpful.)