Tuesday 4 August 2015

AppleScript: Variables, Operators and useful command


In our last blogs "Introduction to AppleScript" and "Getting Started with Apple Script", we discussed what is apple script, how it's useful for Mac users & how to write some basic script. Now it's time to go in deep and learn how to use variable, operators and more useful command.

Variables, Operators and useful command


Declaring Variables:

Variables are basically a way to store information within your script. You can, for example, do a calculation and save the result in a variable.It allows you to access that information later without doing the calculation again.

In AppleScript variables are declared using the "set" and "to" commands. set is the keyword that you use to define a variable. The syntax goes like this:

Format:     set variableName to "variableValueOrData"

Example:   set userFullName to "Rupesh Kumar"

By doing this you are setting your variable name. Many programming languages require that you state the type of variable you want in the declaration (integer, floating point, text, etc.). AppleScript however, is intelligent enough to work with your variables without any instruction about the format.

The most common data types used in AppleScript are numbers, string or text, lists,boolean,records.

string: Stores a string of characters of unlimited length. You define a string by bracketing a quantity between double-quotes:

set myString to "hello world"

You must escape the double-quote and the backslash with backslash.
set myString to "escape \" and \\ with \\"
AppleScript defines several constants of class string: return, space and tab.

boolean: true or false

integer: Stores integers in the range -536870912 .. 536870911

real: Stores (in double-precision) a real number in the range ±10^308.

date: Stores a date.

list: Stores a list of any quantities. You define a list by bracketing its items between braces.the syntax for defining a list variable goes like this: {data,data}. Text has quotes around it as usual. You can retrieve a particular list item by using the item number of listVariable syntax. The first item of the list is item 1. You can add things to the end of a list by using the concatenation operation (with the &).

set names to {"Ritesh", "Rupesh", "Amit"}
log item 2 of names
set names to names & "Rohit"
log names

record - stores a list of key-value pairs. you can access the record using it key.

set the_record to {firstname:"Rupesh", lastname:"Kumar"}
log lastname of the_record
-- kumar


How to add comments in apple Script:

In AppleScript, comment is a text inside the editor that it is ignored by the script and only there for humans to read. Comments are used to annotate the code and remind us what each operation, variable ,func, logic does.

You can add comment in applescript by adding the “—-”. For example:

Syntax:
script code —-Your comment

set name to "Rupesh" ---here we set the text Rupesh to variable name.

The gray text you see in the end of line after —- is the comment.


Some Useful Commands:

activate command:
Brings an application to the front, and opens it if it is on the local computer and not already running.

activate application "TextEdit"
OR
tell application "TextEdit" to activate


launch command:
Launches an application, if it is not already running, but does not send it a run command.
If an application is already running, sending it a launch command has no effect.

launch application "TextEdit"
or
tell application "TextEdit" to launch

display notification command:

Posts a notification using the Notification Center, containing a title, subtitle, and explanation, and optionally playing a sound.

Parameters
text:The body text of the notification. At least one of this and the title must be specified.
with title: The title of the notification. At least one of this and the body text must be specified.
subtitle text: The subtitle of the notification.
sound name text: The name of a sound to play when the notification appears. This may be the base name of any sound installed in Library/Sounds.text

Example:

display notification "It's time to go for lunch" with title "Rupesh Script classes" subtitle "Lunch Time"

copy command:
Copies one or more values, storing the result in one or more variables. This command only copies AppleScript values, not application-defined objects.

The copy command may be used to assign new values to existing variables, or to define new variables.
The copy is a “deep” copy, so sub-objects, such as lists within lists, are also copied.

set Fname to "Rupesh "
set Lname to "Kumar"
copy Fname to Lname
display dialog Fname & Lname

delay command
Waits for a specified number of seconds.
example

tell application “Finder” to activate
delay 2.0
end tell

Operator in AppleScript:

Arithmetic operators: Arithmetic operation can be applied on numbers only.

  + operator  example: log numberA + numberB —-Addition
  - operator  example: log numberA - numberB —-subtraction
  * operator  example: log numberA * numberB —-multiplication
  / operator  example: log numberA / numberB —-division (result will be a real)
  ^ operator  example: log numberA + numberB —- raise a number to a power
div operator  example: log numberA div numberB —- Division (Result will be an integer)

Relational operators: Relational operators can be applied for both number and string. In string operators work on the bases of string length. Only is equal to(=) operator can be applied on any data type & compare the inside data.

 > operator     example: log numberA > numberB       -- is greater than
 < operator     example: log "Rupesh" < "Rupe"          --is less than (False: based on string length)
  >= operator  example: log numberA >= numberB    --is greater than or equal to
  <= operator  example: log "Rupesh" <= "rupesh"     --is less than or equal to (based on string length)

equal, is not equal to:
We mostly use equal, is not equal to operators for numbers, list, record, and text comparison.lets see how it’s work for them in apple script.

numbers: 
Two number are equal if their real value is equal.

Example: log 55 = 55.1 -- result will be false
                log 55 = 55.0  -- result will be true
list:
Two lists are equal if they both contain the same number of items and if the value of an item in one list is identical to the value of the item at the corresponding position in the other list:

{ 1, 2, “Hi” } = {1, 2, “bye”} --result: false

record:
Two records are equal if they both contain the same collection of properties and if the values of properties with the same label are equal. The order in which properties are listed does not affect equality.

{ name:”Rupesh”, age:”24” } = { age:”24”, name:”Rupesh”}  —-result : true

Text:
Two text objects are equal if they are both the same series of characters. They are not equal if they are different series of characters.

"RUpesh" is equal to "rupesh" --result: true

considering case
"RUpesh" is equal to "rupesh"

end considering

concatenation( &):
The process of combining two pieces of text is called a concatenation operation. In AppleScript, you do this with the ampersand symbol. In appleScript & (concatenation) operation be done with string,list,record.

Example:

String concatenation:

set textA to "Hello"
set textB to " Apple"
log textA & textB

list concatenation

set names to {"Ritesh", "Rupesh", "Amit"}
log item 2 of names
set names to names & "Rohit"
log names

Same you can do with record also.

Where, I can found more command:
AppleScript itself has a wide range of commands that can be applied to any program or item in OS X. For each application there are manuals for how to communicate with those applications through AppleScript. These manuals are called "Dictionaries".

To view a dictionary, go to File>Open Dictionary in Script Editor. Scroll down the list of applications, click on any application and hit "OK".  For example, I select iTunes app and click on “OK”. You should see the following window:





The column on the left contains the available "Suites" of commands and items. When you click on a suite, you'll see everything contained in the suite displayed below.




Suites contain commands (C with a circle) and classes (C with a square), classes contain properties (P) and elements (E).



Now, use your smartness and create your own script.