Editing plist file using shell script
Asked Answered
F

8

24

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)

Faceless answered 5/10, 2014 at 10:40 Comment(0)
W
45

Also:

plutil -replace BundleIsRelocatable -bool false plistfilename.plist
Wilds answered 5/10, 2014 at 11:32 Comment(0)
O
10

For String use

plutil -replace NameOfProperty -string "yourNewValue" plistFileName.plist
Opening answered 22/5, 2017 at 10:17 Comment(0)
Z
6

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
Zetland answered 13/6, 2017 at 10:23 Comment(0)
P
5

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.

Palaearctic answered 7/3, 2016 at 5:59 Comment(0)
M
3

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

Moorefield answered 5/10, 2014 at 11:14 Comment(0)
G
2

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.

Granulose answered 10/10, 2014 at 14:13 Comment(0)
F
0

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

Fourfold answered 30/1, 2015 at 10:35 Comment(0)
D
0

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.

Dissociate answered 5/4, 2023 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.