Call script with parm

I am very new to applescript so be kind to me.:slight_smile:

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

Hello and welcome to MacScripters.

You are not that far away from having a working solution!

You should really look into the Applescript Language Guide.

First of all, you must load the script before you run it.


set mscr to load script ("Hfs:path:to:my:script" as alias )

You need to specify that you are passing parameters to the run handler.

tell mscr to run with parameters { your parameters }

you should really specify the three variables you use as global, in both scripts!

Hth

Hi Roger. An echo to McUsr’s welcome.

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.

It seemed to me that he used the same variables in both scripts.

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. :slight_smile:

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. :smiley:

(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

Of course it works in one script file, but the OP is talking about two files,

It doesn’t work in one script file, if the variables are implicitly declared. :expressionless:

So Nigel is totally right. So much for assuming, again. :frowning:

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.

Thanks to all for your help

I got the call to work and got the parms passed but have another problem which i will post seperately

Roger