#!/usr/bin/perl # get file URLs to look for @fileURLsString = @ARGV; @fileURLArgs = split(/ /, $fileURLsString[0]); @fileURLs = (); foreach $fileURL (@fileURLArgs) { $fileURL =~ s/\&/\&/g; push( @fileURLs, $fileURL ); } %in_fileURLs = (); for( @fileURLs ) { $in_fileURLs{$_} = 1 } # get locations for iTunes DBs @dbPaths = (); $pathsString = `defaults read com.apple.iApps iTunesRecentDatabasePaths`; @dbPathPieces = split(/\n/, $pathsString); foreach $potentialPath (@dbPathPieces) { next if( $potentialPath eq "(" || $potentialPath eq ")" ); if( $potentialPath =~ /^\s*/ ) { $potentialPath =~ s/^\s*//; } push( @dbPaths, $potentialPath ); } # expand tildes in paths @expandedDBPaths = (); foreach $path (@dbPaths) { if( $path =~ /^\".*\"$/ ) { $path =~ s/^\"//; $path =~ s/\"$//; } $path =~ s{ ^ ~ ( [^/]* ) } { $1 ? (getpwnam($1))[7] : ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>))[7] ) }ex; push( @expandedDBPaths, $path ); } @trackIDs = (); foreach $path (@expandedDBPaths) { chomp $path; $trackID = ""; $location = ""; open( FILE, $path ) || die "Can't open file: $path\n"; while() { if( $_ =~ /Track ID<\/key>(.*)<\/integer>/ ) { $trackID = $1; } elsif( $_ =~ /Location<\/key>(.*)<\/string>/ ) { $location = $1; if( $in_fileURLs{$location} ) { push( @trackIDs, $trackID ); $trackID = ""; $location = ""; } } } close FILE; } print "$_\n" foreach @trackIDs;