What I'm Doing...

Subscribe

Search

Email

  • Memphis, TN 38103 U.S.A. Otto 240cf86e-5cad-11dc-8314-0800200c9a66

Buy Otto a Beer

Currency:

Amount:

Website(Optional):

Amazon Honor System Click Here to Pay Learn More

Get Otto a Gift

My Amazon.com Wish List
Text

20 Oct 2005-10-20 11:01

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.

Tags: , , , , , , , , , , , ,

Posted in Geekery, Programmery | -

118 Comments

  • QW Says:

    Hey Otto, I just came across this post via ‘EvilLyrics’ forum, and noticed that all the links to your Scripts are dead. I was just curious if you still have them laying around anywhere or archived? I just recently got an iPod video, and a more simple (and possibly automated) way of importing lyrics to each song would be much appreciated. That is, if your scripts work with the latest iTunes 7.0.2.

    Thanks and Take Care,
    QW

  • Otto Says:

    The links should be working now.

  • RH Says:

    Hey Otto
    I can’t get any of the links to work.

    Thanks,
    RH

  • RH Says:

    Hey Otto,
    Figured it out. Your links point to http://ottodestruct.com/itunes/...
    It should be http://ottodestruct.com/iTunes/...
    The T needs to be capitalized.

    Thanks for the great code.
    RH

  • Lynn Says:

    I can not figure out how to run the scripts. I have saved the file to my computer ( C:\Scripts ) and have Windows script Host opened in a cmd window, but I just can’t get the script to run. I’m pretty clueless when it comes to scripts but I really want to run the NolyricsPlaylist one. Can you offer any advice to a newbie.

  • Otto Says:

    There’s really nothing to it. Rename the scripts from .txt to .js then double-click them in explorer, like you’d run any other program. You don’t need a command window open unless your configuration is all screwed up. The windows scripting host will run automatically since it’s associated with .js files by default.

  • Lynn Says:

    :( … But when I double click it, it just opens in notepad. Nothing else happens.

  • Otto Says:

    Then your computer is broken. You’ve misassociated JS files with notepad, somehow. Associate them to open with wscript.exe instead.

  • Seena Says:

    Hey Otto,

    Didn’t know that this was possible, awesome!

    I’ve been looking for a way to create a playlist for each album. In other words, every track that belongs to an album will go into a playlist called “artist - album”. This doesn’t exist for Windows! But there are a few solutions for OSX like this one: ( http://www.dougscripts.com/itunes/scripts/scrip... ), which doesn’t help me.

    Can you write, or help me write, a script that does this?

    Thanks,
    Seena

  • Seena Says:

    Hey Otto,

    Nevermind! One of the sample scripts in the iTunes Windows COM SDK does exactly this (I’m sure you already know).

    Thanks,
    Seena

  • [...] That should be all. You now have your iTunes alarm clock! You can, of course, do lots of other stuff. For example - if you have a program that translates any hot key to a command, you can make scripts for going to next and previous track, play/pause and similar. To get some good ideas, check this Microsoft article out. There is also some info here. Finally, do you have an idea for a script that none of these articles seem to describe? Download the Apple SDK, open the zip file and then open the iTunesCOMInterface.h file. Don’t be scared by all the C code - just search through it (for example, you want to activate Repeat, search for “repeat”). The name of the methods in the C code are most of the time identical to, or similar to, the commands you need to write in your script. Good luck! Tags:apple, code, howto, iTunes, programming, script, Technology windowsShare This [ ] [ January 17, 2007 ] [ Technology ] [ ] [...]

  • Chuck Says:

    Lynn:

    I think you are having the same prob that I was. I originally saved the files as .txt’s. Then when I renamed them they were like this FindDeadTracks.js.txt. The only way that I could figure to change them to just FindDeadTracks.js was in the cmd window.

    Easiest way to do this: Put the files in your root directory, just C:\, not C:\Scripts. at the cmd prompt type: ren FindDeadTracks.js.txt FindDeadTracks.js

    Then you should be able to double click and they will run. I hope that helps, let us know if it doesn’t.

  • rich Says:

    hey otto, i want a programme that saves all my ratings and id tag info? cant seem to find it on ur site….can u point me in the right direction?

  • Chris Says:

    What I would really like is a batch script to change the Video kind. I know applescript has this and its easy to find it on google.

    please! please!

  • Sean Says:

    Also, it would be nice to be able to change the play count to a user specified number. Again this can also be done with apple-script.

    please! please!

  • Stefan Says:

    Great job, scripts work perfectly well and were exactly what i’ve been looking for! Thanks a lot!!

  • John Says:

    Looking for the same this as Sean above - changing the Play Count in Windows… help much appreciated!

  • GBugg Says:

    THANK YOU!
    removedeadtracks.js has SAVED me!!

    Thank you VERY much for making this available!!!

  • RC Says:

    I would like to use a script to add a transcoded TV show to iTunes. Once the work is done, I want to tell iTunes to import the file and provide it some information about the show. Should I be looking in the COM SDK for ideas on how to do this? Is it even possible? I know there are C++, C#, and other ways to do it but I just want to create a simple script and not go through all that development work.

  • Otto Says:

    Everything that you can do in C++ or C# and such, with regards to iTunes and the COM SDK, can also be done through javascripts or vbscripts. So yes, reading the COM SDK’s help file and looking through some of my scripts would be an excellent start to automate your iTunes usages.

    RC: in your case, I would look closely at the LibraryPlaylist object and the AddFile function within it. After you add the file to the main library, you’ll have to search through it to find the new Track listing for it (which may be harder than you think, especially if you have iTunes actually organizing your library), and then you can use the Track object to change any of the parameters for that entry.

    If you actually want to automate your transcoding itself, and you use iTunes to encode files, then consider the ConvertFile and ConvertFile2 functions in the main iTunes object. Also examine the Encoders and the OperationStatus objects.

  • Jon Says:

    Hi Otto,

    Your scripts for windows based iTunes are amazingly helpful. There is one thing that I have been really in dire need of however, that maybe you could help me with. I know the current version of itunes has a feature that shows duplicate tracks (It searches by name of track and artist), but that also means songs on different albums are considered duplicates. I have about 95 gigs worth of music, so going through all of them would be quite tedious, I’ve been searching for a script that’d be able to locate (and preferrably delete) the duplicate tracks if they have the same track name, artist, AND album… Your help is very much appreciated, so far I’ve only found one solution, and it’s software that costs money that I just don’t have.

    Thanks in Advance.

  • Peter Says:

    Hi Otto,

    Very cool work, didn’t realize iTunes could be scripted in Windows…is it possible to replicate the ‘append to comments’ script found on Dougs Applescripts? I use this heavily on my Mac to tag songs and create smart playlists.

    Thanks!

  • michael Says:

    Um otto, I have never done anything like this before. I just copy the whole thing and re-tag it as a .js in the folder that it tells me to in the second line?

  • Paul Says:

    For the one hit wonders… is there any way that you can make it depend on the album only appearing once as opposed to the artist? Would I jst change all of the bits that say artist in note pad to album?

  • Carlos Says:

    Hey, great job and thanks for the hint, just wondering if there´s a way to delete the lyrics of just one playlist, hope you can help me. Cheers.

  • Christian Says:

    Hi Otto,

    What I like most about your scipts is that they are practical in the true sense of the meaning. I’ve ssen too many gadgetry so far.

    Do you have a solution to customize the way iTunes is renaming files or even a standalone tool for this? I would like to rename some WAV files (no ID3 tags, obviously, but full Gracenote information imported into the library) based on the information in the iTunes XML library (!). If I do not find a solution for that, these files will be buried in my library … manual renaming would be time consuming.

    Regards,
    Christian

  • Christian Says:

    … it seems that there are AppleScript Solutions available from Dougscripts: see “renaming files” and “file renamer”

  • [...] iTunes Javascripts » Nothing to see here (tags: itunes windows wscript list) [...]

  • Pat Says:

    alright, im a total newbie at this whole scripting thing, and im sorry for my stupidity in advance. im trying to use the evillyrics import script. ive changed the .txt file to a .js file on my desktop. i run it the .js file, but i dont think its working right. it only seems to import songs one at a time, as i listen to them. i have both itunes and evillyrics open. after a while a small window shows up telling me it has “added lyrics to 1 song”. what am i doing wrong? or is it supposed to go slow like this?

  • conan troutman Says:

    hey thanks a lot for that “find dead tracks” script.

    thats has made my search in my over 11.300 song database easy as something what is easy to do.

    greetz conan troutman

  • pio Says:

    http://img176.imageshack.us/im.....rorgt5.png

    I’m getting this error, can someone help?!?!?!

  • Otto Says:

    pio: Reinstall iTunes. That means that it’s not registered properly, and needs to be installed correctly.

  • Judith Says:

    Thank you so much for the scripts!! Really awesome. This is what I’ve been looking for for ages! I first this library-not-registered problem but just like with some girl in the evillyrics forum it was only because of itunes which was not installed properly. It now works fine! So thanks a lot!

  • Discrepancy Says:

    Someone above asked for an “append to comments” script. For example, if you wanted to sort by filename, you could use this script to take the Location property of all _selected_ songs and put it into the Comment field. Note: this overwrites all comments that might already be in there. This can be easily modified to put in any other piece of information you want, if you know the available properties (which are listed in the SDK). Just replace “currTrack.Location” with the property of your choice. This isn’t as fancy as Doug’s, but it should get you started on the right “track” (pun intended!).

    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)
    {
    currTrack.Comment = currTrack.Location;
    }
    numTracks–;
    i++;
    }
    WScript.Echo (”File location added to Comments field for ” + i + ” tracks.”);

  • Discrepancy Says:

    That same basic code snippet can be changed to allow you to modify things like Skip Count and Video Kind, by replacing the following line:

    currTrack.Comment = currTrack.Location;

    With one of the following, respectively:

    currTrack.SkipCount = 0;

    currTrack.VideoKind = x;

    The enumerated values for VideoKind (replace for x) are:
    0 = None
    1 = Movie
    2 = Music Video
    3 = TV Show

    (This can’t be changed if it’s a purchased video.)

    Remember to save as a .js file, and have some tracks selected in iTunes when you run it.

  • eve Says:

    the no-artwork script was perfect - thank you so much!

  • Scavenger Says:

    Hmm..trying RemoveDeadTracks and getting a
    “Library not registered” error?

  • Otto Says:

    “Library not registered” = Reinstall iTunes. iTunes sometimes fails to register itself as a COM object. Reinstall it and these scripts will then work.

  • Maarten Says:

    I don’t know what the “find dead tracks”-script exactly does, but when I click ‘collect library’ in the advanced menu (don’t know the exact name in English), I get an overview of the dead tracks as well. Can’t remove them with one click though…

  • nLoomis Says:

    Great scripst Otto!, im just entering to the world of iPod and iTunes, and your scripts are the best i have found so far.

  • Pablo Rodriguez Says:

    I’m trying to use your update file as instructed but I keep getting a Windows Script Host dialog box stating: ”
    with a syntax error, code: 800A03EA, Microsoft JScript Compiling error at Line 1, Char 1.

    Any ideas of what I’m doing wrong?

    Thank you.

  • [...] also has other javascripts that actually helped me make my own. I had to search for official iTunes class namings to figure [...]

  • [...] info from one of the albums. -sort by whether album artwork is present or not. Fix: Otto’s iTunes javascript. -sort by whether lyric tag is present (contains lyrics) or not. Fix: Playlist script -integrated [...]

  • Campbell Says:

    Do you have anything that put all the songs from the same artist into playlist.
    ie, eveything from Gomez into a playlist has eveything with Gomez in it
    Cheers

  • John Mahoney Says:

    Hi Otto,
    From your script: Reload/update all songs in the library from the file tags
    Can you give an example of the file tags?

  • Otto Says:

    John: MP3’s and other files have what are called “tags” in them that stores the information like the name of the song and artist and so forth. If you use some other program to manage those tags, then the update script makes iTunes notice changes to those tags.

  • John Mahoney Says:

    Hi Otto,
    Thanks for your quick reply.

    OK. Then can a script be written that updates the song info
    in itunes from data in a *.csv file.
    The name of the song can be in the first field and the rest
    of the info (artist, album, etc) in the following fields.
    And yes this is particularly useful for *.wav songs imported
    into itunes, not ripped from a CD.

    Thanks for your help,
    John Mahoney

  • John Mahoney Says:

    Hi Otto,

    Can a script be written that would import
    songs into itunes from a list in a file that
    contains the full path name of each song.
    Please add this to my wish list.

    Thanks for your help,
    John Mahoney

  • John Mahoney Says:

    Hi Otto,

    I’ve done a lot of programming but never in javascript are the 2 scripts I talked about
    doable and maybe spend some time learning the code? Or are they imposible and I’d
    just be wasting my time?

    Have any suggestions?
    John Mahoney

  • SteveG Says:

    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

  • Victor Feight Says:

    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

  • Curtis Says:

    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.

  • Otto Says:

    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.

  • Torben Says:

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

    Thanks

  • Otto Says:

    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.

  • Bettie Says:

    I desperately need a way to manually change the playcounts on my songs.
    Any help would be greatly appreciated.

  • Otto Says:

    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.

  • Randy Says:

    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

  • fran Says:

    Hello do you have a script for double track for windows/// remover of double soungs///

  • Figment Says:

    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.

  • PWAT Says:

    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.

  • jabp891 Says:

    confused…

    Can this get you your music from your ipod, back on to the computer??

    If so, how do you do it??

  • [...] chimpoid wrote an interesting post today onHere’s a quick excerpt [...]

  • username9876 Says:

    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.

  • Marlie Says:

    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?

  • Otto Says:

    Marlie: Look for wscript.exe on your computer, in your Windows folder. Associate the files with that.

  • Marlie Says:

    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.

  • Otto Says:

    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.

  • Quach Says:

    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

  • Otto Says:

    Quach: Google for iTunes COM SDK. Then download it and read it. Simple.

  • Quach Says:

    Awesome Thanks Otto,
    These Scripts work great.

  • jorkel Says:

    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.

  • [...] Javascript your iTunes — like Applescript for Windows! [...]

  • Andrew Says:

    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

  • Stuart Says:

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

  • username9876 Says:

    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.

  • Otto Says:

    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;

  • William Joyce Says:

    I use the free ActiveX/COM inspector from oaklandsoftware.com to expose the iTunes COM interface.

  • username9876 Says:

    Otto — Thanks very much. That works like a charm!

  • Daniel Aldous-Critchley Says:

    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

  • johnPaulVaughan Says:

    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.”);

  • johnPaulVaughan Says:

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

  • Otto Says:

    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.

  • PerfectVirus Says:

    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.

  • johnPaulVaughan Says:

    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.”);