Re: Learning Apple Script
Re: Learning Apple Script
- Subject: Re: Learning Apple Script
- From: has <email@hidden>
- Date: Tue, 30 Apr 2002 22:17:56 +0100
Greg Reyna wrote:
>
A couple of years ago I devoted a bit of time to learning, but it
>
didn't pan out (my time, I mean...) I have some experience with
>
scripting ARexx, a "Basic", non-OOP language, so I'm not completely
>
approaching this from square one; but at times it feels like I am...
AppleScript supports both procedural and OO programming practices. Most
ASers never even use OOP, so don't let the issue faze you. If you've
already worked with another language then you should aready be familiar
with variables, conditionals, loops and subroutines. You just have to take
that existing knowledge and apply it to AS particular syntax.
The only major concept you'll want to wrap your head around is application
object models: trying to learn this stuff by looking at something like the
Finder dictionary and code examples alone is pretty whiffy, so you really
want to find a good text on the topic.
>
First off, I got a couple of errors so I looked all over but couldn't
>
find a list of error messages and their numbers. Why is that?
A compilation error means your script is syntactically incorrect.
The AppleScript Language Guide (ASLG) contains a list of error codes
(though not scripting addition or application errors, as those are outside
the scope of a language guide). You can download the ASLG as a PDF from
Apple's site.It's a pretty comprehensive reference, ideal when you need to
look up more information on a particulae language feature (it's not a
teaching guide, however, so stick with another text for that).
>
choose file "Locate the file "Offset Document":" of type "QUIL"
>
>
[the error dialog reported-> "Expected end of line, etc. but found
>
class name."]
>
>
--I don't understand... (I got rid of everything after "choose
>
file", and the script worked fine, but what happened?)
Your code is sytactically wrong, therefore the script editor couldn't
compile it.
AppleScript's rather expansive use of keywords means that error messages
can sometimes be a bit misleading: "blah-blah...but found class name" means
it's seen the word 'Offset' outside your quotes [Paul's already explained
where you went wrong with these] and assumed it be be the 'offset' keyword,
which isn't permitted at this point. That vague "Expected...etc." bit in
the message is just a cover-all meaning: "whatever I was expecting to find
this isn't it, so I'm stopping right now".
The best thing to do is often to look for existing examples, and crib off
those.
>
This script is using the "offset" command as a way of doing a search
>
and replace.
Also try digging through the list archives for "TID-based find-and-replace"
- a very common technique amongst ASers. (I have more sophisticated
versions in the stringLib library on my site.)
>
After a line where the script indicates to use its own local
>
subroutine, it has this line to tell what the replace string should
>
be:
>
>
given replaceString: "SuperSubScript Plus"
--------------
on subroutineName given someParameter:foo
display dialog foo
end subroutineName
subroutineName given someParameter:"Hello World"
--------------
>
When I typed this line I mistakenly omitted the colon, but when I
>
went looking for the syntax of the "given" command, I couldn't find
>
it anywhere
See handlers using labelled parameters (p283 of ASLG). Note that labelled
parameters in AS are [IMO] pretty honking (not to mention buggy and
problematic on occasion) - you'd be much better to stick to positional
parameters:
--------------
on subroutineName(foo)
display dialog foo
end subroutineName
subroutineName("Hello World")
--------------
>
Also, when assignments are made within the body of the script are
>
they automatically "exposed" to the internal code of the subroutine?
>
I mean does the subroutine "know" about all the assignments in the
>
rest of the script?
Global variables are; local variables aren't. All variables are locally
scoped unless explicitly declared as global; ie:
global x
property y : "foo"
>
And if so, is there a way to isolate the
>
subroutine so its internal code is separate from the body of the
>
script?
You can make an explicit local variable declaration in your handler
[subroutine] if you have a global variable in the body of your script and a
local variable in your handler, both of which have the same name and which
you don't want to clash. In practice, you're probably better to use
different variable names in the first place, thus avoiding the issue to
begin with (it'll also make code simpler and less confusing to read).
HTH
has
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.