Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automatically Number Songs in iTunes

Hello All,


iTunes currently has no built-in function to automatically edit the track numbers of multiple songs. If you use a Mac, there is an AppleScript called "Albumize Selection" that accomplishes this task, but after many, many hours spent searching the internet, I was unable to find a solution for Windows. With my limited programming knowledge, I was able to write a simple script that will accomplish this task, and I thought I would share it with everyone.


This script (JavaScript) runs on the built-in Windows Script Host - it does not require any third party software. The script will assign consecutive track numbers to the songs selected in iTunes, and will also update the total track count. For example, if you select 20 songs in iTunes, and run the script, it will number them 1 of 20, 2 of 20, 3 of 20, etc. I am running Windows 7, but it should work on any modern version of Windows.


To Save the Script:

1. Copy and paste the script into NotePad.

2. Save the file as MyFile.js ***It is very important that you type the extension .js, because this tells Windows that the file is a script!***.


To Use the Script:

1. Select the songs in iTunes (Click the first song, and then Shift-Click the last song).

2. Double-Click the JavaScript file in Windows Explorer.


The Script:

try

{

var Tracks = WScript.CreateObject("iTunes.Application").SelectedTracks;

var i = 0;

var j = Tracks.Count;

var CurrentTrack;


while (i != j)

{

i++;

CurrentTrack = Tracks.Item(i);

if (CurrentTrack.Kind == 1)

{

CurrentTrack.TrackNumber = i;

CurrentTrack.TrackCount = j;

}

}

}


catch(err)

{

WScript.Echo("The script could not be executed. No tracks were selected.");

}


WScript.CreateObject("WScript.Shell").AppActivate("iTunes");

WScript.Quit();


Tips:

1. You can save the script anywhere on your computer; I keep mine in the iTunes folder.

2. You can create a Windows shortcut to the script. This allows you to set the shortcut's icon, and allows you to assign a keyboard shortcut (such as Ctrl-Alt-N).

3. You can use iTunes while the script is running.



I hope people found this helpful! Let me know if you have any questions!

- David

iTunes for Windows-OTHER, Windows 7, Windows Script for iTunes

Posted on May 30, 2012 6:40 PM

Reply
54 replies

Feb 15, 2013 10:45 AM in response to SteveYates

No private messaging here, but if you post to any thread I've contibuted to I will see it.


That would work, but perhaps better to comment out the line in the "Main Program" section that starts GetInput and add a line below reading Input=-1 - then you can revert back when you want to use the regular version of the script. Note you don't have to work one album at a time. If you have multiple albums all of which need the same correction, i.e. reduce track number by one, then you can select all the tracks of the relevant albums and do them in one go. If you've randomly corrected some albums and not others then you'll probably want to drag the affected albums into a playlist, then fix them all in one hit.


tt2

May 28, 2013 6:56 PM in response to tree_frog

Hi,


Some help for people who have trouble with apparently random track ordering:


No matter what I tried, in iTunes 11.0 I couldn't get the track numbering order to change. I sorted on every different available column with no change in the order of the track numbers. However, during my experimenting, I noticed that the track order seems to go strictly by the order of the Date Added column.


The only way to change the track numbering would be to remove the tracks from the iTunes library and re-add them, making sure that they are added in the required order.


Hope this helps.

Jun 8, 2013 2:11 PM in response to njkeng

Hi Guys. I know I haven't replied in a while - I switched to a Mac not long after the original post, and haven't been able to replicate the problem some of you are reporting. I don't run into any problems using the script on my old Windows machine (which is still running iTunes 10.7).


For those having problems with the script, I have a couple of options for you to try out. First, I've tried modifying the original JavaScript so that it numbers tracks starting from the back of the list - this may help avoid iTunes shuffling the tracks around while the script is running. Here's the updated script (follow the same instructions as in the original post):


Updated JavaScript

try

{

var Tracks = WScript.CreateObject("iTunes.Application").SelectedTracks;

var n = Tracks.Count;

var i = n;

var CurrentTrack;


while (i != 0)

{

CurrentTrack = Tracks.Item(i);

if (CurrentTrack.Kind == 1)

{

CurrentTrack.TrackNumber = i;

CurrentTrack.TrackCount = n;

}

i--;

}

}


catch(err)

{

WScript.Echo("The script could not be executed. No tracks were selected.");

}


WScript.CreateObject("WScript.Shell").AppActivate("iTunes");

WScript.Quit();



If that doesn't work, I have also written a Visual Basic script (I am more familiar with VB and find it much easier to read). Visual Basic is just another programming language, and like JavaScript, will already be installed with Windows. The procedure for using the script is the same as with JavaScript, except that the file must be saved with the .vbs extension.


Visual Basic Script (Save the script as filename.vbs)

Set SelectedTracks = CreateObject("iTunes.Application").SelectedTracks

iTTrackKindFile = 1


If Not(SelectedTracks Is Nothing) Then

i = 0

j = SelectedTracks.Count


For Each Track In SelectedTracks

i = i + 1

If Track.Kind = iTTrackKindFile Then

Track.TrackNumber = i

Track.TrackCount = j

End If

Next

Else

WScript.Echo("The script could not be executed. No tracks were selected.")

End If


CreateObject("WScript.Shell").AppActivate("iTunes")



For those of you who try the scripts, let me know if it works out. If it doesn't, I have one final thing I can suggest, but it exceeds my programming ability to implement.


David

Dec 18, 2013 6:10 AM in response to Sly Bob

Might be a bit late to the party here, but I'm looking for a similar script, but that will only populate Track Count and Disc Count - i.e all my track numbers are correct, and those albums with multiple discs are correct in terms of disc #


The problem I have is that those with track numbers, I am generally missing the 'of x' so would like to auto populate these on a disc/album basis, based upon the highest track number.

Then when it comes to disc no's, on 1 disc generally don;t have a disc number or disc count, so I would automatically like to label these disc 1 of 1, and those with multiple discs generally are missing the 'of x' - so I would like to automatically populate this with the highest disc number in that album.


Anyone know of a script like this? (for windows)

Dec 18, 2013 7:00 AM in response to vaderag

That would be feasible, provided the selection of tracks you run the script on includes all the tracks of each included album... It would need two passes. the first would identify the highest track number for each unique value of Album & AlbumArtist & DiscNo (using artist where album artist is blank and disc=1 if empty) and the highest disc number for each unique value of Album & AlbumArtist, the second pass would then apply the values to each track in turn.


Nag me if I haven't got it done by tomorrow.


tt2

Automatically Number Songs in iTunes

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.