O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Podcasting Hacks
By Jack Herrington
August 2005
More Info

HACK
#54
Build a Simple Sound Cart for Macintosh
Use Xcode on Macintosh to build a sound cart application with AppleScript Studio
[Discuss (0) | Link to this hack]

Having sample sounds close at hand is important for podcasters. Using AppleScript Studio, you can quickly build a standalone application with whatever interface you want to control your recording application (e.g., Audio Hijack Pro ) and play your samples at the touch of a button.

First I start with Xcode, which is available to Apple Developer Connection members (online membership is free) from http://developer.apple.com/tools/download/, and quite possibly on your Mac's hard drive in /Applications/Installers/Xcode Tools. With Xcode, I create a new AppleScript Application project using the New Project command. For this example, I'll use the name SampleCart for the project.

Now I want to build the interface, so I double-click the MainMenu.nib file, which launches Interface Builder (shown in ). Using the controls from the Cocoa-Controls palette, I drag three buttons onto the window and change their titles to Start Recording, Stop Recording, and Sound 1, respectively. Start Recording will start Audio Hijack Pro's recording, Stop Recording will stop it, and Sound 1 will play a sample sound.

Next I select Show Info from the Tools menu. This brings up the information palette, which tells me all about the windows and the buttons. I click the first Start Record button and change the info palette, now named NSButton Info, to display the AppleScript section. Then I select the clicked item under the Action section and select SimpleCart.applescript down in the script section.

Figure 1. Editing the cart application's UI in Interface Builder

This links the clicking of the button to the AppleScript file. I do that with all three buttons. I also give each one a unique name. For this example, I called them startRecording, stopRecording, and sound1, respectively.

By clicking the Edit Script button, I am taken back to Xcode to write the script for these buttons.

       on clicked theObject
	   if the name of theObject is "startRecording" then
               tell application "Audio Hijack Pro"
                   start hijacking session named "bc" 
                   start recording session named "bc"
               end tell
           end if
           if the name of theObject is "stopRecording" then
               tell application "Audio Hijack Pro"
                   stop recording session named "bc"
                   stop hijacking session named "bc"

               end tell
           end if
       end clicked

The preceding code is the click event handler for the script. It takes as an argument the button that was clicked, called theObject. I get the name of the button and then either start or stop recording, depending on what was clicked.

Next I use the Build and Run command under the Build menu to run the application. When it starts, I see Audio Hijack Pro opening up as well. The application knows that I will be using it, so it launches Audio Hijack Pro just in case.

I click the Start and Stop Recording buttons just to make sure they work. If they don't work, I probably have not selected the click event or the AppleScript file in the Info palette. Or the names that I used for the buttons vary from the names I put in the script file.

If I want to check the name of the button coming into the script file, I put this code at the beginning of the on clicked function:

       display dialog the name of theObject as string

That will pop up a dialog with the name of the button before the rest of the script runs.

Now I need to support the Sound 1 button, which means first getting a sound. Really any sound will do. I call mine testsound. aif. First I copy the sound file into the project directory. Then I click the SampleCart item in the project and select Add Files…from the Project menu. From here, I select the sound file. When it asks me if I want to add the file to all the projects, I just click OK.

With the sound file in the project, I can add this code to the on click handler:

       if the name of theObject is "sound1" then
           set theSound to load sound "testsound.aif"
           play theSound
       end if

Wow. That was easy. This loads the sound file from the resources folder into a variable called theSound and then plays the sound. I run the application again, and lo and behold, clicking the Sound 1 button plays the sound. But there is a slight delay, and that's a problem.

What I would like to do is have the sound file loaded when the program starts up and then just play the copy loaded in memory when I hit the button. So, I need to go back to Interface Builder, click the main window, and select the awake from nibevent from the Nib section of the AppleScript items in the Info window. Then I assign that to the SimpleCart.applescript file and select Edit Script again.

Now I have a new event handler called awake from nib where I put this code:

       on awake from nib theObject
           set theSound to load sound "testsound.aif"
       end awake from nib

Those with a keen eye will realize that I'm setting a local variable in this function. So, how do I get the variable theSound to show up in the click handler? I define it as a property at the top of the file, like this:

       property theSound : ""

Then I change the click handler to play theSound:

       if the name of theObject is "sound1" then
           play theSound
       end if

Now when I run the program again, the sound plays instantly when I hit the button.

The complete script looks like this:

       property theSound : ""

       on clicked theObject
           if the name of theObject is "startRecording" then
               tell application "Audio Hijack Pro"
                   start hijacking session named "bc"
                   start recording session named "bc"
               end tell
           end if
           if the name of theObject is "stopRecording" then
               tell application "Audio Hijack Pro"
                   stop recording session named "bc"
                   stop hijacking session named "bc"
               end tell
           end if
           if the name of theObject is "sound1" then
               play theSound
           end if
       end clicked

       on awake from nib theObject
           set theSound to load sound "testsound.aif"
       end awake from nib

See also: <ul/>


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.