iTunes Scripts

To use a script, simply copy the code, and paste into a new text document, then rename the file to the name of the script. Make sure the file extension is renamed to .vbs or .js (whichever is specified in the script name). For help on doing this, go here.

Mac users, check out Doug’s Applescripts.

Download a .zip of the scripts here.

Please feel free to contribute your own scripts… send them to everythingitunes@gmail.com

AlarmClock.vbs
Begins playing Party Shuffle.

ClearLyrics.js
Wipes the lyrics from all tags in every file in iTunes.

CreateAlbumPlaylist.js
Makes a new playlist for every album.

DisplayVersion.vbs
Displays Version Number of iTunes.

FindDeadTracks.js
Creates a “Dead Tracks.txt” containing the names of all ‘dead’ or ‘lost’ files in iTunes.

NameThatiTune.vbs
Game where you think of the name of the song and find out if you’re correct.

NoArtworkPlaylist.js
Makes a new playlist of all tracks without artwork.

NoLyricsPlaylist.js
Makes a new playlist of all tracks without lyrics.

OneHitWonders.js
Makes a playlist of tracks which are the only one for their artist.

RemoveDeadTracks.js
Deletes missing tracks from your library. Irreversible. Use with caution!

RemoveUserPlaylists.js
Deletes all manually-created playlists. (not Auto-Playlists).

Update.js
Imports information from tags of the music files into the iTunes library.

AlarmClock.vbs <TOP>
Begins playing Party Shuffle.
NOTE: Script written by RipperDoc

Set objApp = CreateObject(“iTunes.Application”)
Set colSources = objApp.Sources
Set objSource = colSources.ItemByName(“Library”)
Set colPlaylists = objSource.Playlists
Set objPlaylist = colPlaylists.ItemByName(“Party Shuffle”)
objPlaylist.PlayFirstTrack

ClearLyrics.js <TOP>
Wipes the lyrics from all tags in every file in iTunes.
NOTE: Script written by Otto

var ITTrackKindFile = 1;
var iTunesApp = WScript.CreateObject(“iTunes.Application”);
var tracks = iTunesApp.LibraryPlaylist.Tracks;
var numTracks = tracks.Count;
var i;
for (i = 1; i <= numTracks; i++)
{
var currTrack = tracks.Item(i);
// is this a file track?
if (currTrack.Kind == ITTrackKindFile)
{

try {
if (currTrack.Lyrics != “”)
{
currTrack.Lyrics = “”;
}
}
catch(er)
{
// ignore these errors
}

}
}
WScript.Echo(“All Lyrics Cleared!”)

CreateAlbumPlaylist.js <TOP>
Makes a new plalylist for every album.
NOTE: Taken directly from the Apple iTunes SDK

var iTunesApp = WScript.CreateObject(“iTunes.Application”);
var mainLibrary = iTunesApp.LibraryPlaylist;
var mainLibrarySource = iTunesApp.LibrarySource;
var tracks = mainLibrary.Tracks;
var numTracks = tracks.Count;
var numPlaylistsCreated = 0;
var i;

// FIXME take a -v parameter eventually
var verbose = false;

// first, make an array indexed by album name
var albumArray = new Array();

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

if ((album != undefined) && (album != “”))
{
if (albumArray[album] == undefined)
{
if (verbose)
WScript.Echo(“Adding album ” + album);
albumArray[album] = new Array();
}

// add the track to the entry for this album
albumArray[album].push(currTrack);
}
}

for (var albumNameKey in albumArray)
{
var albumPlayList;
var trackArray = albumArray[albumNameKey];

if (verbose)
WScript.Echo(“Creating playlist ” + albumNameKey);

numPlaylistsCreated++;

albumPlaylist = iTunesApp.CreatePlaylist(albumNameKey);

for (var trackIndex in trackArray)
{
var currTrack = trackArray[trackIndex];

if (verbose)
WScript.Echo(” Adding ” + currTrack.Name);

albumPlaylist.AddTrack(currTrack);
}
}

if (numPlaylistsCreated == 0)
{
WScript.Echo(“No playlists created.”);
}
else if (numPlaylistsCreated == 1)
{
WScript.Echo(“Created 1 playlist.”);
}
else
{
WScript.Echo(“Created ” + numPlaylistsCreated + ” playlists.”);
}

DisplayVersion.vbs <TOP>
Displays Version Number of iTunes.

Set objApp = CreateObject(“iTunes.Application”)
Wscript.Echo “Version: ” & objApp.Version

FindDeadTracks.js <TOP>
Creates a “Dead Tracks.txt” containing the names of all ‘dead’ or ‘lost’ files in iTunes.
NOTE: Script written by Otto

var ITTrackKindFile = 1;
var iTunesApp = WScript.CreateObject(“iTunes.Application”);
var deletedTracks = 0;
var mainLibrary = iTunesApp.LibraryPlaylist;
var tracks = mainLibrary.Tracks;
var numTracks = tracks.Count;
var i;

var fso, tf;
fso = new ActiveXObject(“Scripting.FileSystemObject”);
tf = fso.CreateTextFile(“Dead Tracks.txt”, true);

while (numTracks != 0)
{
var currTrack = tracks.Item(numTracks);

// is this a file track?
if (currTrack.Kind == ITTrackKindFile)
{
// yes, does it have an empty location?
if (currTrack.Location == “”)
{
// write info about the track to a file
tf.WriteLine(currTrack.Artist + “,” + currTrack.Album + “,” + currTrack.Name);
deletedTracks++;
}
}

numTracks–;
}

if (deletedTracks > 0)
{
if (deletedTracks == 1)
{
WScript.Echo(“Found 1 dead track.”);
}
else
{
WScript.Echo(“Found ” + deletedTracks + ” dead tracks.”);
}
}
else
{
WScript.Echo(“No dead tracks were found.”);
}
tf.Close();

NameThatiTune.vbs <TOP>
Game where you think of the name of the song and find out if you’re correct.

Set objApp = CreateObject(“iTunes.Application”)

Set objPlaylist = objApp.LibraryPlaylist
Set colTracks = objPlaylist.Tracks

intTracks = colTracks.Count

intHighNumber = intTracks
intLowNumber = 1

Randomize
intNumber = Int((intHighNumber – intLowNumber + 1) * Rnd + intLowNumber)

Set objSong = colTracks.Item(intNumber)
objSong.Play
Wscript.Echo “Guess the name of this song… Click OK for the answer…”
Wscript.Sleep 1000

Wscript.Echo “SONG: ” & objSong.Name
Wscript.Echo “ARTIST: ” & objSong.Artist

NoArtworkPlaylist.js <TOP>
Makes a new playlist of all tracks without artwork.
NOTE: Script written by Otto

var iTunesApp = WScript.CreateObject(“iTunes.Application”);
var tracks = iTunesApp.LibraryPlaylist.Tracks;
var numTracks = tracks.Count;
var i;
NoArtPlaylist = iTunesApp.CreatePlaylist(“No Artwork”);

for (i = 1; i <= numTracks; i++)
{
var currTrack = tracks.Item(i);
if ( currTrack.Artwork.Count == 0 )
NoArtPlaylist.AddTrack(currTrack);
}

NoLyricsPlaylist.js <TOP>
Makes a new playlist of all tracks without lyrics.
NOTE: Script written by Otto

var iTunesApp = WScript.CreateObject(“iTunes.Application”);
var tracks = iTunesApp.LibraryPlaylist.Tracks;
var numTracks = tracks.Count;
var i;
NoLyricsPlaylist = iTunesApp.CreatePlaylist(“No Lyrics”);

for (i = 1; i <= numTracks; i++)
{ try {
var currTrack = tracks.Item(i);
if ( currTrack.Lyrics == “” )
NoLyricsPlaylist.AddTrack(currTrack);
}
catch(er)
{
}
}

OneHitWonders.js <TOP>
Makes a playlist of tracks which are the only one for their artist.
NOTE: Script written by Otto

// put your playlist name here
var PlaylistName = “One Hit Wonders”;

var iTunesApp = WScript.CreateObject(“iTunes.Application”);

var mainLibrary = iTunesApp.LibraryPlaylist;
// if you want to use some other playlist as the base to look through, uncomment this next line
// and change the “-All Songs” to the base playlist you want to use.:
//var mainLibrary = iTunesApp.LibrarySource.Playlists.ItemByName(“-All Songs”);

// — no need to change anything below this line —
var tracks = mainLibrary.Tracks;
var numTracks = tracks.Count;
var i;
var artistArray = new Array();
for (i = 1; i <= numTracks; i++)
{
var currTrack = tracks.Item(i);
var artist = currTrack.Artist;

if ((artist != undefined) && (artist != “”))
{
if (artistArray[artist] == undefined)
{
artistArray[artist] = new Array();
}
artistArray[artist].push(currTrack);
}
}
OHWPlaylist = iTunesApp.CreatePlaylist(PlaylistName);
for (var artistNameKey in artistArray)
{
var trackArray = artistArray[artistNameKey];

if (trackArray.length == 1)
{
var currTrack = trackArray[0];
OHWPlaylist.AddTrack(currTrack);
}
}

RemoveDeadTracks.js <TOP>
Deletes missing tracks from your library. Irreversible. Use with caution!
NOTE: Taken directly from the Apple iTunes SDK

var ITTrackKindFile = 1;
var iTunesApp = WScript.CreateObject(“iTunes.Application”);
var deletedTracks = 0;
var mainLibrary = iTunesApp.LibraryPlaylist;
var tracks = mainLibrary.Tracks;
var numTracks = tracks.Count;
var i;

while (numTracks != 0)
{
var currTrack = tracks.Item(numTracks);

// is this a file track?
if (currTrack.Kind == ITTrackKindFile)
{
// yes, does it have an empty location?
if (currTrack.Location == “”)
{
// yes, delete it
currTrack.Delete();
deletedTracks++;
}
}

numTracks–;
}

if (deletedTracks > 0)
{
if (deletedTracks == 1)
{
WScript.Echo(“Removed 1 dead track.”);
}
else
{
WScript.Echo(“Removed ” + deletedTracks + ” dead tracks.”);
}
}
else
{
WScript.Echo(“No dead tracks were found.”);
}

RemoveUserPlaylists.js <TOP>
Deletes all manually-created playlists (not Auto-Playlists).
NOTE: Taken directly from the Apple iTunes SDK

var ITPlaylistKindUser = 2;
var iTunesApp = WScript.CreateObject(“iTunes.Application”);
var deletedPlaylists = 0;
var mainLibrary = iTunesApp.LibrarySource;
var playlists = mainLibrary.Playlists;
var numPlaylists = playlists.Count;

while (numPlaylists != 0)
{
var currPlaylist = playlists.Item(numPlaylists);

// is this a user playlist?
if (currPlaylist.Kind == ITPlaylistKindUser)
{
// yes, is it a dumb playlist?
if (!currPlaylist.Smart)
{
try
{
// yes, delete it
currPlaylist.Delete();
deletedPlaylists++;
}
catch (exception)
{
// ignore errors (e.g. trying to delete a locked playlist)
}
}
}

numPlaylists–;
}

if (deletedPlaylists > 0)
{
if (deletedPlaylists == 1)
{
WScript.Echo(“Removed 1 user playlist.”);
}
else
{
WScript.Echo(“Removed ” + deletedPlaylists + ” user playlists.”);
}
}
else
{
WScript.Echo(“No user playlists were removed.”);
}

Update.js <TOP>
Imports information from the tags of the music files to the iTunes library.
NOTE: Taken directly from the Apple iTunes SDK

var iTunesApp = WScript.CreateObject(“iTunes.Application”);
var tracks = iTunesApp.LibraryPlaylist.Tracks;
var numTracks = tracks.Count;
var i;
for (i = 1; i <= numTracks; i++)
{
var currTrack = tracks.Item(i);
currTrack.UpdateInfoFromFile();
}

Disclaimer: No liability resides with the creator of these scripts. If you break your music library or damage your computer in any way, you’ve been warned! However, you should be aware that in almost every case of a script not working, an error message is received or iTunes crashes. No data is lost.

Advertisements

14 Responses to iTunes Scripts

  1. kap says:

    This information is very helpful. However, it would be useful if you also included what to do with the script once you save it. This information is apparently no where (on this site, at least).

  2. yanoff says:

    This was helpful. I used it to write iTunesStats, a VB script that analyzes one’s iTunes library and prints out interesting statistics. Please consider checking it out at http://www.iTunesStats.com. I’m looking for any feedback on the script as well.

  3. Ali Bengali says:

    Hi all ! These scripts are great !!! I often changed some metadata of my mp3s, added covers and then i had to doubleclick every file in itunes to update the Information in itunes. Now i used the update script and it saves me 1h stupid doubleclicking 🙂 Happy – now i can do other nice things with my saved time.

    • Ali Bengali says:

      Oh – just let me say two words:
      1.The quotation marks are not correct when copying the code into my notepad and save as .js file.
      2. I put some “try catch” around the “currTrack.UpdateInfoFromFile” because in some case itunes was not able to do the update. Easily:

      try {
      currTrack.UpdateInfoFromFile();
      }
      catch(er)
      {
      // ignore errors
      WScript.Echo(“Error with File ” && currTrack.Name);
      }

  4. Shawna says:

    My programmer is trying to persuade me to move
    to .net from PHP. I have always disliked the idea because of the expenses.

    But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and am worried about switching to another platform.
    I have heard very good things about blogengine.net. Is there
    a way I can transfer all my wordpress posts into it? Any help would be greatly
    appreciated!

  5. vivek chauhan says:

    Is any script to create iPhone backup through iTunes.

  6. An Ruano says:

    Obviously essentially the most excellent strategy to stay away from getting selected up by
    a contact reaper isn’t to place your email anywhere over a website and also on your own own.
    If getting antispam software sounds overwhelming, studying about the subject could help.
    Some classes may necessitate large file downloads or video streams which will please take a virtual eternity to download which
    has a regular dial-up modem.

  7. […] few JS samples. Here are a couple of links to other sources of Javascript for iTunes. OttoDestruct EverythingiTunes Brian […]

  8. My coder is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year
    and am anxious about switching to another platform.
    I have heard great things about blogengine.net. Is there a
    way I can import all my wordpress posts into it? Any help
    would be greatly appreciated!

  9. Great write-up, I am normal visitor of one?¦s web site, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time.

  10. Jose R Sousa says:

    this garbage doesn’t work

  11. Jose R Sousa says:

    iTunes is the shittiest application ever, it doesn’t update the tags if you change them outside of this shitty app, no matter what you do.
    Maybe if you drown it in the ocean.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: