You are not logged in.
Over the past several months, we have been discussing command handlers and subroutine handlers in detail. This month, we will begin discussing a new topic ... AppleScript Studio. As we proceed, you will find that we will be using handlers regularly, especially command handlers, as AppleScript Studio makes heavy use of them. If you'd like to refer back to my previous columns on handlers, you can do so here... (Part 1, Part 2, and Part 3).
What is AppleScript Studio
When developers first hear the name AppleScript Studio, they often mistake it for an actual application. However, this is not the case. AppleScript Studio is actually a subset of features in two much more comprehensive applications, Xcode and Interface Builder. These applications are installed as part of the Mac OS X Xcode Developer Tools package, which comes on the Mac OS X installation CD. Since Apple updates the Xcode Developer Tools fairly regularly, the best way to gain access to updates is to become a member of the Apple Developer Connection. There are multiple levels of membership available, and the online level is free.
Using AppleScript Studio, developers can create AppleScript-based applications, complete with interfaces that look and feel just like any other Mac OS X application. The concept is fairly straightforward. You begin by creating an Xcode project based on a template. This project contains, among other things, an interface component and a code component. Next, you design the interface for your project in Interface Builder, adding buttons, text fields, progress bars, and any other desired elements. You then specify events to which your interface elements will respond by initiating code within the Xcode project. For example, you might want specific code to trigger when a user clicks a button, when a user enters text into a text field, or when a window is displayed on screen. Once events have been assigned within your interface, you add AppleScript code within the Xcode project to execute when those events take place.
Let's walk through this process now. We will create a very basic AppleScript Studio project that will resize an image using information specified by a user via an interface. In future columns, we will continue to expand on the steps that we will discuss here.
If you have not yet installed the Xcode Developer Tools, you will need to do so if you intend to follow along with this tutorial.
Creating an AppleScript Studio Project
Begin by launching the Xcode application (/Developer/Applications/Xcode), and then selecting New Project from the File menu. Upon doing so, you will be presented with an Assistant window, which will allow you to select from a list of pre-existing project templates provided by Apple. Select the AppleScript Application template, located in the Application category, and click the Next button to proceed.
Applescript:
global theImage
on clicked theObject
-- Handle if the "Resize" button was clicked
if name of theObject = "resize" then
-- Retrieve the resize percentage amount
set theNewSize to (contents of text field "size" of window 1) as integer
-- Determine a path in which to save the resized image
tell application "Finder"
set theImageName to name of theImage
set theImageFolder to (folder of theImage) as string
end tell
set theResizedImagePath to theImageFolder & "Resized-" & theImageName as string
-- Open the image, resize it, and save it as a new file
tell application "Image Events"
launch
set theImage to open theImage
tell theImage
scale to size theNewSize
save in theResizedImagePath
close
end tell
end tell
end if
-- Quit the application
quit
end clicked
on awake from nib theObject
-- Prompt the user to select a file
set theImage to choose file with prompt "Please select an image file:" without invisibles
end awake from nib
Building and Running the Project
Now, we are ready to test our project. To build and run the project from within Xcode, click the Build and Go button in the project window's toolbar. If all goes well, the project should launch as an application, and you should be prompted to select an image to be processed. Navigate to and select an image. Next, the interface should be displayed. Enter the desired number of pixels into the text file in the interface, and click the Resize button. The script should then resize the image using Image Events, and output it into the same directory as the original image. Please note that you won't actually see the image open as this process is performed, as Image Events is a background application.
Our AppleScript Studio project is currently in Debug mode. Once you have tested the project, and are ready to begin using it or distributing it to others, you will need to change it to release mode. To do this, select Release from the Project > Set Active Build Configuration menu, and then click the Build and Go button in the project window's toolbar again. The project will now be rebuilt in release mode. You will find the final built application in the /Build/Release folder within the main project folder.
In Conclusion
You should now have at least a basic understanding of how AppleScript Studio works. Obviously, we have truly only scratched the surface here, and there is much, much more to learn. We will continue to discuss AppleScript Studio in future columns, but you are encouraged to begin exploring it further on your own. As you do so, be sure to check out the fully editable example projects included in the /Developer/Examples/AppleScript Studio folder, as well as the AppleScript Studio documentation that can be found in the Apple Developer Connection reference library (accessible online or via the Help > Documentation menu in Xcode). If you get stuck, you can also post your questions to MacScripter's AppleScript Studio forum.
You are also welcome to download the demonstration project discussed in this column. You can find it here.
Until next time... just script it!
-Ben Waldie
Ben Waldie, Automated Workflows, LLC
AppleScript & Automator Tips & Articles - http://www.automatedworkflows.com/tips/tips.html
Mac Automation Made Simple Video Podcast Series - http://www.automatedworkflows.com/tips/podcast.html
Automator for Mac OS X Visual QuickStart Guide - http://www.automatedworkflows.com/books/books.html
Offline