Grepping plist files

I’m trying to track down why some of my attachments keep being downloaded to ~/Library/Mail Downloads/ even after I have set my Downloads directory in Mail.app to ~/Downloads/. I haven’t found it yet, but in the process I wrote a quick script for searching for a regular expression in .plist files, converting them to XML automatically if needed.

Here it is, use at your own risk (Download link)

#!/usr/bin/perl
# Usage: plistgrep regex file.plist [...]

use strict;

my $regex=qr($ARGV[0]);

shift @ARGV;

foreach my $i (@ARGV) {
  plistgrep($regex, $i);
}

sub plistgrep {
  my $r=shift;
  my $file=shift;
  my @text=plistread($file);
  print map { "$file: $_" } grep(/$r/, @text);
}

sub plistread {
  my $file=shift;
  my $type=`/usr/bin/file "$file"`;
  my @result;
  if ($type =~ /Apple binary property list/) {
    open F, "plutil -convert xml1 -o - '$file' |"
      or die "Error running plutil command: $!\n";
  }
  else {
    open F, "<$file"
      or die "Error opening file $file: $!\n";
  }
  @result=<F>;
  close F;
  return @result;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s