I have a question about running a script object from within another script and getting the result back.
I have a list of variables that I've put into a dialog that returns a record when the checkboxes are selected. Since I don't know the size of the list at the time I'm running the script, I have to create a way to dynamically check each item for selected or not.
I want to build a script object that sets up one test per list item, tests whether it was checked, and returns a list of the checked items.
Here is the code I came up with to test the checked items:
set PR to {dd:"2013-10-08", div5:"0", div2:"0", Client:"My Client", div3:"1", div4:"0", div1:"1", cb:"0"}
set Divisions to {"Division1", "Division2", "Division3", "Division4", "Division5"}
set CountChecked to "on CountChecked(PR,Divisions)" & return & "set DC to {}" & return
repeat with i from 1 to count of Divisions
set CountChecked to CountChecked & "if div" & (i as text) & " of PR = \"1\" then set end of DC to item "
set CountChecked to CountChecked & (i as text) & " of Divisions" & return
end repeat
set CountChecked to CountChecked & "return DC" & return & "end CountChecked" & return
set DivisionChoices to run script CountChecked with parameters {PR, Divisions}
display dialog (count of DivisionChoices) as text -- debug
"Divisions" is the list of items that were displayed in the dialog
"PR" is the record that has one item for each item of Divisions (among other things)
With a list of five items, CountChecked variable looks like this:
on CountChecked(PR,Divisions)
set DC to {}
if div1 of PR = "1" then set end of DC to item 1 of Divisions
if div2 of PR = "1" then set end of DC to item 2 of Divisions
if div3 of PR = "1" then set end of DC to item 3 of Divisions
if div4 of PR = "1" then set end of DC to item 4 of Divisions
if div5 of PR = "1" then set end of DC to item 5 of Divisions
return DC
end CountChecked
When I run the script, I get the error:
"The variable DivisionChoices is not defined." number -2753 from "DivisionChoices"
If I try to save the script to a file first, then load it and run it using the following:
set TmpScript to ((path to desktop) as string) & "CountChecked.scpt"
store script CountChecked in file TmpScript replacing yes
set CountChecked to load script alias TmpScript
I get an Applescript Error:
Can't make "..." into type script
where "..." is the text of the script stored in variable CountChecked.
In Matt Neuburg's "Applescript, The Definitive Guide", he talks about Second-Level Evaluation, on pages 307-308, and has an example where he put a single line of text into a variable, and runs it with the command "set res to run script s", implying that the result of the running of the script is returned in variable "res".
So, my question is, is it possible to build a dynamic script such as this and get it to return a result (in this case a list)?
TIA,
Jim Brandt