• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to do a goto with Applescript?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to do a goto with Applescript?


  • Subject: Re: How to do a goto with Applescript?
  • From: "Mark J. Reed" <email@hidden>
  • Date: Tue, 11 Jan 2011 16:02:35 -0500

If you're just counting, there's no reason not to use i as your loop
control variable, moving onto j,k for nested loops.  If you get more
than three loops deep you might want to think about more descriptive
names, but really, doing things like "repeat with anIndex from ..." is
just make-work on the typing front.  The use of i for indexes has a
long, proud mathematical tradition. :)

As far as other issues in variable names, there's not much in the way
of universal agreement.  The "g for global" prefix thing is a subset
of Hungarian notation, whose adherents seem to be mostly (but not
exclusively) Microsoft developers (by which I mean folks developing
for Microsoft platforms, not just those employed by Redmond).

I prefer to indicate globalness by using all-caps names, myself.  But
it's a matter of taste, along with whether you do words_like_this or
wordsLikeThis (or words-like-this in languages that allow hyphens in
identifiers, or |words like this| in AppleScript, ...), whether you
capitalize variables (in languages that allow you to), and so on.

I feel Alex was being overly prescriptive, but his point about the
importance of clarity stands, especially when you get into longer
programs - and at almost 600 lines, I'd say setBounds qualifies.
Still, I'd say the main issue with that particular script is that it's
pretty much a giant table lookup, coded as a long sequence of if
statements only because AppleScript doesn't have a table data type
(hash/dictionary/associative array/map/whatever).

I do think it would be clearer if the main logic were isolated better
from the app-specific logic.  If I were writing it, I would maybe
define a script object for each application that's responsible for all
the stuff specific to that application.  Make them all have a handler
of some specific name (I used "configure" below), to which you pass in
a record containing the bounds, and then it alters that record as
needed and does whatever extra app-specific stuff it needs to do, and
return a success/failure flag (so the main handler can decide what to
do about those).

Like this:

script HandleAcrobatPro
  to configure (appName, newBounds)
    tell application appName
      if exists «class PdWd» 1 then
        tell newBounds
          set leftX to 0
          set rightX to 1330
          set bottomY to bottomY - 20 -- stay a little more  off
bottom of screen
          set bounds of «class PdWd» 1 to {leftX, topY, rightX, bottomY}
        end
        return true
      else
        return false
      end if
    end tell
  end configure
end script

script HandleFinder
...
end script

script HandleMail
...
end script

Then you could put the if/then logic in a relatively small handler
(here's where a lookup table would come in handy):

to lookupHandler(appName)
     if appName contains "Finder" then
        return HandleFinder
     else if appName contains "Acrobat" then
       return HandleAcrobatPro
     else
      ...
     end
end

And then the main body would look something like this:

tell application "Finder" to set {leftSide, topSide, newR, newB} to
bounds of window of desktop

set newBounds to { leftX: 20, topY: 22, rightX: newR, bottomY: newB }

set appName to short name of (info for (path to frontmost application))

set handlerScript to lookupHandler(appName)

tell handlerScript to configure(appName, newBounds)

Anyway, just a thought.  What's important is to do whatever works for
you - or if you're working on a team of developers, whatever the team
has agreed to :) - so that you can go back and read your own code a
long time after writing it and still be able to understand it.
 _______________________________________________
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

  • Follow-Ups:
    • Re: How to do a goto with Applescript?
      • From: Alex Zavatone <email@hidden>
    • Re: How to do a goto with Applescript?
      • From: "Stockly, Ed" <email@hidden>
References: 
 >Re: How to do a goto with Applescript? (From: "Stockly, Ed" <email@hidden>)
 >Re: How to do a goto with Applescript? (From: Skeeve <email@hidden>)

  • Prev by Date: Mount and move
  • Next by Date: Re: Mount and move
  • Previous by thread: Re: How to do a goto with Applescript?
  • Next by thread: Re: How to do a goto with Applescript?
  • Index(es):
    • Date
    • Thread