In our last blog "Introduction to AppleScript", we discussed what is apple script and how it's useful for Mac users. Now it's time to learn how to write the AppleScript.
You can see, AppleScript Editor window divided into three part. Lets understand what each each part indicate.
Top Part :
Top part contain four buttons. As their name show they are use to compile, build, record and run the script.
Middle Part:
Middle part contain the text field, where we write our script. When you start writing, the colour of text is normal. After you are done entering the code, click on the "Compile" or "Run" button from top. If your syntax is correct, your code will automatically format and colorize. If you have made an error, Script Editor will highlight the problematic area and give you a message about what it thinks might have gone wrong.
You can get the information of colours displayed in your compiled code by going to Script Editor -->Preferences --> Formatting. You can change the default colours and use your own colours.
Bottom Part:
Bottom of your Script Editor window is like a debug log. Once you run the script, bottom event log tells you what the result was, what event is happening.
For example:
tell application "Safari" to close every window
or
The "tell application...." means that the computer tells an app to do something. The part after "ApplicationName" is what the app will do. Here it is "close every window". End tell means it will end the tell block and quit.
say command:
The say command is something that makes your Mac talk!
Syntax: say "Rupesh"
"Rupesh" is what your Mac will say. If you run the script, you will hear a manly voice saying the word. If you type something like "Rueewssaart", Mac voice will say it letter by letter.
For example, How you pass a message to display a chart with text "Hello Rupesh" from person A to B. Here Person A is your system & person B is application you want to communicate using script to display chart with some text.
You personally meet to person A so, no need to indicate person A in message. Now pass the message in daily communication english language.
You probably say this to Person A:
tell the person "B" to display the chart "Hello rupesh"
Yes, you wrote a script. You just need to convert it in technical terms. Technically, Person is an application, chart is an dialog. So, your message will looks like this:
tell application "Finder" to display dialog " Hello Rupesh"
Copy this and paste in your script editor textField area. Now compile & then run the script. You will see a dialog will open with "Hello Rupesh".
Now, use your smartness and create your own script with more application like, mail, safari,textEdit etc. In our next blog we will learn about variables, operators & some more useful command in AppleScript.
Getting Started with AppleScript:
To create an AppleScript, open the application "AppleScript Editor" from your application folder (Application --> utilities --> AppleScript Editor ). A simple window containing a large text field with a strip of buttons along the top.Top Part :
Top part contain four buttons. As their name show they are use to compile, build, record and run the script.
Middle Part:
Middle part contain the text field, where we write our script. When you start writing, the colour of text is normal. After you are done entering the code, click on the "Compile" or "Run" button from top. If your syntax is correct, your code will automatically format and colorize. If you have made an error, Script Editor will highlight the problematic area and give you a message about what it thinks might have gone wrong.
You can get the information of colours displayed in your compiled code by going to Script Editor -->Preferences --> Formatting. You can change the default colours and use your own colours.
Bottom Part:
Bottom of your Script Editor window is like a debug log. Once you run the script, bottom event log tells you what the result was, what event is happening.
Now, Its time to learn some basic command
tell Block:
The tell tells an app to do something. Most commands in AppleScript are located inside a "tell block". It's called a "tell block" because you are "telling" a given application what you want it to do.For example:
tell application "Safari" to close every window
or
tell application "Safari"
close every window
end tell
The "tell application...." means that the computer tells an app to do something. The part after "ApplicationName" is what the app will do. Here it is "close every window". End tell means it will end the tell block and quit.
say command:
The say command is something that makes your Mac talk!
Syntax: say "Rupesh"
"Rupesh" is what your Mac will say. If you run the script, you will hear a manly voice saying the word. If you type something like "Rueewssaart", Mac voice will say it letter by letter.
Creating "Hello Rupesh" script:
As, I said it's very much easier and you have no need some extra programming skills for it. You have to think like you are telling a person A to pass the message of some task to another person B in normal english.For example, How you pass a message to display a chart with text "Hello Rupesh" from person A to B. Here Person A is your system & person B is application you want to communicate using script to display chart with some text.
You personally meet to person A so, no need to indicate person A in message. Now pass the message in daily communication english language.
You probably say this to Person A:
tell the person "B" to display the chart "Hello rupesh"
Yes, you wrote a script. You just need to convert it in technical terms. Technically, Person is an application, chart is an dialog. So, your message will looks like this:
tell application "Finder" to display dialog " Hello Rupesh"
Copy this and paste in your script editor textField area. Now compile & then run the script. You will see a dialog will open with "Hello Rupesh".
Now, use your smartness and create your own script with more application like, mail, safari,textEdit etc. In our next blog we will learn about variables, operators & some more useful command in AppleScript.