|
This tutorial takes you through the task of making a Revolution program that controls and fetches information from your copy of iTunes. This may seem like a difficult task, but thanks to Revolution's support for executing so called "active scripting" languages, it's in fact very straight forward. The ideas used in this tutorial can also be used to interact with other Windows and Mac OS X applications. Note that some of the Windows features discussed in this article were added in version 2.9, so you will need either to have a Revolution Enterprise license or to download the Beta version of 2.9.0-dp-2, our latest developer preview. The example stack should work in older versions of Revolution on Mac OS X.
How does it work?
Revolution programs can communicate with iTunes easily because iTunes makes most of its functionality available via a COM interface on Windows and via AppleScript on OS X.
In this tutorial we use fragments of VBScript to communicate with iTunes on Windows and AppleScript to do so on the Mac. These scripts are executed directly by Revolution using the do as vbscript and do as applescript commands. You can download the associated stack here.
Start talking to iTunes
First off, we will try and make iTunes start playing from inside Revolution, this will show the general pattern for how we talk to iTunes, and from here, the rest of the functions we need should be easy.
We need two fragments of "active script" code to achieve this, the simplest forms of these are shown below.
-- AppleScript code to start iTunes playing on OS X.
tell application "iTunes"
tell source "Library"
tell playlist "Library"
play track 1
end tell
end tell
end tell
'VBScript code to start iTunes playing on Windows.
Dim thePlayLists, theTrack, theLibrary, thePlayList, i
Dim iTunesApp
Set iTunesApp = CreateObject("iTunes.Application")
Set theLibrary = iTunesApp.LibrarySource
Set thePlayLists = theLibrary.Playlists
Set thePlayList = thePlayLists.ItemByName("Library")
Set theTrack = thePlayList.Tracks.Item(1)
theTrack.Play()
These two code fragments can most conveniently be executed in Revolution by putting them into custom properties. To get this working, open Revolution and create a new stack. Set the cMacPlayScript of the stack to the Applescript sample above and the cWinPlayScript to the VBScript sample. Next edit the stack script and put the following simple command into it:
-- Tells iTunes to play the first track of the library on Windows and Mac
command iTunesPlay
local tScript
if the platform is "win32" then
put the cWinPlayScript of me into tScript
do tScript as "vbscript"
else
put the cMacPlayScript of me into tScript
do tScript as "applescript"
end if
end iTunesPlay
Finally, place a button called "Play" onto the stack, and set the script of the button to simply be:
on mouseUp
iTunesPlay
end mouseUp
When the button is clicked, iTunes should come to life by playing the first track of your music library. Of course there is no way to stop iTunes just yet, but the stop script is simpler than the one for play:
-- AppleScript to stop iTunes playing on OS X
tell application "iTunes"
stop
end tell
'VBScript to stop iTunes playing on Windows
Dim iTunesApp
Set iTunesApp = CreateObject("iTunes.Application")
iTunesApp.Stop
The command iTunesStop is shown below and should be added to the stack script of our program.
command iTunesStop
local tScript
if the platform is "win32" then
put the cWinStopScript of me into tScript
do tScript as "vbscript"
else
put the cMacStopScript of me into tScript
do tScript as "applescript"
end if
end iTunesStop
Finally, create a button called "Stop", and set its script to:
on mouseUp
iTunesStop
end mouseUp

We have so far demonstrated how to create a simple program that starts and stops iTunes. Included with this tutorial is an example stack which expands this idea and combines it with our U3 Sampler stack, which plays mp3s from a folder. The original U3 Sampler stack can be found in the Resources/Examples folder of your Revolution distribution. The look and feel of the stack was based on the example from the excellent 3rd edition of the Revolution newsletter by Ben Beaumont (3rd August 2006). If you would like to know more on how to create skinned Revolution stacks like this, the original newsletter can be accessed here.
The iTunes example uses a library stack to keep all the iTunes interaction code in a single place. This library is a substack called "iTunes", which is loaded when the stack is opened. The library uses the same principals as shown earlier in this article, except that the code has been written in a slightly more abstract fashion to avoid too much duplication. The VBScript executed on Windows is the same every time, because otherwise code to declare and initialise the iTunes COM object would have to be copied into the separate script for each function.You are welcome to take the iTunes example stack apart to learn how it works and use any of the code in your own applications.
Many other major applications on Windows and Mac OS X provide an interface through AppleScript or COM objects that allow Revolution to communicate with them.

|