Help with editing a plist file

Hello,


I'm new to scripting and would appreciate any help I can get.


I need to create a shell script that will modify one part of an AD plugin plist file in 10.7. The plist file contains, among other things, this part which is supposed to contain GUID's for AD security groups:


<dict>

<key>ActiveDirectory</key>

<dict>

<key>network admin groups</key>

<array/>

</dict>



I need the file to be modified to contain this, instead:


<dict>

<key>ActiveDirectory</key>

<dict>

<key>network admin groups</key>

<array>

<string>exampleGUID-1</string>

<string>exampleGUID-2</string>

</array>

</dict>



I need to modify it through a shell script or unix command so that it can be automated and run on many machines, without modifying the rest of the file that contains other data unique to each machine.


Here's what I've been playing around with... it will remove the existing entry, and create a string with value "test" properly, but I am stuck on how to create an array containing multiple strings.


#!/bin/bash


#Deletes the existing network admin groups entry

/usr/libexec/PlistBuddy -c "Delete :ActiveDirectory:'network admin groups'" /path/to/file/ourdomain.plist


#Adds entry and sets string to "test"

/usr/libexec/PlistBuddy -c "Add :ActiveDirectory:'network admin groups' string test" /path/to/file/ourdomain.plist



Anybody have suggestions for how this could be done? I'm sure this is simple for somebody out there 🙂


Thanks!

Xserve, Mac OS X (10.7.2)

Posted on Jan 7, 2012 8:34 PM

Reply
4 replies

Jan 7, 2012 10:01 PM in response to drewcubs

Have you read the man page? you work with arrays by using colon delimited lists using a zero-based index. so you'd use the following to add an entry to the beginning of the list:


/usr/libexec/PlistBuddy -c "Add :ActiveDirectory:'network admin groups':0 string exampleGUID-1" /path/to/file/ourdomain.plist


I haven't actually tested it (and I'm more used to modifying plists using AppleScript) but I think this is in the right direction

Jan 7, 2012 10:38 PM in response to twtwtw

Thanks for the reply.


Including the <:0> at the end of the index creates a sub domain with a key of "0". It results in this:


<key>network admin groups</key>

<dict>

<key>0</key>

<string>exampleGUID-1</string>

</dict>



I need it to create an array like so:


<key>network admin groups</key>

<array>

<string>exampleGUID-1</string>

<string>exampleGUID-2</string>

</array>

Jan 9, 2012 2:30 PM in response to twtwtw

Awesome- Thanks for your help... I knew I was just missing something small.


I got it working using the following:


#!/bin/bash


#Deletes the existing entry

/usr/libexec/PlistBuddy -c "Delete :ActiveDirectory:'network admin groups'" /path/to/file/ourdomain.plist


#Adds array element

/usr/libexec/PlistBuddy -c "Add :ActiveDirectory:'network admin groups' array" /path/to/file/ourdomain.plist


#Adds 1st entry and sets string

/usr/libexec/PlistBuddy -c "Add :ActiveDirectory:'network admin groups':0 string test" /path/to/file/ourdomain.plist


#Adds 2nd entry and sets string

/usr/libexec/PlistBuddy -c "Add :ActiveDirectory:'network admin groups':1 string test two" /path/to/file/ourdomain.plist

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Help with editing a plist file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.