macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Using Perl to Manage Plist Files
Pages: 1, 2, 3, 4, 5

Getting Plist Values

Because $plist is a Cocoa object, you can't use this code to print its value:




$file = "/Library/Preferences/SystemConfiguration/preferences.plist";
$plist = NSDictionary->dictionaryWithContentsOfFile_( $file );
print $plist; # this will print: NSCFDictionary=SCALAR(0x180b5a8)

To get a useful printout of $plist, use the Cocoa method description(). This method will print out an ASCII-formated Plist structure, which is easily readable. The result of the description() method is an NSString, so you must convert it to a UTF8String.


$file = "/Library/Preferences/SystemConfiguration/preferences.plist";
$plist = NSDictionary->dictionaryWithContentsOfFile_( $file );
print $plist->description()->UTF8String() . "\n";

Printing out an NSDictionary, especially when it is as big as the network preferences, isn't very useful in a Perl script, but it makes debugging easier.

Getting the value at the root of the Plist is fairly easy. You use the method objectForKey_().


$file = "/Library/Preferences/SystemConfiguration/preferences.plist";
$plist = NSDictionary->dictionaryWithContentsOfFile_( $file );
$value = $plist->objectForKey_("CurrentSet");
print $value->description()->UTF8String() . "\n";

Now $value is something we can use. We can print it and we can make decisions based on its value. The object you get back can be a string, number, Boolean, date, or data, and we can read the values by using ->description()->UTF8String(). Or a value might be a dictionary or an array. If it is a dictionary or an array, you can continue to read the values.

To avoid the clutter of using ->description()->UTF8String(), we will just wrap that in a subroutine:


sub perlValue {
  my $object = ( @_ );
  return $object->description()->UTF8String();
}

Using a Library File

Throughout these two articles, several subroutines are going to be shown. Examples will use these subroutines but not show the subroutine definitions, as that would make for a long article with lots of duplicated text. An example is the subroutine just defined: perlValue. That subroutine will be used in just about every example from here on. You either want to paste the subroutine code into each example script, or you can define a library file that contains the subroutine definition.

The easiest way to create a library is to paste the subroutine code into a file, and put a 1; at the end of the file, like this:


use Foundation;

sub perlValue {
  my ( $object ) = @_;
  return $object->description()->UTF8String();
}

# more subroutines go here...

1;

In the scripts that use the library, you use the folder that contains the script, and you require the script. Suppose you named the file perlplist.pl and saved it in your Desktop folder. You would put this code into the scripts that use the subroutines.


use lib "/Users/yourname/Desktop/";
require "perlplist.pl";

The first example in the next section shows how to do this in detail.

Pages: 1, 2, 3, 4, 5

Next Pagearrow