Archive for the 'Script' Category

Say Command: Say What You Need To Say

One of the guys I know was messing with other people in the office by SSH’ing into their computer and running a terminal command on their computer that would make it speak. It was funy because the person using the computer did not know the computer could do it and he couldn’t figure out who was doing it.

The say command will make your computer speak anything you type. No quotations are needed. In the terminal app you would type

say Hello World.  How are you today?

I decided to write a simple script to have the user input some text, whatever they want, and have the computer say it. The script is shown below.

Let’s have a simple Dialog

When I first started I wanted a dialog box that popped up saying “Hello World!”. Now I want a simple dialog box to pop up but ask for input. This is actually quite simple. All you have to do is add “Default Answer “”‘ to the display dialog statement.

display dialog “How are you?” Default Answer “Ok…” default answer “”

Here is a simple “What day is it?” dialog example.

The returned result for this example is: {text returned:”Monday”, button returned:”OK”}. Alternatively, you can set the text entered into the dialog box directly to a variable by using the statement below:

set textReturned to the text returned of (display dialog "what
day is it?" default answer "Monday")

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.

Hello World [of course]

The first program everyone should write when first starting out is the famous “Hello World!” program. The program is usually only a few lines, and in the case of applescript, only one line.

To begin, open Script Editor and type: display dialog “Hello World!

Click the green run button at the top of the script editor window


The script is compiled and run just after clicking the “Run” button. Clicking either buttons will terminate the script.


 

November 2009
S M T W T F S
« May    
1234567
891011121314
15161718192021
22232425262728
2930  

Top Posts

  • None

Top Clicks

  • None

Recent Comments

Archives