iTunes Javascripts

Somebody on digg mentioned AppleScripting iTunes on the Mac, but it seems to be less well known that you can write Javascripts or VBScripts for iTunes for Windows.
So, here’s a bunch of scripts I’ve written for various tasks:

I’ve written more, but these are the most general purpose ones.

To use any of them, just download them, rename them to a *.js file, then run them like any other program (double click, select and press enter, type the name from a command prompt, etc). If you have all the defaults in XP or have the Windows Scripting Host installed on other Windows boxes, then the wscript.exe program actually runs the scripts, much like cmd.exe runs batch files. Same idea, anyway. The scripts connect to iTunes as a COM object and use it in that fashion. Works really well and is quite handy for scripting tasks in iTunes. Yeah, it doesn’t actually integrate or anything, but it’s still useful.

143 thoughts on “iTunes Javascripts”

  1. Hello Otto,
    I have been searching for a command-line script to insert a new playlist into iTunes. I could then add tracks to the new playlist using a script that I found on MetaFilter called AddToPlayList.js, which works great.

    Any ideas for adding a playlist?
    Thanks,
    Steve

  2. Hello Otto,
    I just wanted to know if there was any modification in code that could be used in ClearLyrics or EvilLyricsImport that would be able to delete/import lyrics directly to my iPod instead of my iTunes library.
    Thank you,
    Victor

  3. I run it the .js file, but i dont think its working right. I have both Evillyrics and iTunes open, but nothing playing.
    It only updates 2 songs. Do I need to make a special playlist to have it update all (I changed the variable updateall to true)

    Thanks in advance.

  4. Curtis: The EvilLyrics script doesn’t magically go and get the lyrics for you. EvilLyrics does that when you play the songs. The script I wrote just looks for the resulting text files and loads them into iTunes as best it can.

  5. How do I make one that gives me an ArtPlaylist just like the NoArtPlaylist but only for the files WITH album art? 😀

    Thanks

  6. Change the line that reads:
    if ( currTrack.Artwork.Count == 0 )

    To this:
    if ( currTrack.Artwork.Count != 0 )

    And you can also rename the playlist, if you like.

  7. Bettie:

    var iTunesApp = WScript.CreateObject("iTunes.Application");
    var track = iTunesApp.CurrentTrack;
    track.PlayedCount = 5;

    Just select your track in iTunes (highlight it) then run this script. It’ll set that tracks played count to 5.

  8. With the addition of iTunes Store hi-quality artwork and coverflow in iTunes 7, I would love to see a script to go through your iTunes library looking for tracks that are using downloaded artwork in an ITC file, extract that artwork and embed the image in the music file itself.

    Thanks
    Randy

  9. Hey, has anybody seen anything out there that will allow you to see what playlists a certain track may belong to? It would make managing my playlists much easier. Sometimes, I can’t remember if a song was included in a playlist and I will add it again, only to find later on that it was already there.

    If so, please email me the fix or code, or whatever, as it would be greatly appreciated.

  10. Is it possible to have iTunes play a video file starting at a particular (user defined) play position (i.e., file location?), and maybe have it play for {x} number of seconds? Thanks.

  11. Otto:

    One of the things that several other programs (like Winamp and MediaMonkey) offer is the ability to transcode files as they are transferred to an iPod. As far as I know, iTunes won’t do this. However, I understand that Doug Adams has a Lossless to AAC Workflow applescript (http://dougscripts.com/itunes/scripts/ss.php?sp=losslessaccworkflow) that does just that. Would it be possible to create a similar script for Windows? If so, this would appear to solve a major issue for a lot of folks who want to keep Apple Lossless files on the PC and AAC files on the iPod. I know very little about scripting and was hoping that you would know if this is possible. Thanks in advance.

  12. I am trying to run these scripts on Vista, but when I try to run a *.js file it just opens in Dreamweaver. I tried to associate it with something else, but I can’t find the program to use. Any suggestions?

  13. Is there a script that would reset all tags or just the TLEN tags? I have a bunch of songs that are tagged so they have no duration.

  14. No, iTunes does not provide that sort of access to those tags. You could use a program like MP3Fix or something to correct your bad length MP3 files.

  15. How do u figure out the var names?? Where did u go to find out what itnes uses for these names.
    ie:
    iTunes.Application
    iTunesApp.SelectedTracks
    iTunesApp.CreatePlaylist
    currTrack.Artwork.Count
    currTrack.VideoKind
    mainLibrary.Tracks etc

  16. Hi Otto,
    is there a script out or any other way to merge/join a bunch of acc / m4a files into one? There’s stuff like that for mac users. I’ve tried to convert my files to mp3, but my mp3-merger chokes when I give him the converted files.
    Help is much apprechiated.
    Thx.

  17. Hi Otto,

    Great scripts, real time savers, thanks!

    Just wondering if you know of a script that will create a playlist of all tracks where the song title contains words which start with a lower case letter. I would like to fix the tags of these tracks, but would rather alter them manually rather than it be automated as there may be some where a lower case letter is more appropriate.

    Thanks

  18. Hi i was just wondering if there was a script where u could see how many times each artist has been played?
    Thanks

  19. Is there a way to call a particular iTunes encoder in a script? For instance, if I always want a particular script to use the AAC Encoder at 192 vbr, what line would I include in a script? Thanks very much.

  20. username9876: You can set the encoder, but not the bitrate. If you call IiTunes::Encoders, it will give you back an IITEncoderCollection. You can look through these to find the IITEncoder object you want, and then set it by putting it into IiTunes::CurrentEncoder.

    To put it another way:
    var iTunesApp = WScript.CreateObject(“iTunes.Application”);
    var encoderCollection = iTunesApp.Encoders;
    var encoder=encoderCollection.ItemByName(“AAC Encoder”);
    iTunesApp.CurrentEncoder = encoder;

  21. Hi
    I have 611 songs in a playlist which were badly named during an import from an old mp3 player. The filename is currently in this format:

    00 – (Tag Missing) – Robbie Williams – Millennium

    and I want it to be

    Robbie Williams – Millennium

    There is no ID3 tagging in the mp3’s. Basically, what I want to do is TRIM the left 21 characters from the actual filename and obviously have itunes update its list too. Even better would be to read the next part of the filename i.e. up to the next ” – ” and add that as the artist tag.

    Is any of this possible??
    Thanks
    Daniel

  22. HI guys,
    Anyone have an idea why this script doesn’t work (I wrote it myself and can’t see what the problem is)

    var ITTrackKindFile = 1;
    var objApp = WScript.CreateObject(“iTunes.Application”);
    var tracks = objApp.SelectedTracks;
    var numTracks = tracks.Count;
    var i = 0;
    while (numTracks != 0)
    {
    var currTrack = tracks.Item(numTracks);
    if (currTrack.Kind == ITTrackKindFile)
    {
    var storedComm = currTrack.Comment;
    if (storedComm.match(/, acoustic/i) == null)
    currTrack.Comment.replace (“acoustic”, “”);
    }
    numTracks–;
    i++;
    }
    WScript.Echo (“The text has been removed from Comments.”);

  23. I should re-phrase… I took another fantastic script that appends to comments and f’ed it up.

  24. johnPaulVaughan: Your problem is right here:
    var currTrack = tracks.Item(numTracks);
    That ain’t right. Also, it’s a weird way to write a loop. Using a while when you have a fixed number of tracks to go through doesn’t make any sense. Use a for loop instead. Like this:

    for (i = 1; i <= numTracks; i++)
    {
    var currTrack = tracks.Item(i);
    ...

    You won’t need to increment i or decrement numTracks this way.

    Another problem: What exactly is this supposed to do?
    if (storedComm.match(/, acoustic/i) == null)
    currTrack.Comment.replace (”acoustic”, “”);

    As written, that doesn’t make much sense. It’s searching for “(comma)(space)acoustic” in the comment, and when it does not find it (the == null part), it attempts to replace the word acoustic with an empty string. I’m not sure that is what you want to do. I think you’re wanting to find the word acoustic and remove it from the comment, yes? In which case forget the match, just do the replace directly.

  25. Otto
    Could you write a script that finda Duplicate songs but only if their title, artist and album are the same?

    iTunes has a decent “Show Duplicates” but it only seems to match artist and title.

    I have dozens of Greteast Hits albums only with the originals they were taken from and this makes using “Show Duplicates” rather tedious since I have to double check the album name too.

    I would still need to do some investigating to make sure which I would delete but at least the list would be shorter.

  26. Thanks for the help Otto (It makes a lot more sense to me now). I created this script from what you suggested, brilliant but I must be missing something because it still doesn’t work – I’m not sure why… /* so close but yet so far */…

    var ITTrackKindFile = 1;
    var objApp = WScript.CreateObject(“iTunes.Application”);
    var tracks = objApp.SelectedTracks;
    var numTracks = tracks.Count;
    for (i = 1; i <= numTracks; i++)
    {
    var currTrack = tracks.Item(i);
    currTrack.Comment.replace(“acoustic”,””);
    }
    WScript.Echo (“The text has been removed from Comments.”);

  27. Still using replace incorrectly. replace doesn’t modify the string itself, it just returns the modified string in memory.
    Try this:
    var str = currTrack.Comment;
    str = str.replace("acoustic", "");
    currTrack.Comment = str;

  28. Otto… I’m trying to hack a script together that allows me to add tv shows (.mp4) into my playlist. I currently have the scipt working to with basic attributes but I want the script to check to see if the file I’m trying to add is already in iTunes.
    I need JScript help since the last time I coded was using “C and c++” back in the day.

    I’m getting a runtime error when it tries so search a collection when it can’t find anthing in the library already. So the first issue is: When a collection is blank as the result of a search is there a way to determine that before the code fails when using the enumerate statement?

    Second issue I’m having is when it has a valid collection,and I try to verify duplicates, it always matches even though the echo statement shows them not matching.

    Here’s the script:
    /*
    addToiTunes.js [video type: 0=music 1=movie 2=musicvideo 3=tv show] [Show] [Title] [AirDateTime]
    */

    var ItunesApp = WScript.CreateObject(“iTunes.Application”);
    var mainLibrary = ItunesApp.LibraryPlaylist;

    var file;
    var videoKind;
    var show;
    var title;
    var emumItems;

    if(WScript.Arguments.length > 0)
    {
    file = WScript.Arguments(0);
    }

    if(WScript.Arguments.length > 1)
    {
    videoKind = WScript.Arguments(1);
    }

    if(WScript.Arguments.length > 2)
    {
    show = WScript.Arguments(2);
    }

    if(WScript.Arguments.length > 3)
    {
    title = WScript.Arguments(3);
    }

    if(WScript.Arguments.length > 4)
    {
    epid = WScript.Arguments(4);
    if (title==””) {title=epid;}
    } //fix for null mediacenter episode titles, use AirDateTime

    WScript.Echo (“Before collection search”, show, videoKind, title);

    var colItems = mainLibrary.Search(show, 0);

    if (file)
    {
    enumItems = new Enumerator(colItems);

    for (; !enumItems.atEnd(); enumItems.moveNext())
    {
    var objItem = enumItems.item();
    WScript.Echo (“For Loop”);
    WScript.Echo (objItem.name, ‘****’, title);
    WScript.Echo (objItem.show, show);

    if (title = (objItem.name))
    {
    WScript.Echo (“Matching show already in Library – don’t Add it”);
    }
    else
    {
    WScript.Echo (“Add to Library”);

    var opStatus = mainLibrary.AddFile(file);
    while (opStatus.InProgress){} //wait until done
    track = opStatus.Tracks.Item(1); //quick and dirty
    if (videoKind)
    {
    track.videoKind=videoKind;
    }
    if (show)
    {
    track.Show=show;
    }
    if (title)
    {
    track.Name=title;
    }
    if (epid)
    {
    track.episodeid=epid;
    }

    }
    }
    }

  29. Hi Otto.

    Is it possible to write a script that increases the playcount.

    It’s a feature that I’d like to use together with my Serato Scratch Live DJ-software.

    I’d want the playcount to increase when I play the track in SSL.

    Thanx
    /Anders

  30. Hi Otto.
    When I run your script, I got this error: “WScript.CreateObject – Could not locate automation class named iTunes.Application”. Error code: 80020009.

    I use iTunes 7.6.1 and Win Xp2. In my registry do not exist the Class iTunes.Application. In regedit (HKCR), I found: IpodManager Class, ItunesDevices Class, ItunesService Class, ServiceDiagnostics Class and iPodUpdaterInterface Class.

    Any ideas? Thanks.

  31. I am just wondering if you have ever successfully added file/track thru the iTunes COM/Script to iPod Shuffle?

    From iTunes SDK, there is AddFile() function to the LibraryPlaylist out of iPod source. It works for most of iPod devices, but to iPod Shuffle, AddFile() function will return 0xA0040203 (ITUNES_E_OBJECTLOCKED)!

    Any idea about this?
    Or do you know of any other function such as “Autofill” for iPod Shuffle?
    Thanks.

  32. evillyrics js..

    seems to work only for some songs. i downloaded a night of lyrics with evillyrics and
    then run the script to automatically write the lyrics into the tag. only 22 song over
    3 hundreds. ..even if the updateall is set to true.
    what can i do to make the script running?

  33. aldo: The evil lyrics script is not needed anymore, the evillyrics program now supports talking to iTunes directly. Use that.

  34. has any one come across this error that billox mentions above?
    “WScript.CreateObject – Could not locate automation class named iTunes.Application”. Error code: 80020009.

    I am also getting this and have tried uninstalling and resintalling numerous times (even older versions) but it has not fixed…
    I wrote some scripts which makes itunes useful for me, but with them not working it’s completely useless.

    🙁
    Dale.

  35. tinman: That problem only has one cause and one solution. The cause is an incomplete iTunes installation. The solution is a complete removal and reinstall of iTunes. Sometimes you can get it working by simply installing iTunes over itself.

    However, since you’ve tried that already, you’re probably out of luck, because that is the only answer to your problem.

  36. so is there no way to register the automation class manually?
    (sorry if that’s a stupid question)

    otherwise.. I guess I have to pray that the next version of itunes (which shouldn’t be far), will fix it.. but I don’t have high hopes!

  37. If there is a way to do it, then I don’t know what it is.

    Reinstalling iTunes has always fixed it in all the cases I’ve seen.

  38. Hi. I’m very new when it comes to all of this and i’ve been running AtomicParsley to edit specific tags which you normally can’t accomplish in editing within iTunes itself. The only one left for me is to edit/add LongDescription for my movies in iTunes. I’ve looked everywhere and i can’t find a solution. I downloaded iTunes COM Windows SDK but have no idea how to handle it or how to write those jscripts to edit the longDescriptions which i’ve desperately been trying to achieve. If anyone can write this script for me or at least give me the tutorial on how to accomplish this then please i beg of you to help me. You can contact me by e-mail aswell (cesar_belifs@hotmail.com). I’ve really been trying to find these answers for 2 whole days now 24/7 with no luck at all…i’m using Windows XP. The short description i’ve accomplished which can be edited with AtomicParsley but to edit the long description is a no go for me, the one you can read when you click on the (i) button on the description field on itunes. Please help me on this one. I’de be most thankful for it!

Leave a Reply to nik Cancel reply

Your email address will not be published. Required fields are marked *

css.php