使用PlistBuddy(osx)在plist中查找条目


1

我试图在Mac计算机上找到当前用户是否启用了iCloud Documents。我找到了它所在的plist(MobileMeAccounts.plist),但是我可以使用plistbuddy语法的一些帮助来在树中定位。

这是我到目前为止的脚本的一部分:

#!/bin/bash

# Purpose: Grab iCloud Document Status

plistBud="/usr/libexec/PlistBuddy"

if [[ -e "/Users/*loggedInUser*/Library/Preferences/MobileMeAccounts.plist" ]]; then

iCloudStatus=`$plistBud -c "print :Accounts:Services:MOBILE_DOCUMENTS" /Users/$loggedInUser/Library/Preferences/MobileMeAccounts.plist`
else

iCloudDocuments="Not Enabled"

fi

echo "$iCloudStatus"

我特意寻找以下代码:

<key>Enabled</key>
<true/>

这是plist。如果向下滚动,您将看到启用它的“MOBILE_DOCUMENTS”:

<?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>Accounts</key>
<array>
    <dict>
        <key>AccountAlternateDSID</key>
        <string>99999999</string>
        <key>AccountDSID</key>
        <string>999999</string>
        <key>AccountDescription</key>
        <string>iCloud</string>
        <key>AccountID</key>
        <string>*****@gmail.com</string>
        <key>AccountUUID</key>
        <string>9999999</string>
        <key>DisplayName</key>
        <string>User Name</string>
        <key>LoggedIn</key>
        <true/>
        <key>Services</key>
        <array>
            <dict>
                <key>Name</key>
                <string>CLOUDDESKTOP</string>
                <key>ServiceID</key>
                <string>com.apple.Dataclass.CloudDesktop</string>
                <key>status</key>
                <string>active</string>
            </dict>
            <dict>
                <key>Name</key>
                <string>FAMILY</string>
                <key>ServiceID</key>
                <string>com.apple.Dataclass.Family</string>
                <key>showManageFamily</key>
                <true/>
            </dict>
            <dict>
                <key>Enabled</key>
                <true/>
                <key>Name</key>
                <string>MOBILE_DOCUMENTS</string>
                <key>ServiceID</key>
                <string>com.apple.Dataclass.Ubiquity</string>
                <key>apsEnv</key>
                <string>production</string>
                <key>authMechanism</key>
                <string>token</string>
                <key>url</key>
                <string>https://p48-ubiquity.icloud.com:443</string>
                <key>wsUrl</key>
                <string>https://p48-ubiquityws.icloud.com:443</string>
            </dict>

Answers:


0

你快到了。

诀窍是你必须在每个数组中指定你想要的对象。在XML路径中添加此索引,您就可以了。

要获取数组中的项列表:

/usr/libexec/PlistBuddy -c "print :Accounts:0:Services" ~/Library/Preferences/MobileMeAccounts.plist

在我的情况下MOBILE_DOCUMENTS是第一项,所以它是索引0(第二项是索引1,第三项索引2等)。

这对我有用:

/usr/libexec/PlistBuddy -c "print :Accounts:0:Services:0:Enabled" ~/Library/Preferences/MobileMeAccounts.plist

将给予“虚假”或“真实”。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.