简体   繁体   中英

How can I use PlistBuddy to add settings without overriding existing ones?

I am trying to write a script that adds settings to the Settings app during build time without overwriting the existing ones, if existing. Here is a snippet of my script:

PLISTBUDDY="/usr/libexec/PlistBuddy"
SETTINGSBUNDLEPATH="$CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist"

{
$PLISTBUDDY -c "Add :PreferenceSpecifiers:0:Type string 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:0:Title string 'Version Information'" "$SETTINGSBUNDLEPATH"
} || {
$PLISTBUDDY -c "Set :PreferenceSpecifiers:0:Type 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:0:Title 'Version Information'" "$SETTINGSBUNDLEPATH"
}

{
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Type string 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Title string 'Version:'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:Key string 'appVersion'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Add :PreferenceSpecifiers:1:DefaultValue string '$APPVERSION'" "$SETTINGSBUNDLEPATH"
} || {
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Type 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Title 'Version:'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:Key 'appVersion'" "$SETTINGSBUNDLEPATH"
$PLISTBUDDY -c "Set :PreferenceSpecifiers:1:DefaultValue '$APPVERSION'" "$SETTINGSBUNDLEPATH"
}

I don't want to overwrite any existing settings though. How do I add these settings to the end of PreferenceSpecifiers?

I have read the documentation , and I have tried using Merge to no success.

$PLISTBUDDY -c "Merge ${PROJECT_DIR}/Settings1.bundle/Root.plist" "$SETTINGSBUNDLEPATH"

Edit: added 'try-catch'es to the code, this fixes the issue when running the script twice.

This first counts all dictionaries PreferenceSpecifiers, as in these answers . Then it will loop over the dictionaries and check if the group is already present, so when you build your app a second time, the script will not add the rows again. If the group is not found, it will add the rows just after the last index. One thing I'm not certain of is why count is not a number and how to properly fix it (the current code works).

PLISTBUDDY="/usr/libexec/PlistBuddy"
SETTINGSBUNDLEPATH="$CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist"

count=`${PLISTBUDDY} -c "Print PreferenceSpecifiers:" ${SETTINGSBUNDLEPATH} | grep "Dict"|wc -l`
let count++
let count--

FOUND=false
for index in `seq 0 $count`
do
    val=`${PLISTBUDDY} -c "Print PreferenceSpecifiers:${index}:Key" ${SETTINGSBUNDLEPATH}`
    if [ "$val" == "versionInfo" ]; then
        FOUND=true
        break
    fi
done

if [ "$FOUND" == false ]; then
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Type string 'PSGroupSpecifier'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Title string 'Version Information'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Key string 'versionInfo'" "$SETTINGSBUNDLEPATH"

    let count++
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Type string 'PSTitleValueSpecifier'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Title string 'Version:'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:Key string 'appVersion'" "$SETTINGSBUNDLEPATH"
    $PLISTBUDDY -c "Add :PreferenceSpecifiers:$count:DefaultValue string '$APPVERSION'" "$SETTINGSBUNDLEPATH"
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM