| MICRO CENTER: COMPUTERS AND ELECTRONICS | ||
![]() |
![]() |
|
| Tech Take-Apart Coding Your Mac by kp |
||
|
Last month, I explored the power of OS X's Automator application. This program makes custom scripting very easy with its pre-loaded functions and drag-n-drop interface. Now, I am going to investigate how to develop your own AppleScripts and outline features of XCode, Apple's application tool. Within Mac OS X there are several tools available to build AppleScripts. For this example, I am going to use the Script Editor which is found in Applications -> AppleScript -> Script Editor. Before writing the script, here is a quick tour around the Script Editor interface.
The first thing that you see is the editing window that has three parts. At the top is a list of buttons - Record, Stop, Run and Compile. Record can create corresponding AppleScript as you do actions, Stop halts the script's process, Run begins the script, and Compile debugs and converts code. You can add additional buttons to the menu by going to View -> Customize toolbar. There are a few other settings you may want to change before starting to code. Under the Preferences menu, there are options for editing and formatting. For editing, it is better to have the" Wrap Lines" and "Use Script Assistant" checked for help while composing scripts. In the Formatting menu, you can set custom defaults to mark keywords, comments, variables, etc. These settings come in handy when you compile your code, and the specific elements have their own identifying features. Other important sections of Script Editor are the Composing Window and the Description, Result and Event Log tabs. The Composing Window is where you type in your AppleScript. The Description tab allows you to type in details about your script, the Result tab shows the answers or calculations for operations, and the Event Log keeps track of the script's actions. Another important menu is the Library window (Window -> Library) which shows a list of all AppleScript-compatible applications. This menu gives you a set of commands for each application. If an application is not listed, you can test to see if it can use AppleScript by dragging the application icon onto the Script Editor icon. If the application is compatible, the Library opens automatically, if not, a warning message displays. Now that you are orientated with the Script Editor application, it is time to learn a few simple AppleScript commands. To demonstrate, I've created a simple script using some common commands. The "tell application" statement is used to direct a command to an application; in this case the Finder. Next, I want to show a dialog box with text, so I indented the command "display dialog". After the dialog box displays and "OK" is selected, a second command is set in motion to open Safari. The "activate" command brings the Safari window forward and opens the specified URL with the "open location" command. To finish, I closed each "tell" command with "end tell". If you have the Script Assistant on, Script Editor can help you compose your code while you write, and in addition, the compiler corrects mistakes or displays a warning. When in doubt, use the Library search feature to look up the definition and function of a command. --activate this script in the startup items To use the script for yourself, copy and paste the above text into Script Editor and modify the name and web URL location. Next, save the script with the .scpt extension. Finally, activate this script as part of the startup items through your user account. Go to System Preferences -> Accounts -> Your Account -> LogIn Items, then click the plus (+) to attach the script. Don't forget to check the item to activate upon startup. Some good references to start with Xcode are Mac OS X Developer’s Guide and Mac OS X Command Line: Unix Under the Hood. Both of these books have plenty of step-by-step instructions and code examples to get you programming fast. Until next month, happy scripting! Shop Online: References: |
Get
Random Access |
|
© Micro Center |
||