使用Terminal或bash脚本创建和写入.plist

Mub*_*her 4 macos bash terminal info.plist

我需要在安装后期间创建一个.plist文件,我可以使用的唯一选项是bash脚本.我必须/Library/launchAgents用bash脚本创建一个foo.plist ,我使用了以下命令:

cd /Library/launchAgents
touch foo.plist
Run Code Online (Sandbox Code Playgroud)

现在我需要将内容写入此.plist文件,例如:

".plist contents" >> foo.plist
Run Code Online (Sandbox Code Playgroud)

是否有可以在终端中执行此操作的命令?

Daw*_*ong 14

PlistBuddy就是你想要的.

/usr/libexec/PlistBuddy Info.plist
File Doesn't Exist, Will Create: Info.plist
Run Code Online (Sandbox Code Playgroud)

然后像这样在文件中添加一个条目,

/usr/libexec/PlistBuddy -c 'add CFBundleIdenfier string com.tencent.myapp' Info.plist
Run Code Online (Sandbox Code Playgroud)

顺便说一句,man plist,man plutil可能对你有所帮助.

Command Format:
    Help - Prints this information
    Exit - Exits the program, changes are not saved to the file
    Save - Saves the current changes to the file
    Revert - Reloads the last saved version of the file
    Clear [<Type>] - Clears out all existing entries, and creates root of Type
    Print [<Entry>] - Prints value of Entry.  Otherwise, prints file
    Set <Entry> <Value> - Sets the value at Entry to Value
    Add <Entry> <Type> [<Value>] - Adds Entry to the plist, with value Value
    Copy <EntrySrc> <EntryDst> - Copies the EntrySrc property to EntryDst
    Delete <Entry> - Deletes Entry from the plist
    Merge <file.plist> [<Entry>] - Adds the contents of file.plist to Entry
    Import <Entry> <file> - Creates or sets Entry the contents of file

Entry Format:
    Entries consist of property key names delimited by colons.  Array items
    are specified by a zero-based integer index.  Examples:
        :CFBundleShortVersionString
        :CFBundleDocumentTypes:2:CFBundleTypeExtensions

Types:
    string
    array
    dict
    bool
    real
    integer
    date
    data

Examples:
    Set :CFBundleIdentifier com.apple.plistbuddy
        Sets the CFBundleIdentifier property to com.apple.plistbuddy
    Add :CFBundleGetInfoString string "App version 1.0.1"
        Adds the CFBundleGetInfoString property to the plist
    Add :CFBundleDocumentTypes: dict
        Adds a new item of type dict to the CFBundleDocumentTypes array
    Add :CFBundleDocumentTypes:0 dict
        Adds the new item to the beginning of the array
    Delete :CFBundleDocumentTypes:0 dict
        Deletes the FIRST item in the array
    Delete :CFBundleDocumentTypes
        Deletes the ENTIRE CFBundleDocumentTypes array
Run Code Online (Sandbox Code Playgroud)

  • 除了这个答案之外,我发现这篇文章非常有用:https://medium.com/@marksiu/what-is-plistbuddy-76cb4f0c262d (2认同)

Mar*_*ell 5

您的问题没有很好地说明您所拥有的内容,或者为什么需要在 中执行此操作bash,但如果您必须这样做,您可以这样做:

#!/bin/bash
VERSION=2.12
cat > foo.plist <<EOF
<?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">
<dict>
        <key>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
</dict>
</plist>
EOF
Run Code Online (Sandbox Code Playgroud)

因此,您将其保存在一个名为的文件中Buildplist,然后执行此操作以使其可执行

chmod +x Buildplist
Run Code Online (Sandbox Code Playgroud)

然后输入以下命令来运行它:

./Buildplist
Run Code Online (Sandbox Code Playgroud)

/Library/launchAgents您可以将第二行更改为如下所示,使其直接将 plist 文件写入:

cat > /Library/launchAgents/yourApp/yourApp.plist <<EOF
Run Code Online (Sandbox Code Playgroud)

您也可以让它接受参数。因此,如果您想将 Author 作为第一个参数传递,您可以这样做:

#!/bin/bash
VERSION=2.12
AUTHOR="$1"
cat > foo.plist <<EOF
<?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">
<dict>
        <key>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
        <author>$AUTHOR</author>
</dict>
</plist>
EOF
Run Code Online (Sandbox Code Playgroud)

然后运行

./Buildplist "Freddy Frog"
Run Code Online (Sandbox Code Playgroud)

通过“弗莱迪青蛙”作为作者。

如果你想避免覆盖任何已经存在的 plist 文件,你可以这样做:

#!/bin/bash
PLISTFILE="/Library/launchAgents/yourApp/yourApp.plist"

# If plist already exists, do not overwrite, just exit quietly
[ -f "$PLISTFILE" ] && exit

cat > "$PLISTFILE" <<EOF
...
...
EOF
Run Code Online (Sandbox Code Playgroud)

我将 plist 文件的名称放在一个变量中以简化维护,并避免输入两次。