Re: Passing variables between scripts
Re: Passing variables between scripts
- Subject: Re: Passing variables between scripts
- From: email@hidden
- Date: Tue, 12 Dec 2000 15:43:10 -0500
On 12 Dec 2000 12:35:38 -0500, email@hidden asked,
>
I have created a series of small helper scripts, each performing
>
a very specific task, that I would like to call via a job-specific
>
script which would call only those helper scripts that are needed.
>
(This should make maintaining the helper scripts a whole lot easier
>
in the long run.) Optimally, the main, job-specific script would allow
>
the user to select a folder containing the files to be processed and
>
that folder location variable would then be passed along to each
>
helper script so the user only has to identify the directory once. A
>
sample "main" script follows:
>
>
global MyFolder
Danger, Will Robinson! Global Variable! Danger!
But a global variable isn't powerful enough, since it only exists throughout the
script. You need something with even larger scope than one program. Or more
appropriately, you need to explicitly pass the parameter from one script to
another.
>
set MyFolder to choose folder with prompt ,
>
"Please select the folder containing the files to process."
>
>
tell application "Finder"
>
activate
You don't need the Finder. One script can talk directly to another.
>
with timeout of 600 seconds
>
select file "UppercaseFirstLetter" of folder "To Process" of
startup disk
>
open selection
>
end timeout
Change this to
with timeout of 600 seconds
tell application "UppercaseFirstLetter" to open {MyFolder}
end timeout
If you write the interface this way, you can make your helper scripts droplets,
and drop a folder on them. That way you can test them independently, and use
them for their small task if you need them. Note that {MyFolder} is a list
containing one alias. This is how the Finder passes the list of files to a
droplet. To be most useful, the helper script should do the right thing for a
list containing one folder, for list with one file, or for a list with many
items, folders or files or both.
If more generally, if you want to pass a variable between scripts, write your
own handler and give the hander an argument as the value to pass in.
-- Script helper
to flaminate from x to y
-- code here to do the action
return 42 -- return a value if you want
end flaminate
-- Main script
tell application "Helper" to flaminate from 10 to 15
if result < 0 then -- use the result if you returned one.
tell application "Helper" to quit -- when you are done with it.
One important thing to keep in mind when using custom handlers (that is,
something other than Run or Open): Save your helper as a "stay open"
application. Your "tell" block will send a "run" command to the helper if it
isn't already running. If the script isn't stay open, it will process the "run"
command, and then exit, not waiting around for the "flaminate" (or whatever)
command that follows.
When you are done using the helper, tell it to quit if you don't want it hanging
around.
If you want, you can pack your helpers all into one script file, with different
handlers to do different tasks.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden