-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkflowHandler.sh
More file actions
144 lines (122 loc) · 4.12 KB
/
workflowHandler.sh
File metadata and controls
144 lines (122 loc) · 4.12 KB
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
#!/bin/bash
VPREFS="${HOME}/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/"
NVPREFS="${HOME}/Library/Application Support/Alfred 2/Workflow Data/"
RESULTS=()
################################################################################
# Adds a result to the result array
#
# $1 uid
# $2 arg
# $3 title
# $4 subtitle
# $5 icon
# $6 valid
# $7 autocomplete
###############################################################################
addResult() {
RESULT="<item uid='$(xmlEncode "$1")' arg='$(xmlEncode "$2")' valid='$6' autocomplete='$7'><title>$(xmlEncode "$3")</title><subtitle>$(xmlEncode "$4")</subtitle><icon>$(xmlEncode "$5")</icon></item>"
RESULTS+=("$RESULT")
}
###############################################################################
# Prints the feedback xml to stdout
###############################################################################
getXMLResults() {
echo "<?xml version='1.0'?><items>"
# if [ "${#string[@]}" = "0" ]; then
# echo "<item uid='oftask' arg='-' valid='no'><title>No results found</title><subtitle>Please try another search term</subtitle><icon></icon></item>"
# fi
for R in ${RESULTS[*]}; do
echo "$R" | tr "\n" " "
done
echo "</items>"
}
###############################################################################
# Escapes XML special characters with their entities
###############################################################################
xmlEncode() {
echo "$1" | sed -e 's/&/\&/g' -e 's/>/\>/g' -e 's/</\</g' -e "s/'/\'/g" -e 's/"/\"/g'
}
###############################################################################
# Read the bundleid from the workflow's info.plist
###############################################################################
getBundleId() {
/usr/libexec/PlistBuddy -c "Print :bundleid" "info.plist"
}
###############################################################################
# Get the workflow data dir
###############################################################################
getDataDir() {
local BUNDLEID=$(getBundleId)
echo "${NVPREFS}${BUNDLEID}"
}
###############################################################################
# Get the workflow cache dri
###############################################################################
getCacheDir() {
local BUNDLEID=$(getBundleId)
echo "${VPREFS}${BUNDLEID}"
}
###############################################################################
# Save key=value to the workflow properties
#
# $1 key
# $2 value
# $3 non-volatile 0/1
# $4 filename (optional, filename will be "settings" if not specified)
###############################################################################
setPref() {
local BUNDLEID=$(getBundleId)
if [ "$3" = "0" ]; then
local PREFDIR="${VPREFS}${BUNDLEID}"
else
local PREFDIR="${NVPREFS}${BUNDLEID}"
fi
if [ ! -d "$PREFDIR" ]; then
mkdir -p "$PREFDIR"
fi
if [ -z "$4" ]; then
local PREFFILE="${PREFDIR}/settings"
else
local PREFFILE="${PREFDIR}/$4"
fi
if [ ! -f "$PREFFILE" ]; then
touch "$PREFFILE"
fi
local KEY_EXISTS=$(grep -c "$1=" "$PREFFILE")
if [ "$KEY_EXISTS" = "1" ]; then
local TMP=$(grep -ve "^$1" "$PREFFILE")
echo "$TMP" > "$PREFFILE"
fi
echo "$1=$2" >> "$PREFFILE"
}
###############################################################################
# Read a value for a given key from the workflow preferences
#
# $1 key
# $2 non-volatile 0/1
# $3 filename (optional, filename will be "settings" if not specified)
###############################################################################
getPref() {
local BUNDLEID=$(getBundleId)
if [ "$2" = "0" ]; then
local PREFDIR="${VPREFS}${BUNDLEID}"
else
local PREFDIR="${NVPREFS}${BUNDLEID}"
fi
if [ ! -d "$PREFDIR" ]; then
return
fi
if [ -z "$3" ]; then
local PREFFILE="${PREFDIR}/settings"
else
local PREFFILE="${PREFDIR}/$3"
fi
if [ ! -f "$PREFFILE" ]; then
return
fi
local VALUE=$(sed "/^\#/d" "$PREFFILE" | grep "$1" | tail -n 1 | cut -d "=" -f2-)
echo "$VALUE"
}
getLang() {
defaults read .GlobalPreferences AppleLanguages | tr -d [:space:] | cut -c2-3
}