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 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)

# 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";
  close F;
  return @result;

Tags: · · ·