-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
319 lines (269 loc) · 7.37 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) 2020 Dean Jackson <deanishe@deanishe.net>
// MIT Licence applies http://opensource.org/licenses/MIT
// Created on 2020-08-01
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strings"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/update"
"github.com/deanishe/awgo/util"
"github.com/pkg/errors"
"howett.net/plist"
)
const (
// workflow's GitHub repo (for updates)
repo = "deanishe/alfred-services"
helpURL = "https://github.com/deanishe/alfred-services/issues"
// property list containing list of services
servicesList = "${HOME}/Library/Caches/com.apple.nsservicescache.plist"
// properly list containing enabled information
pbsList = "${HOME}/Library/Preferences/pbs.plist"
)
var (
iconUpdateAvailable = &aw.Icon{Value: "icons/update-available.png"}
iconError = &aw.Icon{Value: "icons/error.png"}
iconWarning = &aw.Icon{Value: "icons/warning.png"}
)
var (
wf *aw.Workflow
fs = flag.NewFlagSet("alfred-services", flag.ExitOnError)
flagHelp = fs.Bool("h", false, "show this message and exit")
flagUpdate = fs.Bool("update", false, "check for newer version of the workflow")
)
// Service is a macOS service.
type Service struct {
Name string // name of service
Types []string // supported pasteboard types
AppName string // name of app service belongs to (optional)
AppPath string // path of application that defines service
Disabled bool // whether service is disabled
}
// Title returns a more readable name.
func (s Service) Title() string {
// Safari action has this weird name. Replace it with something better.
if s.Name == "Search With %WebSearchProvider@" {
return "Search Web"
}
return s.Name
}
// UID returns a unique ID for Service.
func (s Service) UID() string { return s.AppPath + " - " + s.Name }
// Icon returns a workflow icon for the service.
func (s Service) Icon() *aw.Icon { return &aw.Icon{Value: s.AppPath, Type: aw.IconTypeFileIcon} }
// Supports returns true if this service supports any of the given types.
func (s Service) Supports(types []string) bool {
for _, t1 := range s.Types {
for _, t2 := range types {
if t1 == t2 {
return true
}
}
}
return false
}
// ByName sorts services by name.
type ByName []Service
// Implement sort.Interface
func (s ByName) Len() int { return len(s) }
func (s ByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
var rxServiceName = regexp.MustCompile(`^(\S+) - (.+) - (\S+)$`)
// read services from property list.
func loadServices() ([]Service, error) {
var (
disabled = map[string]bool{}
services []Service
path string
data []byte
err error
)
// extract enabled/disabled info from pbs.plist
path = os.ExpandEnv(pbsList)
if data, err = ioutil.ReadFile(path); err != nil {
return nil, errors.Wrap(err, "read pbs.plist")
}
v1 := struct {
Services map[string]struct {
Modes struct {
ContextMenu bool `plist:"ContextMenu"`
ServicesMenu bool `plist:"ServicesMenu"`
TouchBar bool `plist:"TouchBar"`
} `plist:"presentation_modes"`
} `plist:"NSServicesStatus"`
}{}
if _, err = plist.Unmarshal(data, &v1); err != nil {
return nil, errors.Wrap(err, "unmarshal pbs list")
}
for key, modes := range v1.Services {
m := rxServiceName.FindStringSubmatch(key)
if m == nil {
log.Printf("[WARNING] could not parse pbs service name: %s", key)
continue
}
if !modes.Modes.ContextMenu && !modes.Modes.ServicesMenu && !modes.Modes.TouchBar {
disabled[m[2]] = true
}
}
// extract main services metadata
path = os.ExpandEnv(servicesList)
if data, err = ioutil.ReadFile(path); err != nil {
return nil, errors.Wrap(err, "read services list")
}
v2 := struct {
Apps map[string]struct {
BundleID string `plist:"bundle_id"`
Name string `plist:"name"`
Services []struct {
Menu struct {
Name string `plist:"default"`
} `plist:"NSMenuItem"`
Types []string `plist:"NSSendTypes"`
} `plist:"service_dicts"`
} `plist:"ServicesCache"`
}{}
if _, err = plist.Unmarshal(data, &v2); err != nil {
return nil, errors.Wrap(err, "unmarshal services list")
}
for path, app := range v2.Apps {
for _, v := range app.Services {
services = append(services, Service{
Name: v.Menu.Name,
Types: v.Types,
AppName: app.Name,
AppPath: path,
Disabled: disabled[v.Menu.Name],
})
}
}
sort.Sort(ByName(services))
return services, nil
}
// get clipboard data types via environment variable or script
func pasteboardTypes() []string {
if s := os.Getenv("PBOARD_TYPES"); s != "" {
return strings.Split(s, "|")
}
var types []string
data, err := util.Run("./PasteboardTypes.js")
checkErr(err)
checkErr(json.Unmarshal(data, &types))
wf.Var("PBOARD_TYPES", strings.Join(types, "|"))
return types
}
// get contents of clipboard via environment variable or pbpaste
func clipboardContents() string {
if s := os.Getenv("CLIPBOARD"); s != "" {
return s
}
data, err := util.RunCmd(exec.Command("/usr/bin/pbpaste", "-Prefer", "txt"))
checkErr(err)
s := string(data)
wf.Var("CLIPBOARD", s)
return s
}
func init() {
aw.IconError = iconError
aw.IconWarning = iconWarning
wf = aw.New(update.GitHub(repo), aw.HelpURL(helpURL))
fs.SetOutput(os.Stderr)
}
func usage() {
fmt.Fprint(fs.Output(), `alfred-services (-files|-services) [input...]
Alfred workflow to run macOS services
`)
fs.PrintDefaults()
}
func run() {
checkErr(fs.Parse(wf.Args()))
if *flagHelp {
usage()
return
}
if *flagUpdate {
wf.Configure(aw.TextErrors(true))
log.Printf("checking for update ...")
checkErr(wf.CheckForUpdate())
return
}
var (
query = fs.Arg(0)
clipboard string
types []string
allServices []Service
services []Service
err error
)
log.Printf("query=%q", query)
// check updates
if query == "" && wf.UpdateAvailable() {
wf.Configure(aw.SuppressUIDs(true))
wf.NewItem("Update Available").
Subtitle("⇥ or ↩ to update workflow").
Autocomplete("workflow:update").
Valid(false).
Icon(iconUpdateAvailable)
}
if wf.UpdateCheckDue() {
if !wf.IsRunning("update") {
checkErr(wf.RunInBackground("update", exec.Command(os.Args[0], "-update")))
}
}
// show list of services
types = pasteboardTypes()
if len(types) == 0 {
wf.Warn("No Data on Pasteboard", "")
return
}
for _, s := range types {
log.Printf("[pasteboard] type=%q", s)
}
allServices, err = loadServices()
checkErr(err)
log.Printf("%d total service(s)", len(allServices))
for _, s := range allServices {
if !s.Disabled && s.Supports(types) {
services = append(services, s)
}
}
log.Printf("%d enabled service(s) support current pasteboard types", len(services))
if len(services) == 0 {
wf.Warn("No Matching Services", "No services support the current data")
return
}
clipboard = clipboardContents()
for _, s := range services {
it := wf.NewItem(s.Title()).
Subtitle(s.AppName).
Arg(s.Name).
UID(s.UID()).
Valid(true).
Largetype(clipboard).
Icon(s.Icon())
it.NewModifier(aw.ModCmd).
Subtitle("Reveal "+s.AppPath).
Arg(s.AppPath).
Var("reveal", "true")
}
if query != "" {
wf.Filter(query)
}
wf.WarnEmpty("No Matching Services", "Try a different query?")
wf.SendFeedback()
}
func main() {
wf.Run(run)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}