|
TITLE
AppleScript 1.3.1: Common Support Questions
|
Article ID:
Created:
Modified:
|
30615
7/17/98
5/29/01
|
|
TOPIC
This article provides answers to some of the most common AppleScript questions listed below.
What is AppleScript?
What are the new features in AppleScript 1.3?
How can I tell if an application is scriptable?
How can I tell if an application is recordable?
Certain Finder operations aren't scriptable; what options do I have?
How can I tell if AppleScript is properly installed?
I can't get my script to work. How can I troubleshoot it?
What's the best way to learn AppleScript?
Where can I get more help with AppleScript in general?
Where can I get help with one specific scripting problem I'm having in AppleScript?
DISCUSSION
Question:
What is AppleScript?
Answer:
AppleScript is an object-oriented script language built into the Macintosh Operating System, which lets you send commands or messages, called Apple Events, to applications. Using a natural, English-like syntax, users can create scripts to automate complex and/or repetitive tasks, or simplify desktop and network administration.
Applications that support AppleScript are said to be scriptable. Some applications, such as the Finder, are also recordable, which means you can create a script by manually performing and recording actions within that application. Still other applications are said to be attachable, which means that you can attach scripts to application components, such as menus.
The Script Editor, which is installed with the MacOS, is a utility that enables users to create and compile scripts, or save them as applications (called applets). The Script Editor also lets you record actions performed in recordable applications; the resulting script can then be edited, and saved as either a compiled script or an application.
Question:
What are the new features in AppleScript 1.3?
Answer:
AppleScript 1.3, included with Mac OS 8.5, has been completely rewritten in native Power PC code. As a result, scripts may run up to 5 times faster than before.
Other important improvements include:
-
More Finder components are scriptable (ColorSync, Application Switching, Network Setup, Apple System Profiler, Appearance control panel, Apple Menu Options, File Exchange, Location Manager, Help Viewer, Desktop Printing, Disk First Aid)
-
Many commands have been enhanced and features added, such as unit conversions (feet to meters), a delay command, a "give up after x seconds" parameter for display dialog.
-
More Scripting Additions included, and some that previously shipped as separate components have been combined into one, now called "Standard Scripting Addition"
-
A new scripts folder is created within the System Folder as a stable location for scripts
-
ability to attach scripts to folders ("Folder Actions")
-
a Help Guide that provides descriptions of command syntax and examples of their use.
-
See the Help Guide section entitled "What's New in AppleScript" to get more details on all the new features of 1.3.
Question:
How can I tell if an application is scriptable?
Answer:
Any scriptable application or software components (such as control panels or scripting additions) will have a dictionary which contains a list of commands and objects that the application understands, along with a brief description of syntax and parameters. To see if an application has a dictionary, you can try either of the following steps:
-
drag and drop the application over the Script Editor utility; if it contains a dictionary, the Script Editor will open and display it.
-
Open the Script Editor and use the Open Dictionary command (under File) to navigate to the folder which contains your application. Applications and software components that have a dictionary will be visible in the Open dialog box. If you do not see your application in this dialog box, it is not scriptable.
Question:
How can I tell if an application is recordable?
Answer:
To tell if an application is recordable, open the Script Editor, and hit the record button. Then switch to your application and perform some actions. For example, in the Finder (which is recordable), you might create a new folder, give it a name, move it to another location. After performing your actions, return to the Script Editor and select stop [recording] button. If the application is scriptable, you will see the commands you performed entered in the Script Editor window.
Question:
Certain Finder operations aren't scriptable; what options do I have?
Answer:
First, when scripting the Finder, its a good idea to open the Finder dictionary (choose Open Dictionary in Script Editor, then navigate to the Finder) to view what types of commands and objects it understands. Many control panels will have their own dictionary as well, but not all are scriptable. Here are some workarounds that may help you if you need to perform an action on a software component that is not scriptable:
-
Look for a Scripting Addition (aka OSAX). Scripting Additions are like system extensions, in that they add functionality. Some scripting additions are included with AppleScript to provide functionality not in the OS, such as the display dialog function, which allows you to create a dialog box to be presented to the user. Another allows you to set monitor depth (the Monitor & Sounds Control Panel is not scriptable). Explore the scripting additions that are installed as part of AppleScript by reviewing their dictionaries and experimenting with them.
Other Scripting Additions can be found on the Internet; most are freeware and include a read me file of some sort that describes their use. Also, you can open their dictionaries to learn how to use them. See the question below about getting more help with AppleScript for web sites where you can browse for scripting additions.
-
Certain macro-making utilities, such as QuicKeys, are also scriptable. You may be able to record the actions you want to occur in QuicKeys, and then call that QuicKeys script from your AppleScript.
Question:
How can I tell if AppleScript is properly installed?
Answer:
Try using one of the Automated Tasks from under the Apple Menu. Automated tasks are applescripts provided by Apple and installed with the Mac OS. To see if Applescript is working on your machine, you can select a file or folder on your hard disk, and then select the Automated Task "Add an alias to Apple Menu". If this task is successfully completed, then you know AppleScript is working on your machine.
Question:
I can't get my script to work. How can I troubleshoot it?
Answer:
Here are some tips to help you troubleshoot problems you may have when creating your own scripts:
-
If using AppleScript 1.3 which ships as part of Mac OS 8.5, consult the Help Guide for examples of command syntax.
-
Spelling and punctuation are very important; check your script carefully. Be sure that open quotation marks are closed where they should be. A typographical error may cause a command to be considered a variable, resulting in unexpected results.
-
AppleScript considers every new line in your script to be the beginning of a command or statement. If you need to break a long line, using the option-Return key to create a continuation character.
-
Be sure your commands directed to a particular application are in an appropriate "tell" statement. For example, if you want to create a new folder, you have to: tell application "Finder" to make new folder.
-
Test your scripts in the Script Editor with the Event Log and Result windows open. Set the Event log to both Show Events and Show Event Results. Here you will see the result of the script when it is run, which can help you identify problems.
-
If you have problems accessing files or folders, verify the path to that file or folder carefully. If the path is not correct or the folder/file does not exist, you may receive an Access Not Allowed error when trying to access it. Read the section on file references in the Help Guide for assistance.
-
Use parentheses in your commands to indicate the order in which actions should take place. In the example "copy (date string of (current date)) to todaysDate":
- The current date function will be executed first, returning a string of date items.
- Next, AppleScript will get the date string of that result, and -Finally, the variable todaysDate will be set to that date string.
Without the parentheses, you'd get an error when you tried this command.
-
Substitute real values for variables in your script. Problems can sometimes occur when variables contain data of the wrong type (an integer instead of a string) or are not what you think (full file reference instead of file name). To test for this, substitute the actual value for the variable, as you think it should be, and see if the script works then. If so, then investigate how to get the correct data into your variable.
-
Try to isolate the problem code as much as possible. You can either create a new script to perfect the one command you're having problems with, or "comment out" other commands in the script to temporarily keep them out of the picture. You can comment out a line in a script by either placing two dashes (--) at the beginning of the line, or by enclosing the lines within "(*" and "*)". Examples:
-- this line in a script would be a comment.
(*this paragraph within a script would also be considered a comment.*)
-
Use the display dialog command to help you debug your script. This is especially useful if you are trying to debug an applet, and it is impractical to attempt to run the script from the Script Editor. You can use this commands to help you track the execution of script (i.e., display dialog "Now in the repeat loop"), or to display the value of a variable (display dialog "X is now equal to " & x).
-
Use "try" statements for error handling. By placing a command within a "try" statement, AppleScript will attempt to execute the command and if it fails, it will continue through the script. See the Help Guide for examples and syntax for the try statement, and to learn how it can help you see errors being generated.
Question:
What's the best way to learn AppleScript?
Answer:
Regardless of the application you'll be scripting in, most people find that creating your own scripts is easiest when you have some examples to follow. Use the Apple Help Guide to see examples of scripting the Finder. Sometimes it helps to copy the examples EXACTLY, and verify that they work. Then edit them gradually to your own specifications.
When working with third-party applications, check documentation or files installed for examples of scripting that application. You can also check out the company's web site, and search their support databases for guidance.
For a ground-up approach, there are several excellent books on the market that will teach you AppleScript. Look for recently updated editions, as early books did not cover scripting the Finder (because the Finder was not scriptable when AppleScript was first introduced). There are also some excellent resources on the Web, as listed below.
Question:
Where can I get more help with AppleScript in general?
Answer:
For general help with AppleScript, try these resources:
http://applescript.apple.com
- Apple's Web Pages for AppleScript:
http://www.applescriptsourcebook.com
- "The AppleScript Sourcebook" from Bill Cheeseman; includes downloadable scripts, libraries and applets.
http://lists.apple.com/Applescript.html
- Subscribe to AppleScript Mailing List (or search archives):
http://developer.apple.com/techpubs/mac/AppleScriptFind/AppleScriptFind-2.html
- AppleScript Finder Guide, online
.
http://www.wolfram.com/~lou/AppleScript/
- AppleScript Tutorial from Wolfram Research:
Question:
Where can I get help with one specific scripting problem I'm having in AppleScript?
Answer:
Apple Technical Support only offers "up-and-running" support with scripting; they can assist you if AppleScript doesn't seem to be working at all, either because it is not installed properly, or some other condition is causing it to fail. But Apple, and most other software companies, do not offer free assistance to users attempting to write their own scripts. There are other resources for this type of assistance, however:
- the AppleScript mailing list
http://lists.apple.com/Applescript.html
a discussion group which communicates via email to exchange help and ideas with other scripters
- Apple Discussion about AppleScript, a Web-based discussion area for users to help users, is available at
http://discussions.info.apple.com/webx?.ee6ba86
.
Both of these resources involve users helping users. For best results, when composing your question, be sure to include:
-
Specifically what you're trying to do.
-
What application you're scripting (include version).
-
If possible, the script itself or a portion of the script that shows the commands and syntax you're using.
-
What happens when you run the script/how does it fail?
Does it fail to compile?
Does it get to a certain point and fail?
Do you see errors in a dialog box or in the Event log?)
The more detailed you are, the easier it will be for others to offer suggestions that may help solve the problem.
|