Print Page | Close Window

Apple Script

Printed From: One Stop Testing
Category: Quality Assurance @ OneStopTesting
Forum Name: Software Process Improvement
Forum Discription: It includes lots of process oriented things like requirements engineering, risk management, software peer reviews, project management, metrics, and process assessment etc.
URL: http://forum.onestoptesting.com/forum_posts.asp?TID=387
Printed Date: 29Dec2024 at 7:40pm


Topic: Apple Script
Posted By: vidhya
Subject: Apple Script
Date Posted: 29Mar2007 at 3:04am
What is Apple Script?

AppleScript is a scripting language devised by Apple Computer, and built into Mac OS. More generally, AppleScript is the word used to designate the Mac OS scripting interface, which is meant to operate in parallel with the graphical user interface.

AppleScript was designed to be used primarily as a scripting language, offering users an intelligent mechanism to control and exchange information with various applications.

Prior to System 7, the Mac OS application runtime had only a rudimentary event model that could specify a small and fixed number of low-level events like "key was pressed" or "mouse was clicked". Each application was responsible for decoding these low-level events into meaningful high-level user actions, like "select cut from the Edit menu". In many cases, the code for reading the event and decoding it was mixed together; for instance, the code handling a mouse click might decode it to selecting the Quit item from the File menu, and then quit the application immediately.

Adding AppleScript support required the author to fully separate this decoding from carrying out the command, a task Apple referred to as factoring (...the application). Application developers were encouraged to write two complete event handling "stacks", one for handling the low-level events (clicks, etc.), and another for high-level events (AppleEvents). The actual work code that handled these commands, once decoded, was to be completely separated and called identically from both stacks.

AppleScript in Mac OS X
In Mac OS X AppleScript is simpler for developers to implement, particularly for those applications being developed in Cocoa. Unlike the Mac OS where events are handled by the applications, under Cocoa, events are decoded into a "high level" command by the NSApplication object, and the messages dispatched directly to the correct object. That is, all Cocoa applications are "factored" by default; the developer doesn't write any of the event handling code (normally) and writes only the "work methods" that those events will call.

Another major advantage is that Cocoa objects are presented to the outside world (other applications and even machines) in a standardized format that anyone can examine directly. Under Cocoa AppleScript is much "thinner"; the script engine decodes the script, translates object names from human-readable to their internal format, and then calls those methods on the target application directly.

The natural language metaphor
Whereas Apple Events are a way to send messages into applications, AppleScript is a particular language designed to send Apple Events. In keeping with the Mac OS tradition of ease-of-use, the AppleScript language is designed on the natural language metaphor, just as the graphical user interface is designed on the desktop metaphor. AppleScript programs are generally readable by anyone, and editable by most. The language is based largely on HyperCard's HyperTalk language, extended to refer not only to the HyperCard world of cards and stacks, but also theoretically to any document. To this end, the AppleScript team introduced the AppleEvent Object Model (AEOM), which specified the objects any particular application "knew".

Generally, AEOM defined a number of objects, like "document" or "paragraph", and the actions that could be done to them, like "cut" and "close". The system also defined ways to refer to properties of objects, so one could refer to the "third paragraph of the document 'Good Day'", or the "color of the last word of the front window". AEOM uses an application dictionary to associate the Apple Events with human-readable terms, allowing the translation back and forth between human-readable AppleScript and bytecode Apple Events. To discover what elements of a program are scriptable, dictionaries for supported applications may be viewed. (In the Xcode and Script Editor applications, this is under File → Open Dictionary.)

Examples:
Code:
 tell application "QuarkXPress"
      tell document 1
         tell page 2
            tell text box 1
              set word 5 to "Apple"
             end tell
         end tell
      end tell
  end tell


Code:
pixel 7 of row 3 of TIFF image "my bitmap"


AppleScript Studio
With Mac OS X, AppleScript has grown well beyond its humble beginnings. AppleScript Studio is a development environment, which comes free with Mac OS X, which uses AppleScript as the primary programming language, in conjunction with the Cocoa framework used to construct graphical user interfaces.

With Mac OS X v10.3 ("Panther") and Mac OS X v10.4 ("Tiger"), AppleScript Studio and Project Builder have been rolled into the Xcode IDE. Interface Builder, another component of Xcode, lets you build a user interface in a drag-and-drop fashion (similar to Visual Basic) and then "run" the user interface to see what the forms and menus looks like.

Panther and Tiger also come with an enhanced version of Script Editor, the once minimalist editor for compiling and running AppleScripts. One new feature of this editor is that if you right-click (or control-click) on the editing area, you get a pop-up menu with a large range of options for script fragments to paste into your script. This is an excellent feature for people learning to write AppleScript. From that menu, you can also open up the directory where these scripts are kept, and have a look at them. You can also add your own scripts (although you need to restart Script Editor for these changes to show up in the pop up menu).


-------------
MBA Examination papers



Print Page | Close Window