Newsroom Update

Apple Music today announced the release of its 100 Best Albums of all time, a list crafted by Apple Music’s experts alongside industry professionals. Learn more >

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

Dec 22, 2013 6:00 AM in response to vaderag

I've doublechecked and the only values the script assigns are DiscNumber, DiscCount & TrackCount, and then only if the values need to change.


When using track-by-track prompting the effective album artist is shown if it is blank, i.e. Various Artists if this is a compilation, or the <Artist> otherwise, but the tag isn't changed. The effective album artist and the album title are used together to uniquely identify each album since this reflects the way that iTunes groups things. See Grouping tracks into albums for more.


If album artist is populating automatically as the tracks are updated then the most likely culprit is Windows Media Player. See Getting iTunes & Windows Media Player to play nicely for details. Note too that WMP's background tag changes are often responsible for clearing disc and track counts.


tt2

Dec 23, 2013 2:11 AM in response to turingtest2

Well, tried it on the whole library (about 20k songs) and it worked perfectly on all but 2 albums and i suspect that they didnt work properly due to incorrect tagging initially


Thanks so much for your help on this - saved me some serious time and meant that the wife might see me a bit over Christmas!! 😉


Have a fantastic Christmas yourself 🙂

Jan 11, 2014 4:26 AM in response to tree_frog

I have a similar problem. The metadata for track number in an album is showing up fine. The album has 15 tracks and they show up in Windows Explorer in the Track # field as 1, 2, 3... to 15. When I drag and drop these files to iTunes the bloody thing inexplicably lists the track number for the album as 1 of 14, 2 of 14 etc. until track 15 which is correctly showing as 15. How do I just get iTunes to display the metadata correctly? I've had trouble with 2 songs in this album and I had to re-download them so something got messed up along the way. Please help... I'm loosing my marbles with iTunes.

Jan 11, 2014 5:12 AM in response to Onychophoran

If we're only talking about fixing the odd album, select all the tracks of the album, press CTRL+I to Get Info, enter the total number of tracks in the right-hand box (Y) of the Disc X of Y section. Press OK.


If this is a feature of tree_frog's script which you want to use frequently then let us know. He might drop by with a fix, otherwise I could work on it, or build an equivalent script of my own.


tt2

Jan 11, 2014 8:07 PM in response to turingtest2

turingtest2 wrote:


If we're only talking about fixing the odd album, select all the tracks of the album, press CTRL+I to Get Info, enter the total number of tracks in the right-hand box (Y) of the Disc X of Y section. Press OK.


Onychophoran, if I understand your problem correctly, turingtest2's suggestion should work very well, without needing a script. I don't know of any other way to fix this problem.


Just to double-check: The problem you are describing is a problem with iTunes itself, and not with the script I posted at the start of this thread?

Jan 24, 2014 4:06 PM in response to tree_frog

As a side note, if anyone is looking for a *Mac* version of this script, here it is:

This is an AppleScript, which will run on any Mac without 3rd-party software.


To Save the Script:

1. Copy and paste the script into AppleScript Editor (found in the /Applications/Utilities folder).

2. Save the file with a name of your choice.


To Use the Script:

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

2. Open the script by double-clicking it in Finder.

3. Click the 'Run' button in the toolbar of the AppleScript Editor.


The Script:

tell application "iTunes"

set sel to selection

if sel is not {} then

set c to length of sel

repeat with i from 1 to c

set t to item i of sel

set the track number of t to i

set the track count of t to c

end repeat

end if

end tell


Tips:

1. If you place the script in the ~/Library/iTunes/Scripts folder, a script icon will appear in the menu bar of iTunes, allowing you to run it directly.

Aug 17, 2014 12:44 AM in response to tree_frog

I use Exact Audio Copy on Windows to rip all my CDs, and since I output to WAV files from there and then convert to Apple Lossless within iTunes, I don't get any of the track numbering. I've been looking for a solution like this for a while and this actually works, so thank you so much!


I just had a few comments for anyone else that wanders across this thread:

  1. I tried the original script just now (August 17, 2014) on iTunes 11.3.1.2 on Windows and it worked perfectly fine.
  2. It doesn't matter what you name the file.
  3. If you have changed the default program for opening JS files (like I have), then all you have to do is select the songs you want to run this script on, right-click on the JS file, and then click on Open with > Microsoft ® Windows Script Based Script Host.
  4. Not to be a stickler (that means I'm going to be!), but I do a lot of JS programming, so I tidied up the original script to make it more JS-like and past through JSLint without any errors. Here's the script:


/*global WScript */


var tracks,

i,

len,

currTrack;


try {

tracks = WScript.CreateObject('iTunes.Application').SelectedTracks;


for (i = 1, len = tracks.Count; i <= len; i += 1) {

currTrack = tracks.Item(i);


if (currTrack.Kind === 1) {

currTrack.TrackNumber = i;

currTrack.TrackCount = len;

}

}


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

WScript.Quit();

} catch (err) {

WScript.Echo('No tracks selected.');

}


It does exactly the same thing as the original script, but the code is a bit "cleaner".

Thanks again for this awesome script!


P.S. I couldn't figure out how to get the pretty block-quote formatting for the code, so I just used Courier New to format it. Sorry.

Aug 21, 2014 10:00 AM in response to tree_frog

Hi, I have Win 7 latest version of iTunes.

I copied the script as shown on the original (page 1)

in notepad, saved as shown, and made a playlist in iTunes.

I highlighted the songs, double clicked the scrip, it told me no tracks were selected.

Also, when I copied the script, it did not copy even to the sides as your looks, mine started more in the middle

Any suggestions? Thanx

Dec 7, 2014 9:04 AM in response to tree_frog

David - I just tried it, and nothing happens. I'm running Windows 7 and iTunes 12.x. Even though I copied it correctly and named it correctly, it just opens the NotePad file when I double-click it. I then associated the file with "Open With" and found Java.exe, but now when I double-click it, it quickly opens a DOS (black) window for about a half a second, then it disappears, and nothing happens to my track names. Any ideas?

Dec 7, 2014 12:25 PM in response to leslyons

leslyons, java.exe is going to be for Java programs which is very different than JavaScript.


Which script did you use, the original or the one from tree_frog on Jun 8, 2013? That post also has a VB Script...it sounds like your file association to run the .js files isn't working so try the .vbs file instead.

Another trick is to open a command prompt, and run the script from there. That way if it shows an error you'll still see it on the screen, whereas if you run it directly the command prompt window will close immediately.

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.