Re: Newbie Question: Script Objects
Re: Newbie Question: Script Objects
- Subject: Re: Newbie Question: Script Objects
- From: Andrew Oliver <email@hidden>
- Date: Sun, 16 Feb 2003 15:20:55 -0800
On 2/16/03 9:17 AM, "Simon Brown" <email@hidden> wrote:
>
I'd like to know if my assumption that properties declared at the root level
>
of script objects persist only as long as the parent script app is running
>
and will be reset next time the whole script is executed (if not, there's no
>
point in me trying to use a script object for what I'm trying to achieve).
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. For example:
property runCount : 0 -- reset value to 0 anytime the script is compiled
on doSomething()
set runCount to runCount + 1
display dialog "I've been run " & runCount & " times."
end doSomething
on run
doSomething()
end run
Each time you run this script (including if you quit between runs), the
runCount will increment, and will continue to track the number of times the
script is run until you recompile the script, at which time it resets to 0.
If you don't want the value to be saved between invocations, copy the
property into a global variable first, and use that. Global variables are
reset on each run:
property runCount : 0 -- reset value to 0 anytime the script is compiled
global runCountThisTime
on doSomething()
set runCountThisTime to runCountThisTime + 1
display dialog "I've been run " & runCountThisTime & " times."
end doSomething
on run
copy runCount to runCountThisTime -- copy the property value to a global
repeat 10 times
doSomething()
end repeat
end run
In this case, each time you run the script, it starts with a
runCountThisTime of 0 which increments by 1 each time the doSomething()
handler is called.
>
>
I would also like to know why only one of the following scripts works,
>
dependent on the position of the script object in the script:
>
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.
_______________________________________________
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.