Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Jul 19, 2017 9:12 AM in response to turingtest2

What I've done is work with disc 1, then disc 2, disc 3 and so on. I think it's worked reliably with disc 1, and then not with subsequent discs. In several cases the library shows songs 1 through 37 (for example), all on disc 1 - but there are sometimes other types of errors. What sequence of steps would you suggest that would handle this? thanks!

Jul 19, 2017 9:26 AM in response to jbrigger

If you arrange the whole album in sequence ordered manually for say a three disc album you can then select all of the tracks of the first disc, get info, and set Disc 1 of 3, then with the second disc set Disc 2 of 3, and so on. Once the disc numbers are in place you can select the whole album and use tree_frog's script to number the tracks from 1 to <no. of tracks>. This assumes you have complete albums and don't need to leave spaces for any missing tracks.


tt2

Dec 27, 2012 11:05 PM in response to SteveYates

SteveYates wrote:


On my iTunes 11 the script functions however it numbers the tracks in random order not the order in which they are listed in iTunes.


Make sure that the tracks are sorted by play order (ascending), and that the tracks are not shuffled.


Look at the picture I supplied with the original post – it shows which column should be used to sort the tracks.

Dec 28, 2012 7:59 PM in response to tree_frog

Hi tree_frog,


I wonder if it's because your original picture has no track numbers to start. Somehow many of my imported albums have their numbering off by one, for example have tracks numbered 2-11 (no track one) instead of 1-10. If you select 5-6 already-numbered tracks and try to renumber them, does it work for you? For me it seems to work for three to four tracks and above that it starts getting them out of order, like iTunes starts reordering the Tracks.Item collection partway through, as it goes along.

Jan 3, 2013 8:53 PM in response to SteveYates

Hmm... It works fine for me under all conditions, even if the tracks have (incorrect) track numbers already in place. I'm still using iTunes 10.7, so maybe it is a bug with iTunes 11?


You're sure that you have the tracks sorted by the same column as shown in the image? If it's still not working once Apple releases iTunes 11.1, let me know.

Feb 13, 2013 2:09 AM in response to SteveYates

SteveYates wrote:


Hi tree_frog,


I wonder if it's because your original picture has no track numbers to start. Somehow many of my imported albums have their numbering off by one, for example have tracks numbered 2-11 (no track one) instead of 1-10. If you select 5-6 already-numbered tracks and try to renumber them, does it work for you? For me it seems to work for three to four tracks and above that it starts getting them out of order, like iTunes starts reordering the Tracks.Item collection partway through, as it goes along.


Possibly a little late in the day, but I have a script called AddToTrackNumber which can add or subtract from the current value for each selected track. I use it for renumbering second & subsequent discs of multi-disc albums.


tt2

Feb 14, 2013 8:38 PM in response to turingtest2

Hi turingtest2, thanks for the pointer. I tested both scripts with iTunes 11.0.1.12. Yours worked fine in several albums, and interestingly the track listing seems to change/update as it goes along. Tree_frog's script ended with the second track in last position on that one test, typical of what I saw before.


I'm not familiar with Apple's API at all but as a programmer I don't see any logic errors in tree_frog's script. My wild guess is that every so often, hence partway through the script, iTunes 11 decides to "update" the object containing the tracks and reorders them by the new numbers, so all the numbers are then off from then on as the script tries to iterate through the rest of them.


I honestly don't remember at what point my tracks all got off by one number, but decreasing the track numbers is exactly what I would need to do, so thanks.


Message was edited by: SteveYates

Feb 15, 2013 2:30 AM in response to SteveYates

I think the main difference between tree_frog's script and mine is that his assigns numbers on the basis of the position in the selection (which can potentially change while the script is running) whereas mine adds or subtracts a value from the current track number. Often with iTunes scripts I process the selection from last to first in case the update causes the updated item to vanish from the playlist it was in.


tt2

Feb 15, 2013 8:50 AM in response to turingtest2

It could be that for iTunes 10 and earlier "WScript.CreateObject("iTunes.Application").SelectedTracks" creates a copy of the selected tracks, and in iTunes 11 it links directly to the iTunes object which is reordered during updating. Weirdly it seemed to work for a few tracks but if I picked more than that it ran off the rails.


I don't see a way to send private messages, but is there a way in your script to fix the step at -1 or put it on a command line? That would save me a bunch of clicks since I have a couple hundred imported CDs to renumber. 🙂 Perhaps if I modify Sub GetInput to be just "Input=-1"?

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.