-
Notifications
You must be signed in to change notification settings - Fork 5
/
_plist_utils.sh
executable file
·147 lines (127 loc) · 2.93 KB
/
_plist_utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
PlistBuddy=/usr/libexec/PlistBuddy
function printValue() {
# $1: Key name
# $2: Plist file
$PlistBuddy -c "Print '$1'" "$2" 2> /dev/null
}
function printObject() {
# For dictionaries and arrays
# $1: Dictionary name
# $2: Source plist file
$PlistBuddy -x -c "Print '$1'" "$2"
}
function printArrayItems() {
# $1: Array name (key) in root dictionary plist
# $2: Plist file
for ((index=0; $? == 0; index++)); do
printValue "$1:$index" "$2" 2> /dev/null
done
return 0
}
function setValue() {
# $1: Key name
# $2: Value
# $3: Plist file
$PlistBuddy -c "Set '$1' '$2'" "$3" &> /dev/null
}
function add() {
# $1: Item name (key)
# $2: Value type
# $3: Value
# $4: Plist file
$PlistBuddy -c "Add '$1' $2" "$4" &> /dev/null
setValue "$1" "$3" "$4"
}
function addArray() {
# $1: Array name
# $2: Plist file
$PlistBuddy -c "Add $1 Array" "$2" &> /dev/null
}
function addDictionary() {
# $1: Dictionary name
# $2: Plist file
$PlistBuddy -c "Add $1 Dict" "$2" &> /dev/null
}
function addString() {
# $1: String name
# $2: String value
# $3: Plist file
add "$1" "String" "$2" "$3"
}
function addInteger() {
# $1: Integer name
# $2: Integer value
# $3: Plist file
add "$1" "Integer" "$2" "$3"
}
function delete() {
# $1: Key name
# $2: Plist file
$PlistBuddy -c "Delete '$1'" "$2"
}
function mergePlist() {
# $1: Plist to be merged
# $2: New key in address
# $3: Parent plist to be merged into
$PlistBuddy -c "Merge $1 '$2'" "$3"
}
function copy() {
# $1: Key to copy
# $2: Destination key
# $3: Plist file
$PlistBuddy -c "Copy '$1' '$2'" "$3"
}
function append() {
# $1: Array name (key) in root dictionary plist
# $2: Value type
# $3: Value
# $4: Plist file
add "$1:0" "$2" "$3" "$4"
}
function appendArrayWithString() {
# $1: Array name (key) in root dictionary plist
# $2: String value
# $3: Plist file
append "$1" "String" "$2" "$3"
}
function appendArrayWithInteger() {
# $1: Array name (key) in root dictionary plist
# $2: Integer value
# $3: Plist file
append "$1" "Integer" "$2" "$3"
}
function replaceVar() {
# $1: Variable key
# $2: Original plist file
# $3: New plist file
value=$(printValue "$1" "$3")
setValue "$1" "$value" "$2"
}
function replaceDict() {
# $1: Dictionary key
# $2: Original plist file
# $3: New plist file
printObject "$1" "$3" > /tmp/org.the-braveknight.node.plist
delete "$1" "$2"
addDictionary "$1" "$2"
mergePlist /tmp/org.the-braveknight.node.plist "$1" "$2"
}
function indexForItemInArray() {
# $1: Array key in root plist
# $2: Item (string, integer)
# $3: Plist file
for ((index=0; $? == 0; index++)); do
item=$(printValue "$1:$index" "$3" 2> /dev/null)
if [[ $? -ne 0 ]]; then return $?; fi
if [[ "$item" == "$2" ]]; then
echo $index
break
fi
done
}
function removeItem() {
# $1: Array key in root plist
# $2: Plist file
$PlistBuddy -c "Delete $1" "$2" 2> /dev/null
}