Recently I published a "work in progress" version of an iTunes object (Link) which shows how DesktopX can be used to interact with applications installed on your computer.

This article is designed to introduce people into how this was achieved so that they can try to use DesktopX to do similar things. It is not designed to be an exhaustive guide to scripting, nor an exhaustive guide to interacting with iTunes. Rather, it is a simple introduction which specifies how to achieve some of the basics so people can develop objects themselves.

OK, so let's get started. What is ideal is a guide to the application's object model. Just like DesktopX, other applications have various properties and methods that can be accessed. Apple proves a SDK for iTunes that includes such a help file. You can download this help file here: Link

Alternatively, you can use a tool to view these methods. There are several available, but I find the simplest and most effective tool Visual Basic CCE. I will use this in this article, but you can you your preferred tool. You can download VB CCE free from here: Link

So, let's get started ...

To start with create an object, and create a new script for it. What we want to do here is create an object that can represent iTunes so that other objects can interact with it. For this reason I will name the object "iTunes". The key command here is CreateObject. You need to know what to "create" and the iTunes literature tells me that I need to create an iTunes.Application object. I'm creating a variable to represent this.



We can now refer to iTunes directly. Within this object we can use iTunesApp to refer to the object, and externally we can refer to it using DesktopX.ScriptObject("iTunes").iTunesApp. Note that we need to define the iTunesApp variable outside the code so it can be accessed by other objects.

If you don't have documentation but you know an application can be scripted, Google can often be the savour. Do a search for "createobject iTunes" or "createobject Outlook" and you probably find a site that tells you what you need to know. Just remember that in DesktopX you don't need the "WScript." prefix.

OK, so we can now really start to play with iTunes. Let's have a look at the Object Model using VB CCE.

Launch VB and click OK on the dialog. it doesn't really matter what you select as we're not actually creating anything. The first thing to do is tell VB that you want to be using iTunes. From the "Project" menu, select "References". You can now see all the things on your computer that you can refer to using this technique. Check the reference to the iTunes 1.1 Type Library and click "OK".



If you now go to the "View" menu and choose "Object Browser" - this is the tool you really need as your reference. From the Libraries combo, select the iTunesLib reference that you just added. All of a sudden you will be presented with information about everything you can do with iTunes.



Globals refers to the base iTunesApp object that you created and on the right are all the properties and methods associated with that. The methods have the green icon like BackTrack and Play. So, for example you can do iTunesApp.Play() to play the currently selected track. If you look at the properties these are the elements of iTunes you will need to interact with, and it helps to have some knowledge of the application itself. You will probably know that the starting point with iTunes is selecting the source for your music, which can be the "Library", an "Audio CD" or "iPod" amongst other items. With this in mind check out the Sources property.



What this tells you is that if you refer to iTunesApp.Sources then you are referring to a collection of objects known as IITSourceCollection. If you look on the left you will see the IITSourceCollection class. Looking at that you will see that it has 3 properties and we will use the "Count" and "Item". You will see that each "Item" is an IITSource. The iTunes help file is actually more revealing that VB at this point. You can see that each source has a kind.

We can now start to use DXScript to do something useful. Create a new object called iTunesSources and make it a DXCombo ActiveX control. This will be a place to display all the possible sources.

OK, so back to our iTunes object. Let's write that script, and a fairly simple one at that now we know what we're dealing with. We need to add the sources to the combo box, and as part of that process we need to convert the "Kind" into something useful rather than just a number. The documentation tells me all the ITSourceKind variants.



What we have now is a combo box with all our sources on there.



OK so we have all our sources. What we now want to do is to be able to see what is available on these sources. I'm going to focus on the main Library as there are variances with things like the Radio Player. Therefore, assume you should only select the Library in this demo or you may get some oddites.

In iTunes, the Library will have Playlists associated with it and each playlist will have tracks associated with it. Create 2 DXListbox objects; "iTunesPlaylists" and "iTunesTracks".

OK, so now, we need to add script to the list of sources so when we click it it displays the Playlists. Again this is a similar script. You can see from the documentation that the IITPlayListCollection works basically the same way as the collection of sources, so the script is very similar. In fact, it's a little easier because be don't need to translate the kind. If you display a "List of all members" in the documentation you will see that each playlist has a "Name" property.



You now have a list of the playlists available when you click the source. Here's the script for the source combo.



We now just need to do something very similar to display the tracks when you click a playlist. Here's the script for the playlist list.



You now have a series of objects that selects the appropriate tracks based on the source and playlists.



The final thing to do is to play a track when it is clicked. This again is easy to achieve. Just add this code to the tracks list.



That is about it for the basics. Just explore the object model further and the sample object and you can see how far you can develop applications using DesktopX!

Click this link to download the sample objects: Link


Comments
on Jun 17, 2004
Ecxellent work Martin
on Jun 17, 2004
Thumbnails are your friend.
on Jun 17, 2004
Someone please explain the need to do all of this. Not sure I fully understand why it is necessary. I won't do Apple Script on my G5, or work within Unix, so what is the benifit of doing what is basically the same thing on a Win Machine.

Pam
on Jun 18, 2004
Pam,

OK, take this somewhat flippant scenario. You're having a party and want people to control the music. If you load iTunes somebody could accidentally delete your entire music collection. With this, you don't have to display all the controls and as such people can control the music without deleting any. This about the browser on your kids computer. How about building a series of custom object so the can go to disney.com and stardock.com without them typing www.somepornsite.com.
on Jun 18, 2004
Martin,

on Jun 18, 2004
Good point Martin.
on Jun 18, 2004
Martin,

Thank you (sorry about the above, hit the wrong key). You may have convinced me to attempte to play around with this form of scripting. As to the kids playing around on my computer; not really sure that will happen they are in their thirties. Oh, and the grandkids know matter; grandma will run over them with her wheel chair.

Again thanks. Pam
on Jun 18, 2004
That was suppose to read as, "Oh, and the grandkids know BETTER;

Pam
on Jul 17, 2004
Cool work.

Hey, I have an idea, who is willing to develop something that saves web pages you like or need for the purpose of doing research for a school project or for work. I think it can be done with DX scripts. Press a button and it saves the web page for you and any information about the page you saved (date, author, web site URL and other stuff)
on Jan 30, 2005
Thanks for the article Martin. I may be mistaken but I think the VB CCE tool you refer to in your article is no longer available on the link you provided. It may be that this link proves to be useful for those that have read your piece and want the download.

Link

Below is the source page I found the link at .

Link

As I'm gathering information rather than seeking to provide it - this is on the fly as a result of trying to track down a link you referenced.

Feel free to delete if my post is misinformed or misplaced.



on Feb 01, 2005
This is all very cool, but I have 2 questions:

How did you get your dx script editor to have that font?

How did you get your combo boxes to size like that? All I can get is a 64x64 box which only shows the first letter. (edit: OK, I got it looking right with an MS Forms combo box. With the DX combobox, I couldn't even select it to resize.)(edit2: Ok, nevermind about sizing these activex objects. After much ctrl-clicking, over and over and over, for some reason they started to allow themselves to be selected)
So I guess I just have that one question LOL


Thanks