Scripting Audio Hijack Pro via AppleScript

Scripting via AppleScript

Beginning with version 2.5, Audio Hijack Pro supports a full AppleScript suite. Just about anything you can do with the graphical user interface, you can now also script. The following is a brief overview of the object model that Audio Hijack Pro exposes. For a complete reference, see Audio Hijack Pro's Scripting Dictionary in Script Editor. If you are new to AppleScript, we recommend you pick up a copy of AppleScript : The Definitive Guide by Matt Neuburg, before proceeding.

  1. Creating a session
  2. Retrieving an existing session
  3. Accessing audio source attributes
  4. Accessing schedule and timer attributes
  5. Accessing recording attributes
  6. Accessing effects attributes
  7. Sending commands
  8. Putting it all together

Creating a session

Just like in the graphical interface, the Session is the main object of interest in the scripting interface. Unlike the graphical interface, each session in the scripting interface has a specific type: application session, radio device session, and system audio session. As should be clear, the type of the session specifies what kind of audio source it uses.

So, to create a new session, we use a standard AppleScript make new ... at ... statement, along with the type of session we want to create:

set appSession to make new application session at end of sessions --For hijacking an application set deviceSession to make new audio device session at end of sessions --For hijacking an audio device set aSession to make new session at end of sessions --Error! The "session" class can't be instantiated!

Note that if you try to instantiate the session class (e.g. make new session), you will get an error. The session class is an abstract base class on which the other concrete subclasses inherit from.

Retrieving an existing session

Existing sessions can be accessed through the session element of the application class:

set theSession to first session whose name is "iTunes Session" --By name set activeSessions to every session whose hijacked is true --All the active ones set allSessions to every session --All of 'em

Accessing audio source attributes

Now that we have a session, we can configure its audio source. The source attributes available are dependent on the type of the session. Application sessions have targeted application and launch argument. Audio device sessions have input device and output device. And Radio sessions have frequency and output device.

--Setting up an application session tell application "Finder" set pathToiTunes to POSIX path of (application file id "hook" as alias) end tell set targeted application of appSession to pathToiTunes set launch argument of appSession to "http://tess.fast-serv.com:8956/listen.pls" --Setting up an audio device session set input device of deviceSession to (first audio input whose name is "Built-in Audio: Internal microphone") set output device of deviceSession to (first audio output whose name is "Default System Output")

Whenever files are involved, Audio Hijack Pro uses POSIX paths (/path/to/file), and not AppleScript files or aliases (Volume:path:to:file).

Note as well, that whenever an audio device is needed, you can retrieve them from the audio input and audio output elements of the application object:

set theNames to (name of every audio input) set aName to first item of (choose from list theNames without multiple selections allowed)

Accessing schedule and timer attributes

A sessions schedule is accessible through the timer element:

set allTimers to every timer of theSession --The existing timers set aTimer to make new timer at end of timers of theSession --Making a new one delete the last timer of theSession --Removing an old one

The basic attributes of a timer are enabled, actions, start time and duration.

set enabled of theTimer to true --Remember to turn your timers on! set actions of theTimer to {"record", "quit"} --Also can do "mute" set start time of theTimer to ((current date) + (60)) --Start time is a date set duration to 300 --5 minutes, duration is in seconds

By default, new timers are in "one-shot" mode, where they are set to fire on a specific date. To turn them into "repeating" timers, you turn on any one of their "runs" attributes.

set {runs Monday, runs Wednesday, runs Friday} to {true, true, true} --MWF

Accessing recording attributes

Most of Audio Hijack Pro's recording settings are simple attributes of the session:

--Set some Tags set title tag of theSession to "80s Airwaves" set genre tag of theSession to "80s" --And some file settings set output folder of theSession to "~/Music" --This is a POSIX path set output name format of theSession to "%tag_title %date"

The recording format, file size limit, and recording time limit attributes are a bit more complex, as they are represented by a special class (audio recording format and size or length limit).

set recording format of theSession to {encoding:AAC, bit rate:128, channels:Stereo, style:Bookmarkable} --Record to a Bookmarkable AAC set recording format of theSession to {encoding:MP3, bit rate:192, channels:Stereo, style:VBR} --Record to a VBR MP3 set file size limit of theSession to {enabled:yes, unit:MB, value:650} --Split every 650MB

Accessing effects attributes

Currently, the Effects system cannot be directly accessed via AppleScript. If you'd like this in a future version, please contact us at audiohijackpro@rogueamoeba.com

Sending commands

Finally, there are the commands for hijacking:

start hijacking theSession start hijacking theSession relaunch yes --Avoids the relaunch alert stop hijacking theSession

And recording:

start recording theSession stop recording theSession split recording theSession pause recording theSession unpause recording theSession

Putting it all together

A complete example script that shows how to put all these basic pieces together into one script, is available at: http://rogueamoeba.com/audiohijackpro/scripting/downloads/BasicExample.scpt