Re: Idle Help
Re: Idle Help
- Subject: Re: Idle Help
- From: email@hidden
- Date: Wed, 6 Dec 2000 16:27:42 -0500
On Wed, 6 Dec 2000 10:56:01 -0700, Byron Peterson <email@hidden> asked,
>
How do I pass variables from the 'run' section to the 'idle' section
>
without using 'global' or 'property' statements.
>
>
ie.
>
>
on run
>
set test to "Hello World!"
>
end run
>
>
on idle
>
display dialog test
>
return 10
>
end idle
This is sort of what the definition of a global variable is. Some languages
have finer-grain control of what goes into and out of different sections of
code, (like Modula-2's import and export directives), but these really work at
the level of modules and not subroutines. In AppleScript terms, that is at the
level of script objects and not handlers. You'll end up using something of
global scope; either a global variable, a property, or a script object with a
property. The challenge is to get as much "globalness" as you need, without
just throwing all the shared data out into the world individually. The approach
from object-oriented design is to package the data and the handlers that
manipulate it together into an object.
If you want two handlers to share that variable, and keep others from getting
access by mistake, you can put the two handlers and the variable together into
the script object, and then call the handlers from the main script. That is,
script package
property message : "(No message set)"
on run
set test to "Hello World!"
end run
on idle
display dialog test
return 10
end idle
end script
on run
tell package to run
end run
on idle
tell package to idle
return result
end
Whether you keep the names "run" and "idle" in the script object, or change them
to something different, is a style matter that should become clear in your
actual script. If the "run" handler is always and only called as part of the
script running, and the "idle" handler is always and only called when the script
is idling, you'd probably be clearest staying with the special names. This
would be the case if the script object were really acting like a little script,
being run and idling in synchronization with the main script. But if the "run"
handler of the script object is really "initialize", and "idle" is really
"displayStatus" (that is, if they have a more important abstract purpose than
just "the thing to do when the script is run"), than use the more descriptive
names.
Another thing you might do is to put a little less total functionality into the
script object, but give it a better-defined and more cohesive purpose. In your
example, it looks like fundamental function that needs the shared data is to
display messages. So make the script object a message-displaying thing.
script messenger
property message : "(No message set)"
on displayMessage()
display dialog message
end idle
end script
on run
tell messenger to set its message to "Hello World!"
end run
on idle
tell messenger to displayMessage()
return 10
end
Nerd sidebar: Object oriented design pedants may say I should provide a "set
data" handler in messenger that looks like this:
to setMessage to newMessage
set message to newMessage
end setMessage
and replace 'tell messenger to set its message to "Hello World!"' with
'"tell messenger to setMessage to "Hello World!"'. But I think the syntax I
used reads better, and fundamentally AppleScript doesn't have any way to hide a
script object's properties from outsiders. But I recognize there are cases
where the only way to change the object's properties is through a handler, which
may do other things besides assigning the new value to the property. But that
handler, I won't name "setSomething". I'll call it by a name that indicates the
more complex job it is doing.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden