Running scripts via command line don't do anything

14 views
Skip to first unread message

Trevin

unread,
Nov 7, 2009, 6:42:44 PM11/7/09
to Teridon's iTunes Programs
I'm trying to modify the "mm_rating_to_itunes" script to actually to
the opposite -- copy the iTunes rating to POPM frame.

This is useful for me because iTunes is used as my primary media
player due to my iphone, but I also heavily use Windows Media Player
(WMP) since I have a Windows 7 media center in my living room. WMP
supports ratings in the POPM frame, so I can get the best of both
worlds if I had an easy way to do this.

I modified the scripts to what I think will work, but when I run it,
nothing happens on the command line (I have activestate perl
isntalled). I thought it was my script, but when i run the normal
"mm_rating_to_itunes" it also does nothing. It executes and exits
cleanly with no errors msgs.

If I then run the "mm_rating_to_itunes" script from Teridon's EXE, it
works perfectly.

It's as if function within the script isn't executing properly. What
am I doing wrong? should I be passing parameters of some sort to teh
script when runnign by command line? I expected it to pickup on the
currently selected songs in iTunes like it does when I run it via the
Exe. HOwever, I'm new to perl and scripting against iTunes so it
might be something easy I'm overlooking.

Any help appreciated.

ter...@gmail.com

unread,
Nov 7, 2009, 11:32:34 PM11/7/09
to Teridon's iTunes Programs

The file "mm_rating_to_itunes.pl" doesn't do anything by itself,
really; it just defines a subroutine called "mm_rating_to_itunes".
That subroutine is called from "itunes_scripts.pl". The GUI passes
the name of subroutine to run (the script name without the ".pl") as
an argument to itunes_scripts.pl.

So, if you're trying to run your modified version (assuming you
haven't changed the name), then run "itunes_scripts.pl
mm_rating_to_itunes".

None of this will work if you changed the name of the script.

HTH,
Rob

Trevin

unread,
Nov 16, 2009, 11:58:02 PM11/16/09
to Teridon's iTunes Programs
I've actually changed the name of the file to
"itunes_rating_to_popm.pl", but I also added what I thought were the
right hooks into "itunes_scripts.pl" as well by:

- adding "require 'itunes_rating_to_popm.pl'
- adding entry to the %script_list array

What am I missing?

Robert Jacobson

unread,
Nov 17, 2009, 8:33:18 PM11/17/09
to teridons-itu...@googlegroups.com
Trevin wrote:
> I've actually changed the name of the file to
> "itunes_rating_to_popm.pl", but I also added what I thought were the
> right hooks into "itunes_scripts.pl" as well by:
>
> - adding "require 'itunes_rating_to_popm.pl'
> - adding entry to the %script_list array
>
> What am I missing?

I don't know, that sounds correct. What happens when you run
itunes_scripts.pl itunes_rating_to_popm

It should say (after the copyright):
"Running itunes_rating_to_popm"

If might help if you post the code changes you made.

--
Rob

Trevin

unread,
Nov 17, 2009, 10:13:05 PM11/17/09
to Teridon's iTunes Programs
On Nov 17, 5:33 pm, Robert Jacobson <teri...@gmail.com> wrote:
> I don't know, that sounds correct.  What happens when you run
>         itunes_scripts.pl itunes_rating_to_popm
>
> It should say (after the copyright):
>         "Running itunes_rating_to_popm"      

Yep, it shows that line so it looks like it's executing it. But after
seeing nothing happen to the songs,I decided to add in some debugging
code like 'print "foo";' in certain sections to know it was executing.
"foo" never got printed so I think there's something else afoot.

> If  might help if you post the code changes you made.

here's the code, modified from your original:

================
sub set_popm_rating($);

our %itunes_rating_to_popm_rating = (
20 => "23", # 1.0 stars
20 => "53", # 1.0 stars
40 => "64", # 2.0 stars
40 => "104", # 2.0 stars
60 => "128", # 3.0 stars
60 => "154", # 3.0 stars
80 => "196", # 4.0 stars
80 => "205", # 4.0 stars
100 => "252", # 5.0 stars
100 => "255", # 5.0 stars
);

sub itunes_rating_to_popm {

use strict;
use Win32::OLE;
use MP3::Tag;
use Data::Dumper;

## Create the OLE Object
my $iTunes = Win32::OLE->new('iTunes.Application') or die Win32::OLE-
>LastError();

my $tracks = $iTunes->SelectedTracks;

if (not defined $tracks) {
print "You must select some tracks in iTunes first!\n";
}

for (my $i = 1 ; $i <= $tracks->Count ; $i++ ) {
my $track = $tracks->Item($i);
my $kind_string = $track->KindAsString();
if ($kind_string =~ m/^MPEG/) {
&set_popm_rating($track);
} else {
print "Track " . $track->Name() . "does not appear to be an
MP3\n" . "Its kind is: " . $kind_string . "\n";
}
}

}

sub set_popm_rating ($) {

my $track= shift;
my $filename = $track->Location();
# my $filename = $ARGV[0];
# my $filename = "./MP3samples/BF2RealTone-1star.mp3";

my $rating;
my $itunesrating;
my $popmrating;
my $mp3 = MP3::Tag->new($filename);
$mp3->get_tags();

# print Dumper $mp3;
my $tag;

if (exists $mp3->{'ID3v2'} ) {
$tag = $mp3->{'ID3v2'};
} else {
print "ERROR: no ID3v2 tag exists in file $filename!\n";
}

my ($info, $name, @rest) = $tag->get_frame("POPM");

if (not defined $info) {
print "Error getting ID3v2 POPM tag for $filename, skipping\n";
return 0;
} else {
# POPM tag $info is a hash:
# {
# 'URL' => 'no@email',
# 'Counter' => 0,
# 'Rating' => 53
# };
$rating = $info->{'Rating'}; # get the current popm rating
$popmrating = $itunes_rating_to_popm_rating{$rating};
$itunesrating = $track->{'Rating'};

if (exists $itunes_rating_to_popm_rating{$rating} ) { #check that
the rating matches our defined translation
# only write POPM rating if it's different
print "rating is $rating\n";
print "popmrating is $popmrating\n";
print "itunesrating is $itunesrating\n";
if ($popmrating != $itunesrating) {
$track->{'Rating'} = $popmrating;
print "for track " . $track->Name . " with rating
$itunesrating, POPM rating is $popmrating\n";
}
} else {
print "ERROR: Got iTunes rating of $rating, for which no
translation exists\n";
}
}
}

1;
================

Robert Jacobson

unread,
Nov 18, 2009, 11:30:29 AM11/18/09
to teridons-itu...@googlegroups.com
Trevin wrote:
> Yep, it shows that line so it looks like it's executing it. But after
> seeing nothing happen to the songs,I decided to add in some debugging
> code like 'print "foo";' in certain sections to know it was executing.
> "foo" never got printed so I think there's something else afoot.
>
>> If might help if you post the code changes you made.
>
> here's the code, modified from your original:

While your code is syntactically correct, it doesn't do anything like what you
want it to do.

In fact, it looks like it will change the iTunes rating to the translated MM
rating number, which will probably break the iTunes ratings for anything over 2
stars.


To change the POPM tag, you have to use something like:
$mp3->set_id3v2_frame("POPM", ( "URL", $rating, $counter) );
$mp3->{ID3v2}->write_tag;


Also, the last time I checked, Media Monkey was using some very odd numbers for
the ratings. Supposedly these match what Windows Media Player is doing -- so
I'm not really surprised that the numbers don't make any sense.

I've attached a script that actually works (in my limited testing, anyway, with
iTunes 7.6.2.9 and Media Monkey 3.1.0.1256). I also released an updated scripts
package so that everyone can import ratings from iTunes into the POPM tag.
(Though, MediaMonkey already has an import ratings from iTunes function)

As usual, download from
http://www.pobox.com/~teridon/itunesscripts/index.html

--
Rob
itunes_rating_to_popm.pl

Trevin

unread,
Nov 19, 2009, 12:48:23 AM11/19/09
to Teridon's iTunes Programs
Thanks!

I didn't see

On Nov 18, 8:30 am, Robert Jacobson <teri...@gmail.com> wrote:
> I've attached a script that actually works (in my limited testing, anyway, with
> iTunes 7.6.2.9 and Media Monkey 3.1.0.1256).  I also released an updated scripts
> package so that everyone can import ratings from iTunes into the POPM tag.
> (Though, MediaMonkey already has an import ratings from iTunes function)
>
> As usual, download fromhttp://www.pobox.com/~teridon/itunesscripts/index.html

I went to http://mysite.verizon.net/teridon/itunesscripts/index.html
but didn't see an updated scripts package, nor did I see you post up
your new itunes_rating_to_popm script.The only new script I see is for
iTunes font size.

Robert Jacobson

unread,
Nov 19, 2009, 6:51:42 AM11/19/09
to teridons-itu...@googlegroups.com
Trevin wrote:
> I went to http://mysite.verizon.net/teridon/itunesscripts/index.html
> but didn't see an updated scripts package

Click the big "Download" button. I didn't post the new script as a separate
item, but it's available via the GUI in the scripts package.

--
Rob

Trevin

unread,
Nov 19, 2009, 11:44:47 AM11/19/09
to Teridon's iTunes Programs
Awesome, thanks!

I just tried out the source version you posted and it works like a
charm and indeed does what I need it to.

I have one small request -- can you add logic that checks if the POPM
frame is already set with the *same* rating, it shouldn't overwrite?
I don't want to needlessly change the files unless the rating truly is
different. This will allow allow me to bulk select all songs in iTunes
and run your script and it will only change files that it really needs
to. The way you're doing it now has a negative impact when you factor
in file backup strategy since the files will always be altered.

On Nov 19, 3:51 am, Robert Jacobson <teri...@gmail.com> wrote:
> Trevin wrote:
> > I went tohttp://mysite.verizon.net/teridon/itunesscripts/index.html
Message has been deleted

Trevin

unread,
Nov 21, 2009, 5:09:13 PM11/21/09
to Teridon's iTunes Programs
On Nov 19, 8:44 am, Trevin <tre...@trevinchow.com> wrote:
> I have one small request -- can you add logic that checks if the POPM
> frame is already set with the *same* rating, it shouldn't overwrite?
> I don't want to needlessly change the files unless the rating truly is
> different. This will allow allow me to bulk select all songs in iTunes
> and run your script and it will only change files that it really needs
> to.  The way you're doing it now has a negative impact when you factor
> in file backup strategy since the files will always be altered.

Rob:

I edited your script and got it to work for my needs now, which skips
any file that already has a matching POPM tag to avoid needless tag
updates. I also outputted a few more status messages along with a
counter to summarize how many songs were updated or skipped.


=======
sub set_popm_rating($);


our %itunes_rating_to_popm_rating = (
10 => "13", # 0.5 stars
20 => "23", # 1.0 stars
30 => "54", # 1.5 stars
40 => "64", # 2.0 stars
50 => "118", # 2.5 stars
60 => "128", # 3.0 stars
70 => "186", # 3.5 stars
80 => "196", # 4.0 stars
90 => "242", # 4.5 stars
100=> "252", # 5.0 stars
);

my $updatecounter = 0;
my $skippedcounter = 0;

sub itunes_rating_to_popm {


use strict;
use Win32::OLE;
use MP3::Tag;
use Data::Dumper;


## Create the OLE Object
my $iTunes = Win32::OLE->new('iTunes.Application') or die
Win32::OLE->LastError();


my $tracks = $iTunes->SelectedTracks;


if (not defined $tracks) {
print "You must select some tracks in iTunes first!
\n";
}


for (my $i = 1 ; $i <= $tracks->Count ; $i++ ) {
my $track = $tracks->Item($i);
my $kind_string = $track->KindAsString();
if ($kind_string =~ m/^MPEG/) {
&set_popm_rating($track);
} else {
print "Track " . $track->Name() . "does not
appear to be an MP3\n" . "Its kind is: " . $kind_string . "\n";
}
}
print "Songs updated: $updatecounter\n";
print "Songs skipped: $skippedcounter\n";


}


sub set_popm_rating ($) {

my $track= shift;
my $filename = $track->Location();
# my $filename = $ARGV[0];
# my $filename = "./MP3samples/BF2RealTone-1star.mp3";


my $rating;
my $itunesrating;
my $popmrating;
my $url;
my $counter;
my $mp3 = MP3::Tag->new($filename);
$mp3->get_tags();


# print Dumper $mp3;
my $tag;


print "File: $filename\n";

if (exists $mp3->{'ID3v2'} ) {
$tag = $mp3->{'ID3v2'};
} else {
print "ERROR: no ID3v2 tag exists in file!\n";
$skippedcounter++;
return;
}


# Get iTunes Rating
$itunesrating = $track->{'Rating'};
my ($info, $name, @rest) = $tag->get_frame("POPM");

if ($itunesrating == 0) {
print " Skipping, no iTunes rating\n";
print " Done\n";
$skippedcounter++;
return;
} else {
#print " iTunes rating is $itunesrating\n";
print " iTunes rating: " .
$itunes_rating_to_popm_rating{$itunesrating} . "\n";
print " POPM rating : " . $info->{'Rating'} .
"\n";
}

# Set POPM frame
if (exists $itunes_rating_to_popm_rating{$itunesrating} ) {
# default URL and counter
$url = "no\@email";
$counter = 0;
if (defined $info) {
#POPM tag already exists, use existing URL and
counter
#$url = $info->{'URL'};
#print " URL is $url\n";
#$counter = $info->{'Counter'};
#print " counter is $counter\n";
}
if ( $info->{'Rating'} == $itunes_rating_to_popm_rating
{$itunesrating}) {
#Skip
print " Skipping, matching POPM rating
already exists...\n";
print " Done\n";
$skippedcounter++;
return;
} else {
#Either no POPM rating yet exists or mismatched
value. Always favor iTunes rating and write that.
warn " Overwriting existing POPM rating of
" . $info->{'Rating'} . "\n";
print " Setting POPM frame\n";
$mp3->set_id3v2_frame("POPM", ($url,
$itunes_rating_to_popm_rating{$itunesrating}, $counter) );
print " Writing ID3v2 Tag\n";
$mp3->{ID3v2}->write_tag;
print " Done\n";
$updatecounter++;
}
} else {
print " ERROR: no translation for iTunes rating
$itunesrating\n";
$skippedcounter++;
}

}


1;


=======
Message has been deleted

Trevin

unread,
Nov 21, 2009, 5:28:55 PM11/21/09
to Teridon's iTunes Programs
I added one more change:

- Added error handling for catching when a track is selected in
iTunes
but does not exist in the file system. Turns out when this happens,
your script bails. So this will let it handle gracefully and continue
with the rest of the selected songs.


=============
sub set_popm_rating($);


our %itunes_rating_to_popm_rating = (
10 => "13", # 0.5 stars
20 => "23", # 1.0 stars
30 => "54", # 1.5 stars
40 => "64", # 2.0 stars
50 => "118", # 2.5 stars
60 => "128", # 3.0 stars
70 => "186", # 3.5 stars
80 => "196", # 4.0 stars
90 => "242", # 4.5 stars
100=> "252", # 5.0 stars
);

my $updatecounter = 0;
my $skippedcounter = 0;
my $notfoundcounter = 0;

sub itunes_rating_to_popm {


use strict;
use Win32::OLE;
use MP3::Tag;
use Data::Dumper;


## Create the OLE Object
my $iTunes = Win32::OLE->new('iTunes.Application') or die
Win32::OLE->LastError();


my $tracks = $iTunes->SelectedTracks;


if (not defined $tracks) {
print "You must select some tracks in iTunes first!
\n";
}


for (my $i = 1 ; $i <= $tracks->Count ; $i++ ) {
my $track = $tracks->Item($i);
my $kind_string = $track->KindAsString();
if ($kind_string =~ m/^MPEG/) {
&set_popm_rating($track);
} else {
print "Track " . $track->Name() . "does not
appear to be an MP3\n" . "Its kind is: " . $kind_string . "\n";
}
}
print "\n\n";
print "Songs updated: $updatecounter\n";
print "Songs skipped: $skippedcounter\n";
print "Songs notfound: $notfoundcounter\n";
print " -----\n";
print " Total: ";
print $updatecounter + $skippedcounter + $notfoundcounter .
"\n";


}


sub set_popm_rating ($) {

my $track= shift;
my $filename = $track->Location();
if ($filename eq "") {
warn "Song in iTunes, but can't be found in file system
\n";
$notfoundcounter++;
return;
=============
Reply all
Reply to author
Forward
0 new messages