I am trying to have one script call another script and pass a parm then have the called script return 3 variables.
I don’t seem to be ab le to get it right. I can remove the parm’s from both scripts and the calling script will call the called script. Maybe someone can give me some help in getting the code to work.
Thanks for any help or suggestions
Roger
– Calling script
set myType to “BK”
set myCount to “”
set myData to {}
run script file “Macintosh HD 2:RGS:RGSLoader.scpt” {myType}
return {myType, myCount, myData}
– Called script (RGSLoader)
on run {myType}
– Load data for citation type
set myCount to “000”
set myData to {}
– Set values for each record
if myType = “BK” then
set myRec001 to {"(BK) ", “001”, "Page Number ", “Page Number:”, ", “, “attribute”, “N”, “Next”}
copy myRec001 to end of myData
set myCount to myCount + 1
set myRec002 to {”(BK) ", “002”, "Person of Interest ", “Person of Interest:”, ", “, “attribute”, “Y”, “Next”}
copy myRec002 to end of myData
set myCount to myCount + 1
set myRec003 to {”(BK) ", “003”, "(recorded ", “Date:”, “)”, “Date”, “Y”, “Done”}
copy myRec003 to end of myData
set myCount to myCount + 1
end if
return {myType, myCount, myData}
end run
What you want to do should be as simple as this in your calling script ” that is, you set your three variables to the values returned by the called script:
set myType to "BK"
set {myType, myCount, myData} to (run script file "Macintosh HD 2:RGS:RGSLoader.scpt" with parameters {myType})
In the past, running another script from a file used to stop the calling script at that point, so I haven’t done it for years. But it may be OK now. If not, you can load the other script into the calling script before running it:
set myType to "BK"
set RGSLoader to (load script file "Macintosh HD 2:RGS:RGSLoader.scpt")
set {myType, myCount, myData} to (run script RGSLoader with parameters {myType})
For safety, don’t follow McUsr’s advice about globals.
He used the same variable names in both scripts. But the called script returns the relevant values to the calling script anyway (which is good practice), so there’s no need for the variables to be global. And if there’s no need for globals, it’s safer not to have them.
Yes, a much better advice would have been to declare them local, because as they stand, they are implicit globals anyway with the same scope as explicit globals.
I’ve just tested that assertion and it turns out that even when the variables are explicitly declared global in both scripts, those in one can’t be accessed from the other. So the whole topic’s a non-starter! :lol:
I didn’t assert it, but I am sure I saw something like that work a while a go, I’ll look into it.
(That was the time I started thinking about Universals, and not Globals.
Try this:
property tlvl : me
script myscript
global myvar
set myvar to 5
tell tlvl's myOtherScript to run
log myvar
end script
script myOtherScript
global myvar
log myvar
set myvar to 6
end script
tell myscript to run
It doesn’t work in one script file, if the variables are implicitly declared.
So Nigel is totally right. So much for assuming, again.
Edit if one script is loaded from the other, the thing with declaring global, works.
So an implict global, is not a totally Global, since a declared global at least practically reaches further. (Between scripts, provided the global is declared before calling a script where it isn’t declared.