使用终端或 bash 脚本创建和写入 .plist
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27379507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating and writing into .plist with Terminal or bash script
提问by Mubasher
I need to create a .plist file during post install and the only option I can use is a bash script. I have to create a foo.plist into /Library/launchAgents
with a bash script and I've used the following command:
我需要在安装后创建一个 .plist 文件,我可以使用的唯一选项是 bash 脚本。我必须/Library/launchAgents
使用 bash 脚本创建一个 foo.plist并使用以下命令:
cd /Library/launchAgents
touch foo.plist
now I need to write contents into this .plist file for example like this:
现在我需要将内容写入这个 .plist 文件,例如:
".plist contents" >> foo.plist
Is there a command that can do this in the terminal?
是否有可以在终端中执行此操作的命令?
采纳答案by Mark Setchell
Your question doesn't specify very well what you have got, or why you need to do it in bash
, but if you must do it that way, you can do it like this:
你的问题没有很好地说明你有什么,或者为什么你需要在 中这样做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
So, you save this in a file called Buildplist
and then do this to make it executable
因此,您将其保存在一个名为的文件中Buildplist
,然后执行此操作以使其可执行
chmod +x Buildplist
and then you run it by typing this:
然后你输入这个来运行它:
./Buildplist
You can make it write the plist file directly into /Library/launchAgents
by changing the second line to something like this:
您可以/Library/launchAgents
通过将第二行更改为以下内容,使其直接写入 plist 文件:
cat > /Library/launchAgents/yourApp/yourApp.plist <<EOF
You can make it accept parameters too. So if you want to pass the Author as the first parameter, you can do it like this:
你也可以让它接受参数。所以如果你想传递 Author 作为第一个参数,你可以这样做:
#!/bin/bash
VERSION=2.12
AUTHOR=""
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
and then run
然后运行
./Buildplist "Freddy Frog"
to pass "Freddy Frog" as the author.
通过“Freddy Frog”作为作者。
If you want to avoid overwriting any plist file that already exists, you can do it like this:
如果你想避免覆盖任何已经存在的 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
I put the name of the plist file in a variable to simplify maintenance, and avoid typing it twice.
我将 plist 文件的名称放在一个变量中以简化维护,并避免输入两次。
回答by DawnSong
PlistBuddyis what you want.
PlistBuddy就是你想要的。
/usr/libexec/PlistBuddy Info.plist
File Doesn't Exist, Will Create: Info.plist
Then add an entry to the file like this,
然后像这样在文件中添加一个条目,
/usr/libexec/PlistBuddy -c 'add CFBundleIdenfier string com.tencent.myapp' Info.plist
By the way, man plist
, man plutil
may be helpful for you.
顺便说一句,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