user2653062
user2653062

Reputation: 717

Editing plist file using shell script

I have used pkgbuild to create a default Component Property List file. The file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-     1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>BundleHasStrictIdentifier</key>
        <true/>
        <key>BundleIsRelocatable</key>
        <true/>
        <key>BundleIsVersionChecked</key>
        <true/>
        <key>BundleOverwriteAction</key>
        <string>upgrade</string>
        <key>RootRelativeBundlePath</key>
        <string>MyApp.app</string>
    </dict>
</array>
</plist>

I want to modify this file by using shell script. I tried using defaults write but it didn't do anything.

What is the way to do it?(For example: I want to set BundleIsRelocatable to false)

Upvotes: 24

Views: 22869

Answers (9)

Mor4eza
Mor4eza

Reputation: 305

also you can edit plist file with its property names like this:

plutil -replace items.0.assets.0.url -string "yourtext" yourfile.plist

enter image description here

Upvotes: 0

gymtanlaundry
gymtanlaundry

Reputation: 1

cli for mac would be defaults read / write. example is we read the com.apple.dock.plist and change the dock orientation to the left.

1.) we read the keys and values needed or if you know this skip to 2.)

#let's you know and reads the available keys and values that can be changed.

defaults read com.apple.dock.plist 

2.) #Let's modify the dock

orientation is the key and left is the value

defaults write com.apple.dock orientation left

defaults help is very useful. Hope this helps This is the native way to do it in macOS without adding plistbuddy and you can just script it.

Upvotes: 0

MrBrownser
MrBrownser

Reputation: 171

A bit late, but for the record, you just need to specify the absolute path AND add the .plist extension to the filename. If you are running your script in same directory that the plist file, your case would be translated into:

defaults write $PWD/YourPlistFilename.plist BundleIsRelocatable -bool false

Upvotes: 6

kuzdu
kuzdu

Reputation: 7524

For String use

plutil -replace NameOfProperty -string "yourNewValue" plistFileName.plist

Upvotes: 10

AechoLiu
AechoLiu

Reputation: 18368

Using PlistBuddy, a simple tutorial HERE.

 /usr/libexec/PlistBuddy -c "Set :BundleIsRelocatable bool false" plistfilename.plist

It can run as ONE command line to update the key/value. I use it to update CFBundleVersion generally, which can be found in this post.

Upvotes: 5

psergiocf
psergiocf

Reputation: 1617

The last response of Phil-CB here should be usefull.

Upvotes: 0

Lri
Lri

Reputation: 27593

Using sed:

sed -i '' '/<key>BundleIsRelocatable</{n;s/true/false/;}' file.plist

If the plist is not XML, run plutil -convert xml1 file.plist first.

Upvotes: 2

clt60
clt60

Reputation: 63892

Also:

plutil -replace BundleIsRelocatable -bool false plistfilename.plist

Upvotes: 46

hola
hola

Reputation: 3500

Use PlistBuddy!

Very simple and straight forward. Example:

/usr/libexec/PlistBuddy ComponentPropertyList.plist
Command: Set :0:BundleIsRelocatable false
Command: save
Saving...
Command: exit

Thats it! Now BundleIsRelocatable is false :D

Upvotes: 3

Related Questions