This lesson takes you through the task of making a LiveCode program that controls and fetches information from your copy of iTunes. This may seem like a difficult task, but thanks to LiveCode 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 but the example stack should work in older versions of LiveCode on Mac OS X.
You can download the stack that accompanies this lesson from: https://tinyurl.com/y9a7deq8
LiveCode 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 LiveCode using the do as vbscript and do as applescript commands.
First off, we will try and make iTunes start playing from inside LiveCode, 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 LiveCode by putting them into custom properties. To get this working, open LiveCode 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:
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
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.
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
Place a button called "Stop" on the stack 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. At the top of this lesson is a link to the example stack. The look and feel of the stack was based on the example from the excellent 3rd edition of the LiveCode newsletter by Ben Beaumont (3rd August 2006). If you would like to know more on how to create skinned LiveCode stacks like this, there is a lesson 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 LiveCode to communicate with them.