Re: Newbie Question: Script Objects
Re: Newbie Question: Script Objects
- Subject: Re: Newbie Question: Script Objects
- From: Simon Brown <email@hidden>
- Date: Mon, 17 Feb 2003 00:22:52 +0000
>
From: Andrew Oliver <email@hidden>
>
Properties in any script are persistent until the script is recompiled. The
>
property's current value is automatically saved by AppleScript as soon as
>
the script closes.
You're right, I could use global variables, but I was trying to save a line
of code for each declaration...i.e.
global x
set x to false
as opposed to
property x: false
My assumption was based on this snippet from the AppleScript Language
Guide.pdf (p318) Which I couldn't find before but have since re-read the
whole chapter to confirm since your email:
script Joe
property currentCount : 0
on increment()
set currentCount to currentCount + 1
return currentCount
end increment
end script
tell Joe to increment() --result: 1
tell Joe to increment() --result: 2
When it encounters the identifier currentCount at any level of the script
object Joe, AppleScript associates it with the same identifier declared at
the top level of the script object. The value of the property currentCount
persists until you reinitialize the script object by running the script
again.
------ End snippet from the language guide.
Which seemed to suggest that properties declared within a script object
rather than within a script only persisted for the life of this particular
instance of the script object, i.e. Only while the parent script is open.
I tested the above script and it does in fact reset the property value every
time the script is closed and then reopened, thought not when the script is
simply re-run without closing it and opening it again. Hope that makes sense
to you, because I'm still not sure I understand where and when to use script
object and exactly where to place them within my scripts.
>
I've never noticed this before, but looking through some of my scripts it
>
seems that I've always tended to include script objects before I use them.
>
Whether this is by coincidence or design I couldn't tell you now.
I don't understand why AS can 'look ahead' for handlers but not for script
objects. I'd spent hours writing the script object in isolation from the
main script I was intending to use it in and was mystified that when I put
it into the main script it would only run when called from within the run
handler but errored saying "variable [script object name] is not defined"
when called from within the idle handler (or anywhere apart from the run
handler). I suspect it has something to do with initialising the script
object, though as in the example I posted previously, it had already been
called once from within the run handler and so should therefore be
initialised, at least for the complete duration of the run handler.
Thanks for your response. The learning curve is a bit steep for me at the
moment. Maybe I'll just have to bite the bullet and go buy AS in a nutshell!
Best regards
Simon Brown
_______________________________________________
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.