Passing data script to script

Wondering if anyone has an example of how to pass info from one script to another ... using only Applescript. I know it can be done (think I have examples at home, but don't have access to them). Thanks!

Posted on Nov 15, 2005 12:29 PM

Reply
13 replies

Nov 15, 2005 12:39 PM in response to Gabe Ripley

Is this what you're thinking of? I have one script named 'test.scpt' saved to the desktop, it looks like this:

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">on run {msg}
display dialog msg
end run</pre>

Then, in a separate Script Editor document, I execute this code:

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">run script (path to desktop as string) & "test.scpt" with parameters {"hello world"}</pre>

The saved script file executes and displays a dialog containing the variable from the first script.

Nov 15, 2005 12:57 PM in response to Gabe Ripley

That's a compile error. Double-check your syntax. Script Editor should highlight the character(s) in the script that's having trouble. If you can't work out what's wrong you could paste that line (or as much of the script that's relevant) here for suggestions. This error also happens sometimes when there is a syntax problem in a target script that is not compiled (plain text).

Nov 15, 2005 1:58 PM in response to Gabe Ripley

Ah, that makes sense because the run script command can also be used to run plain text passed to it directly.

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">run script "display dialog \"hello world\""

run script file ((path to desktop as string) & "test.scpt") with parameters {"hello world"}

run script "hd1:Users:michaelhenley:Desktop:test.scpt" with parameters {"hello world"}</pre>

It must attempt to run the text as a script first, but before throwing an error checks to see if it's a valid path to a script file. Very smart.

Nov 30, 2005 6:15 PM in response to Michael Henley

This really is not a way to pass data from script to script. What it really does is incorporate the lines of code in the script called by the "run script" command into the calling script. You can see this by using a script property in the object script. It will not be modified after the script's lines have been executed in the calling script. Control is never passed to the called script but remains with the original script, which executes the code.

Nov 30, 2005 6:35 PM in response to Thomas Camilleri

That's true, but this command is still useful for scripters who merely want to better control how they organize and run their scripts, without having to wrap their heads around object oriented programming. I personally use this technique to edit the idle handlers of always-on script applications without restarting them. If a scripter needs to get and set persistent properties all up and down the chain, they ought to check the real deal OO.

Nov 30, 2005 7:54 PM in response to Thomas Camilleri

Here is an example of how to run a script file such that its properties persist between runs, and independent of the calling script.

We save this code as a compiled script on the desktop:

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">property x : 0
set x to x + 1</pre>Then we run the following code in Script Editor multiple times. On each successive run, the number in the results window will increase by one:

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">set test_path to (path to desktop as string) & "test.scpt"
set test_scpt to load script file test_path
run test_scpt
set the_result to test_scpt's x
store script test_scpt in file test_path replacing yes
get the_result</pre>

Mac OS X (10.4.3)

Dec 1, 2005 12:53 AM in response to Michael Henley

Thanks Michael. Interesting technique, but not yet quite what I'm looking for.

This technique loads the script with property x into the calling script as a script object; it still doesn't pass control to the called script. The behavior seems to be that the modified property value of the loaded script object is (1) not saved in the test.scpt file and (2) not loaded into the calling script. If test.scpt is run from the script editor 10 times, the property x value is incremented each time, finally reaching 10. However, after running it 10 times and then running the calling script once, the "x" property of the script object in the calling script is 1 rather than 11, indicating that the property value that is loaded is the initial declared value of property x. Conversely, if after running the calling script one runs "test.scpt" again, the next value of property x will be 11, indicating that the last value of property x of the script object in the calling script (which in this example is 1) has not been saved to file "test.scpt".

Only the code itself, and therefore the initial values of properties, is passed between script files with load and save operations. The script-modified values of properties remain in the files where the modifications took place.

BTW, how do you get those links to scripts in your posts?

Dec 1, 2005 1:11 AM in response to Thomas Camilleri

The Script Editor has deceived you. Properties persist, but as soon as you do anything to their parents in Script Editor, they will be reset to the default value. To demonstrate this, add say x to the end of test.scpt and save it as a script application. Then change test.scpt to test.app in the calling script. After calling it a few times, locate test.app in Finder and double click to launch it. It will speak the expected number, and when you run the calling script again, it will jump however many ticks test.app advanced itself.

Thank you for asking about my script links, you're the first! I started a thread about it here:
http://discussions.apple.com/thread.jspa?threadID=133520

You can get the latest and greatest version from my website:
http://homepage.mac.com/mike.com/AS.html

Note that with the recent Apple Discussions system upgrade there is no longer a 4000 character limit, so you can ignore the warning that pops up. I'm actually not sure what the limit is these days, but other boards that I post to have a 4000 character limit, so that's why I left the warning in there along with its various fat-trimming options. (:

PowerMac G5 (June 2004) 2x1.8GHz 1.25GB, PowerBook G4 (12-inch DVI) 1x1GHz 768MB Mac OS X (10.4.3)

Dec 2, 2005 7:47 AM in response to Michael Henley

Thanks, just what I wanted! Using the last example, with the script compiled as an application and loading it, would something like this work?

set theScript to load script (reference to script)
run script theScript with parameters {x,y}


I took a quick look at your links script but couldn't make much sense out of it, since I know nothing about shell scripting. When I have some time I'll try to figure it out.

Dec 2, 2005 2:52 PM in response to Thomas Camilleri

No, not the way you want. As we've seen before, the run script command will not preserve properties in the called script. You can, however, tell my_scpt to run or run my_scpt. In oo we don't need to pass parameters to the script in the run call, because once the script is loaded we have complete access to get and set its properties at any time. Before running the loaded script, issue something like set x of my_scpt to 10 or set my_scpt's x to 10.

PowerMac G5 (June 2004) 2x1.8GHz 1.25GB, PowerBook G4 (12-inch DVI) 1x1GHz 768MB Mac OS X (10.4.3)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Passing data script to script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.