Re: Can AppleScript Include script files?
Re: Can AppleScript Include script files?
- Subject: Re: Can AppleScript Include script files?
- From: has <email@hidden>
- Date: Mon, 10 Jan 2005 16:04:25 +0000
Steve wrote:
I've got a small contract to do some work on some OS 9 applescripts
for a client. They have about 3 dozen scripts that all have one big
chunk of code in them that is identical. Ideally this could be an
included script file or something along those lines, but the problem
is that the code creates about 40 variables that are needed in each
file.
Tip: large numbers of global variables are often a sign that your
code isn't very well structured (which would present a problem for
creating a good modular design). As a rough rule of thumb: if code is
so so complex that you can't easily keep track of what's going on,
restructure and simplify it till you can. Globals may be reduced by
[e.g.] reorganising/restructuring handlers to reduce their dependency
on globals and/or using more powerful organisational techniques such
as object-oriented programming (which AppleScript is actually quite
good at, even though very few folk need/bother to use it).
How can I go about using the same code to create the variables and
have them available within the current script?
Short answer: you can't. The AppleScript language has no C-style
'include' option (which is logical given its object-centric nature),
and no 'import' statement a-la Perl, Python, Java, etc. (which is
incredibly annoying) that'd allow you to do something like 'from
MyLib import *'.
Now, I _think_ Script Debugger has the ability to merge code from
multiple sources into a single script a-la C-style includes, but I
don't use SD myself so you'd need to check that one. To be honest
though, smooshing code directly together like that can cause more
problems than it solves (name collisions, etc.).
I've tried run script file, passing the hundred or so parameters and
I get back a list of the variable values, but I still have to set
all the variables again in each script. Not much help.
Absolutely wouldn't go there if I were you.
I've tried load script and put the common code into that file and
again, no luck.
This is the usual way to do it: load the library script into a
property and access its properties and handlers there, e.g.:
property _MyLib : load script (alias "path:to:script")
_MyLib's someVar
_MyLib's doSomething()
This approach avoids any nasty namespace collisions and unwanted
interactions as long as you avoid declaring global variables in any
of your script (bad, as they go everywhere and will be a nightmare to
track) and stick to using handlers and properties only.
You can either use an ad-hoc, compile-time binding approach like the
above, or if you need more powerful, dynamic library management then
AppleMods' Loader system <http://applemods.sourceforge.net/> might be
worth looking into.[1][2]
HTH
has
[1] With one caveat: I promised Gary a couple months ago to write up
the developer docs for Loader, but still haven't gotten around to it
yet (other projects demanding my attention, seasonal distractions,
etc.:p); there is some interim documentation on my site if you're
interested though.
[2] If you're new to modular design and OOP, you may need to read
around a bit outside AS to learn the basic theory. For AS-specific
examples, AppleMods should be a very good place to look: there's
stuff there ranging from very basic up to relatively complicated,
most of the code is pretty solid, and many libraries have sample
scripts included.
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden