Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace line in .plist file to modify <string> - is it possible?

Basically I want to know if it's possible to create a script to edit an entry in a .plist file. However I don't want to replace a specific string, but the entire line so a new string can be added as the property will keep changing. An example:

<string>Something</string>
<key>SomethingElse</key>
<string>Numbers</string>
<key>Entry</key>

And having the third line changed to something like the following, regardless of what that line currently says.

<string>randomstuffhere</string>

I've been looking for a couple of hours and I think it's probably possible using sed but after looking at examples I can't seem to decipher it to construct it myself. Any and all help is appreciated. Oh, and I'd like to avoid using perl if possible.

EDIT: When using the following, the changes are made in the Terminal window but it doesn't seem to save it to the file. Using -i is apparently the way to fix this but it throws an error for me, so I'm not sure what I'm doing wrong.

sed -n '/SomethingElse/{p;n;s/>.*</>randomstuffhere</;};p' my.plist
like image 247
whitfin Avatar asked Sep 22 '12 00:09

whitfin


People also ask

How do I edit info plist?

All you have to do is click on the blue project icon -on top of the left- and go to the “Build Settings” tab, search “Info. plist” and choose “Info. plist File” section. Then change the information of the section from your folder's name.

How do I edit info plist on Mac?

plist file. If you need to edit these items, Control-click (or right-click) Info. plist in the sidebar and select Edit Manually. This allows you to add or edit items in raw XML format.

How do you comment in Info plist file?

Syntax of plist is pretty much like XML, so you can use XML style comments. By the way, you can select a portion of plist text and press cmd-/ (like you do for making a single-line comment in regular . m, . h files) and XCode will comment selected block for you automatically.


1 Answers

It's probably easiest to use defaults to edit it:

defaults write /abs/path/to/plist SomethingElse "randomstuffhere"

Note that you must supply the absolute path to the plist file, and leave the ".plist" off of its filename.

Also (as pointed out in the comments) this tends to convert the plist file to the binary plist format variant; programs that use appropriate .plist reading frameworks will read this fine, but it'll no longer be human-readable. If you want it to be in the XML format variant, you can convert it with plutil -convert xml1 /path/to/plist.plist.

Alternately, you can use PlistBuddy instead of defaults:

/usr/libexec/PlistBuddy -c "Set :SomethingElse 'randomstuffhere'" /path/to/plist.plist
like image 173
Gordon Davisson Avatar answered Oct 29 '22 15:10

Gordon Davisson