huesler-informatik My 2 cents

Quicktip - Access .plist files with RubyCocoa

As part of the automated deployment process for a cocoa application, I needed to access a .plist file to change some values before the application gets bundled and uploaded.

Since plist files are plain XML files, I could have used something like libxml to get the job done but since this script runs on a Mac, I have access to RubyCocoa. That spares me the XML parsing and writing and gives me a simple persistent dictionary instead. This is what I ended up with:

I think that is pretty neat. However, I ran into a small pitfall when I tried to compare an NSString with a regular expression. I got this warning:

'NSString#=~' doesn't work correctly. Because it returns byte indexes. 
    Please use 'String#=~' instead.

The solution is pretty simple, just call to_s on the dictionary values.