Re: a "repeat" before a "on idle"
Re: a "repeat" before a "on idle"
- Subject: Re: a "repeat" before a "on idle"
- From: Paul Berkowitz <email@hidden>
- Date: Sat, 31 Aug 2002 23:15:52 -0700
On 8/31/02 8:45 PM, "Antonio Inojal" <email@hidden> wrote:
>
Hello. I am currently making a test on a script which is idle for 5
>
seconds but which repeats nevertheless. I have found that a script which
>
begins with on idle will end as soon as it hits "end idle". Maybe if you
>
make the script an application which stays open, it will continue but ,
>
and correct me if I am wrong, variable data will be lost. ( To
>
illustrate this look at the code below and see that "i" is first given a
>
value of 5, then 7. What I would like is that when the script repeats in
>
the beginning, "i" still has a value of 7) Now, when I place a repeat
>
statement before all other source, and below it I place an 'on idle' ,
>
it gives me some error about it waiting for the end of repeat and
>
encountered on idle:
Values that are set in a handler (including 'on idle') are not retained at
all, and in any case, a regular variable - even in a main script, is going
to be reset again before it can be re-used. Otherwise it would error the
first time through a script since it would be undefined. As soon as you
define it - which you must do to use it the first time - it will re-define
back to that value on every other occasion.
That is (primarily) what script properties are for. You just need to declare
a property at the top of the script, and when you change it in the script
run it will retain the changed value. So it's usually best to initialize to
an empty value such as "", 0 or missing value, and only set it to some other
value the first time - unless the progress of the script changes it later
on. And what's the Finder doing there? Are you using the Finder for anything
in your script?
property i : 0
on idle
activate -- just for these display dialogs if needed
if i = 0 then set i to 5
display dialog "First view of i: " & (i as string)
-- do some stuff, which may:
set i to i + 2
beep -- why beep? what did it tell you?
display dialog "Second view: " & (i as string)
return 5
end idle
You'll see that the second time through the idle handler, it starts out at
7, not 5. (For this particular script, you could just set i to 5 in the
property - no need to start at 0. But often you do need a method such as
above.)
--
Paul Berkowitz
_______________________________________________
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.