Another reason I like Perl

brian_d_foy on 2003-11-30T20:06:05

I have the Robert Greenberg "How To Listen To and Understand Great Music" lecture series on 48 CDs, which I am importing into iTunes this weekend. While the first few CDs had entries in GraceNote (and I have talked about the pain of this before), most do not, so I need to label each track myself.

I have heard a lot of people complain about the sigils in Perl---those goofy characters before the variable name. They are so confusing!

Are they really more confusing than what I have to do to name variables in AppleScript (or Java, C++, Python, ...)? Since these things do not have special ways to denote variables, instead of naming the variable "index", which seems a good and proper name for a loop index, I have to find something else because "index", in AppleScript, is a keyword. In the same fashion I was bit by Python's "count", a good and proper name for a count of things. I end up putting a lot of possessives or articles in front of the name I really want, so they come out like "myIndex" or "theCount" ("Two glorious names, ah, ah, aaah!").

Witness:

tell application "iTunes"
	copy (a reference to (get view of front window)) to thisPlaylist
	
	set theLesson to the text returned of (display dialog "Enter the lesson:" default answer "")
	
	-- if no tracks are selected, use them all 
	if selection is {} then
		copy every file track of thisPlaylist to allTracks
	else
		copy selection to allTracks
	end if
	
	set listLength to length of allTracks
	set subList to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}
	
	set myIndex to 1
	repeat while myIndex ≤ listLength
		set aTrack to item myIndex of allTracks
		set aTrack's artist to "Robert Greenberg"
		set aTrack's album to "How To Listen To And Understand Great Music"
		set aTrack's name to "Lesson " & theLesson & (item myIndex of subList as string)
		set myIndex to myIndex + 1
	end repeat
	
end tell


Crying

pudge on 2003-12-04T00:28:34

This is just crying to be rewritten using Mac::Glue. :-)
#!/usr/local/bin/perl
use warnings;
use strict;
 
use Mac::Glue ':all';
 
my $itunes = new Mac::Glue 'iTunes';
 
$itunes->activate;
 
my $lesson = $itunes->display_dialog(
    'Enter the lesson:', default_answer => ''
)->{text_returned};
 
my $playlist  = $itunes->prop(view => window => 1);
my @selection = $itunes->prop('selection')->get;
my @tracks = @selection
    ? @selection
    : $playlist->obj('file_tracks')->get;
 
my @sublist = ('a' .. 'k');
 
for my $i (1 .. @tracks) {
    my $track = $tracks[$i - 1];
    $track->prop('artist')->set(to => "Robert Greenberg");
    $track->prop('album')->set(to => "How To Listen To And Understand Great Music");
    $track->prop('name')->set(to => "Lesson $lesson $i");
}
 
__END__
There's a slight bug somewhere in the new UTF handling in Mac::Glue that causes the artist and album to show up incorrectly in iTunes. I have what I think is a fix (checking Encode::is_utf8() before sending data to iTunes as Unicode) for the next version, but I am not altogether comfortable with it. Odd that iChat doesn't have the same problem with the same data when I send to it; I suspect iTunes might be handling it incorrectly.

Regardless, until the next version of Mac::Glue, you can do:
to => param_type(typeChar(), "Robert Greenberg"))

Re:Crying

pudge on 2003-12-04T03:29:29

Oh, and I guess that should be $sublist[$i-1]. And heck, make it 0..$#tracks so we can just use $i for both arrays. :-)

Re:Crying

brian_d_foy on 2003-12-04T04:27:20

I won't tell Andy that you didn't test it.

Re:Crying

pudge on 2003-12-04T08:04:20

I did test it, but I was just naming them 1 2 3, forgetting you wanted them a b c. :-)

How was that lecture?

perrin on 2005-02-26T18:03:06

I know this journal entry is ancient, but I was considering getting that same music lecture series and would appreciate a capsule review.

Re:How was that lecture?

brian_d_foy on 2005-02-26T23:56:39

I just finished Robert Greenberg's Operas of Mozart, which I think is much better partly because I've gotten over his speaking style. The information is compelling and interesting, but his speaking style and cadence grates on me. His comedy is poorly delivered, but he just keeps on trying.

That's just my opinion though. Everyone I know who has listened to Greenberg's levtures have been delighted by them.

I'll be in New York next week, so you can hear one of the lectures if you like. They're all on my laptop. :)

If you decide to buy them, you can find them for half-price on eBay.