BrT

Musings about technology and other neat stuff

BrT random header image

Grepping plist files

April 28th, 2008 · No Comments

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;
}

Tags: · · ·