Advertisement

AppleScript: saving scripts

Just as there are multiple ways to save Automator workflows, there are also multiple ways to save AppleScripts. In this how-to, I will show you ways in which you can save your AppleScripts for future use.

Read on for all the details.



Saving as a Script
You can save an AppleScript as an actual script file by going to File > Save; and choose "Script" from the file format drop-down box. When you save your script as the ".scpt" file, you will be able to open it back up at any time and continue work on it (similar to a text document) using whatever script editor you like.




Saving as an Application (.app)
You can also save your scripts as an application file (or .app file) so that you may double-click on them to run the script; just like a real application. To do this, go to File > Save; and choose "Application" from the drop-down menu. You may notice that there is also an "Application Bundle" format. If you have ever right-clicked on an application and chosen "Show Package Contents," then you have an idea of what this option does. It will allow you to have a file structure for your Application (this is known as a application fork).



What's with the options?
The "Run Only" option allows you to protect your scripts when you are distributing them; it compiles (or converts your script into something that only the computer can read) your script so that no one can open and edit it. Please note, this can be over come by a decompiler (however, I have no knowledge of an AppleScript decompiler at this time).

The "Stay Open" option is only available under certain formats (mainly the application and application bundle formats); and allows the script to stay open after it runs. This is only used under certain circumstances, mainly those which require an idle state.

The "Startup Screen" allows the scripter to give details about what the application will do and allow the user to either quit the script or go ahead and run it. The details about the application are set by using a description field.

Let's build an Application!
Now that I've told you a little about saving AppleScripts, let's build a script and save it as an application. We'll build a script that tells the Finder to display a dialog asking to continue and then use one of the system events that I showed you earlier. Type the script exactly as shown:

tell application "Finder" to display dialog "Would you like to Logout?"

tell application "System Events" to log out

Once you have it typed, let's save the application. Go to File > Save; choose "Application" from the file format drop-down box. When you have it saved, double-click on it's icon. The application will run and display a Finder dialog box asking you "Would you like to Logout?" if you click "OK" it will logout the current user, and if you click "Cancel," it will stop the script and quit the application.



Congratulations, you've just made your first AppleScript application!

[Update:] One of our readers, Nik, reminded me that when you save an AppleScript as an "Application" only, it will NOT be saved as a universal application, instead, it will be formated as a PowerPC app. However, if you use the "Application bundle" format, you will save your AppleScript as a universal application (meaning it can run on both PPC & Intel Macs).