Re: How to do a goto with Applescript?
Re: How to do a goto with Applescript?
- Subject: Re: How to do a goto with Applescript?
- From: "Mark J. Reed" <email@hidden>
- Date: Wed, 12 Jan 2011 10:33:02 -0500
On Wed, Jan 12, 2011 at 10:16 AM, Robert Poland <email@hidden> wrote:
> 1. In reviewing the list of comments/advice, I get the impression that the use of the "if" statement is hazardous and the use of the "else if" statement should be forbidden.
>
> Am I mistaken?
Yes. There's nothing wrong with "if" or "else if". The problem is
with giant hunks of inline code:
if condition then
-- dozens of lines of code here
else if other_condition then -- else? wait, I forgot what the
original if was checking
-- many more lines of code here
...
else -- completely lost by now
-- more code here
end if
Ideally, what you want is for each clause of the if/else to be only a
few lines. One way to do that is to move the logic into handlers:
if condition then
handleCondition()
else if other_condition then
handleOtherCondition()
...
end
Or into script objects:
to findHandler()
if condition then
return conditionScript
else if other_condition then
return otherConditionScript
...
end if
end findHandler
set handlerObject to findHandler()
tell handlerObject to doSomething();
You also want to cut down on the amount of code by running the same
bit of code multiple times, instead of copying near-duplicates of it
into multiple places in the script.
> 2. Would the creation of multiple data files (one for each monitor) and a handler for each application window instead of using the "else if" statement be as fast or faster to run? I would assume that a data file would have to be read every time the script is run.
Speed is not the issue here. It's AppleScript; speed is not its
forte. The goal of the suggestions is to make the script easier to
understand and customize further, not faster.
This is fundamentally a data-driven application; it would be nice if
you could change a data file instead of having to modify the script
every time you want to add a new app or change the way a particular
app behaves.
> 3. Creation of hard coded data files would also require provisions for modifier keys.
Not sure what you mean by that, but the fact that there was different
code for each app, not just different data values, was part of my
rationale for using script objects instead of a data file.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden