-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmacros.go
124 lines (102 loc) · 2.7 KB
/
macros.go
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
package main
import (
"encoding/xml"
"errors"
"os"
"os/exec"
"strings"
)
type KmItem struct {
Keys []string `xml:"key"`
Values []string `xml:",any"`
}
type KmCategory struct {
Keys []string `xml:"key"`
Values []string `xml:"string"`
Items []KmItem `xml:"array>dict"`
}
type KmCategories struct {
Categories []KmCategory `xml:"array>dict"`
}
type KmMacro struct {
UID string
Name string
Category string
Hotkey string
}
func getKmMacros() (map[string]KmMacro, error) {
// Allow to change the command for fetching macros, so the function could be unit-tested
getAllMacrosCommand := os.Getenv("GET_ALL_KM_MACROS_COMMAND")
if getAllMacrosCommand == "" {
getAllMacrosCommand = "osascript ./get_all_km_macros.scpt"
}
categoriesWithAllMacros, err := getKmCategories(getAllMacrosCommand)
if err != nil {
return nil, err
}
getHotkeyMacrosCommand := os.Getenv("GET_HOTKEY_KM_MACROS_COMMAND")
if getHotkeyMacrosCommand == "" {
getHotkeyMacrosCommand = "osascript ./get_hotkey_km_macros.scpt"
}
categoriesWithHotKeyMacros, err := getKmCategories(getHotkeyMacrosCommand)
if err != nil {
return nil, err
}
macros := make(map[string]KmMacro)
var uid string
for _, category := range categoriesWithAllMacros.Categories {
for _, item := range category.Items {
uid = item.getValueByKey("uid")
macros[uid] = KmMacro{
UID: uid,
Name: item.getValueByKey("name"),
Category: category.getValueByKey("name"),
Hotkey: "",
}
}
}
for _, category := range categoriesWithHotKeyMacros.Categories {
for _, item := range category.Items {
uid = item.getValueByKey("uid")
macro, isExists := macros[uid]
if isExists == true {
macro.Hotkey = item.getValueByKey("key")
// TODO Use pointer instead?
macros[uid] = macro
}
}
}
return macros, nil
}
func getKmCategories(command string) (KmCategories, error) {
out, err := exec.Command("sh", "-c", command).Output()
var categories KmCategories
if err != nil {
return categories, errors.New("Unable to get macros from Keyboard Maestro")
}
if !strings.Contains(string(out), "<?xml") {
return categories, errors.New(string(out))
}
err = xml.Unmarshal(out, &categories)
if err != nil {
return categories, errors.New("Unable to get macros from Keyboard Maestro")
}
return categories, nil
}
func (item KmItem) getValueByKey(requestedKey string) string {
for i, key := range item.Keys {
if key == requestedKey {
return item.Values[i]
}
}
return ""
}
// TODO Find out how to use the same func for both KmItem and KmCategory
func (item KmCategory) getValueByKey(requestedKey string) string {
for i, key := range item.Keys {
if key == requestedKey {
return item.Values[i]
}
}
return ""
}