This repository has been archived by the owner on Mar 4, 2018. It is now read-only.
/
numbers
executable file
·99 lines (86 loc) · 2.92 KB
/
numbers
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
#!/usr/bin/env bash
export LC_CTYPE='en_US.UTF-8' # UTF-8 case awareness
scriptdir="${BASH_SOURCE[0]%/*}"
source "$scriptdir"/lib/strings.sh
# Parse arguments
desc=''; icon=''
case "$1" in
--found)
code=$':%n\t%u\t:%nn\t:%c\t%hp\t%wp\t%mp\t%Mp\t%op\t%pp'
desc='first found number' ;;
--home)
code=$':%n\t%u\t:%nn\t:%c\t%hp' ;;
--work)
code=$':%n\t%u\t:%nn\t:%c\t%wp' ;;
--mobile)
code=$':%n\t%u\t:%nn\t:%c\t%mp' ;;
--main)
code=$':%n\t%u\t:%nn\t:%c\t%Mp' ;;
--other)
code=$':%n\t%u\t:%nn\t:%c\t%op' ;;
--pager)
code=$':%n\t%u\t:%nn\t:%c\t%pp'
desc='pager number' ;;
--fax)
code=$':%n\t%u\t:%nn\t:%c\t%fp'
desc='fax number' ;;
*)
exit 1 ;;
esac
desc="${desc:-${1#--} phone number}"
icon="${icon:-public.vcard}"
if ! is_blank $2; then
# Normalize Unicode representation of characters with diacritics
is_7bit "${*:2}" && query=(${*:2}) || query=($(utf8_normalize "${*:2}"))
# Start XML output
source "$scriptdir"/lib/feedback.sh
feedback --start
# Get contact data matching query
prev_title=''; prev_number=''; matches=0
while read -r || [[ -n "$REPLY" ]]; do
# Catch contacts’ stdout message on 0 matches
[[ $REPLY == 'error: no one found' ]] && break
# Parse contact output line into record items
IFS=$'\t' record=($REPLY)
name="${record[0]#:}"
uid="${record[1]}"
nick="${record[2]#:}"
[[ ${record[3]#:} != "$name" ]] && company="${record[3]#:}" || company=''
number="${record[4]}"
title=''
# Only keep records with a phone number
if [[ -n "$number" ]]; then
# Word incremental search; first match defines display format
for q in "${query[@]}"; do
# Query words starting with an upper case letter are case sensitive
is_lower "$q" && shopt -s nocasematch || shopt -u nocasematch
if [[ $name =~ "$q" ]]; then
[[ -z "$title" ]] && title="$name${company:+ ($company)}"
elif [[ $nick =~ "$q" ]]; then
[[ -z "$title" ]] && title="“${nick}”${name:+ $name}${company:+ ($company)}"
elif [[ $company =~ "$q" ]]; then
[[ -z "$title" ]] && title="$company${name:+ – $name}"
else # next record
continue 2
fi
done
# Skip duplicate records
[[ $title == "$prev_title" && $number == "$prev_number" ]] && continue
# Output XML feedback item
text="Call $desc: $number"
feedback "$title" --subtitle="$text" --uid="$uid" --action="$number" --valid --icon="$icon" --filetype
# Store control var values
prev_title="$title"; prev_number="$number"; let matches++
fi
done < <("$scriptdir"/contacts -SH -f "$code" ${query[0]} | sort)
# Create fallback item if no matching records are found
if (( $matches < 1 )); then
title='No matching contacts found'
text="Action this item to run Alfred default searches for “${query[*]}”."
arg="alfredsearch:${query[*]}"
icon='/Applications/Contacts.app'
feedback "$title" --subtitle="$text" --uid='noresults' --action="$arg" --icon="$icon" --fileicon
fi
# End XML output
feedback --end
fi