Archive for May 12th, 2008

Throw you for a “Loop”

Yesterday I was trying to figure out how to script a loop in AppleScript. I was trying to use an if statement that would keep going until something stopped it. After a few attempts and the script failing I scoured the web searching for ways to make it work. I ended up on Apple’s AppleScript Language Guide where I found the Repeat statement on page 203 [download the PDF version]. I had a duh! moment from my VisualBasic days. “I need a ‘while’ or ‘for’ loop.”

The Repeat statement allows you to repeat a portion of script until you tell it to stop. It can repeat forever if you want or you can have it end if a certain button is clicked. Here is the code. I’m sure this can be cleaned up so it is not as redundant but that is something I will have to learn. Also, I hope to modify this script to allow user input

As stated in the commented out section, the script continues until the “Stop” button is clicked.

if then, else, end if

if then, else statements are a very important part of AppleScripting or any programming for that matter. The statement is relatively simple to construct and are about the only way to compare variables. Say you want to determine if you need a coat for today; depending on the temperature you could make the decision. If the temperature is lower than 50 degrees, you should probably take a coat. Let’s take a look at a simple pseudocode that describes this situation.

The temperature is 67.
if the temperature is lower than 50 degrees
    You should wear a coat
else
    You'll be fine without a coat.
end if

Here is how the statement would look with the variables declared.

Code for theTemperature

This simple applescript can be modified to use ranges rather than a set temperature. For instance, if the temperature is between -200 and 50 degrees and 51 and 200 degrees. The lower and upper range is set to an extreme [at least for IL] so that nothing could be left out.


 

May 2008
S M T W T F S
     
 123
45678910
11121314151617
18192021222324
25262728293031

Top Posts

  • None

Top Clicks

  • None

Recent Comments

Archives