Create an iTunes Visual Plugin with REALbasic by Erick Tejkowsi
08-23-01




This week we'll create a simple REALbasic application that enables you to churn out custom iTunes visual plugins. Have a fun message you want to surprise a co-worker or loved one with? Try passing it along via iTunes to really get their attention.

I know what you're saying.... "You can't build an iTunes plugin with REALbasic!". Fundamentally this is true. Using a few tricks, though, we can fake it. Using an existing plugin, we will replace a few items within it and build a new copy of the plugin with the revised settings, thus making a "new" plugin. The end effect is an iTunes plugin of your very own that happily draws your name in multiple colors when run.

Build the Interface

This week's interface is drop-dead simple to create. Launch REALbasic, open Window1 and drag an EditField, StaticText, and PushButton into the window. The EditField will be used as a place to enter your name. Pressing the PushButton will:

To give you an idea of how this might look:

08-23_interface.jpg (5k)

Add the Code

Before we jump in and start coding, you need to know a few things. First, the plugins you will create are based on a template plugin named "ResexPlugin". When installed, this plugin is a fully functioning. It displays the word "ResExcellence" in flickering colors. Further, the name of the plugin (as it appears in the iTunes menu) is "ResEx Plugin". If you do a quick count, you will see the following:

ResExcellence 13 characters long
ResEx Plugin 12 characters long

The plan is to replace these two bits of information with your own data. Since this resides within the data fork of the plugin, it is vital that you not change the length of either string. Changing them will likely render the plugin useless. Don't need all the characters? We'll fill in any blank spots with a "space", since it maintains the string lengths yet won't alter the display.

The code is fairly minimal this week as well. For starters, double-click PushButton1 and enter the following code:

dim src,dest as folderItem
dim s,yourname,yourpluginname as string
dim binin as binaryStream

//find the default plugin
src=GetFolderItem("ResexPlugin")
if src<>nil and src.exists then

  dest = GetSaveFolderItem("",EditField1.text+" plugin")
  if dest<>nil then
    CopyFileOrFolder(src,dest)

    //we are going to change the file , so openAsBinaryFile(TRUE)
    binin = dest.openAsBinaryFile(TRUE)

    //ResExcellence = default text drawn onscreen
    //ResEx Plugin = default plugin name

    s = binin.read(binin.length+1)
    //add thirteen spaces to the name to be certain we
    //have the minimum number of characters
    yourname = left(Editfield1.text+" ",13)
    s = replaceAll(s,"ResExcellence",yourname)

    //the longest plugin name is 12 characters
    yourpluginname = left(Editfield1.text+ " Plugin ",12)
    s = replaceAll(s,"ResEx Plugin",yourpluginname)

    binin.position = 0
    binin.write s
    binin.close
  end if
end if

If you were paying attention, you may have noticed the undefined CopyFileOrFolder call. This leads us to our last code example. Select Edit->New Method and create a new method:

08-23_copymethod.jpg (20k)

You may recognize this method from the Language Reference in REALbasic. In fact, if you navigate to the "FolderItem" entry in the Language Reference, you will find the code for CopyFileOrFolder, which you can drag from into your new method. In case you like to type, the code is:

Dim i as Integer
Dim newFolder as FolderItem
If source.directory then //it's a folder
  newFolder=destination.child(source.name)
  newFolder.createAsFolder
  For i=1 to source.count //go through each item
    If source.item(i).directory then
      //it's a folder
      CopyFileOrFolder source.item(i), newFolder
      //recursively call this
      //routine passing it the folder
         else
source.item(i).CopyFileTo newFolder //it's a file so copy it
    end if
  next
else //it's not a folder
  source.CopyFileTo destination
end if

Conclusion

That's it! Test the project and when you're happy with the results, build the final application. The resulting plugin should produce somthing that looks like this:

08-23_resexview.jpg (8k)

Don't forget to keep a copy of the template plugin (entitled "ResexPlugin") in the same folder as your application. As usual, you can download the application and completed project. It also includes the C source code in case you are interested in building your own iTunes plugin in CodeWarrior. Until next week!