This repository has been archived by the owner on Jan 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
tabs.go
297 lines (247 loc) · 5.93 KB
/
tabs.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
//
// Copyright (c) 2017 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-10-22
//
package main
import (
"fmt"
"log"
"net/url"
"strings"
aw "github.com/deanishe/awgo"
safari "github.com/deanishe/go-safari"
)
// Activate the specified window (and tab).
func doActivate() error {
wf.Configure(aw.TextErrors(true))
log.Printf("Activating %dx%d", winIdx, tabIdx)
return safari.ActivateTab(winIdx, tabIdx)
}
// doFilterTabActions is a Script Filter for tab actions.
func doFilterTabActions() error {
log.Printf("url=%s, scheme=%s", actionURL, actionURL.Scheme)
acts := []Actionable{}
for _, a := range TabActions() {
acts = append(acts, a)
}
// No URL actions for favorites:// and bookmarks:// etc.
if actionURL.Scheme == "http" || actionURL.Scheme == "https" {
for _, a := range URLActions() {
acts = append(acts, a)
}
}
return listActions(acts)
}
// doTabAction performs an action on a tab.
func doTabAction() error {
wf.Configure(aw.TextErrors(true))
var (
err error
tab *safari.Tab
URL *url.URL
)
log.Printf("window=%d, tab=%d, action=%s", winIdx, tabIdx, action)
wins, err := loadWindows()
if err != nil {
return err
}
for _, w := range wins {
if tab != nil {
break
}
if w.Index == winIdx {
for _, t := range w.Tabs {
if t.Index == tabIdx {
tab = t
URL, err = url.Parse(t.URL)
if err != nil {
return err
}
break
}
}
}
}
if tab == nil {
return fmt.Errorf("Tab not found : %02dx%02d", winIdx, tabIdx)
}
if actionType == "bookmarklet" {
bm := safari.BookmarkForUID(action)
if bm == nil {
return fmt.Errorf("Unknown bookmarklet: %s", action)
}
js, err := bm.ToJS()
if err != nil {
return err
}
return tab.RunJS(js)
}
if actionType == "tab" {
ta := TabAction(action)
if ta == nil {
return fmt.Errorf("Unknown action : %s", action)
}
return ta.Run(tab)
}
if actionType == "url" {
ua := URLAction(action)
if ua == nil {
return fmt.Errorf("Unknown action : %s", action)
}
return ua.Run(URL)
}
return fmt.Errorf("Tab not found : %02dx%02d", winIdx, tabIdx)
}
// doCurrentTab outputs workflow variables for the current tab.
func doCurrentTab() error {
tab, err := safari.ActiveTab()
if err != nil {
return fmt.Errorf("Couldn't get active tab: %s", err)
}
log.Printf("%v", tab)
av := aw.NewArgVars()
av.Var("ALSF_WINDOW", "1").
Var("ALSF_TAB", fmt.Sprintf("%d", tab.Index)).
Var("ALSF_URL", tab.URL)
s, err := av.String()
if err != nil {
return err
}
_, err = fmt.Println(s)
return err
}
// Filter tabs and output Alfred results.
func doFilterTabs() error {
showUpdateStatus()
log.Printf("query=%s", query)
wins, err := loadWindows()
if err != nil {
return err
}
for _, w := range wins {
for _, t := range w.Tabs {
it := wf.NewItem(t.Title).
Subtitle(t.URL).
Copytext(t.URL).
Valid(true).
Match(fmt.Sprintf("%s %s", t.Title, urlKeywords(t.URL)))
if t.Active {
it.Icon(IconActive)
} else {
it.Icon(IconTab)
}
it.Var("ALSF_WINDOW", fmt.Sprintf("%d", t.WindowIndex)).
Var("ALSF_TAB", fmt.Sprintf("%d", t.Index)).
Var("ALSF_URL", t.URL).
Var("action", "activate")
it.NewModifier("cmd").
Subtitle("Other actions…").
Var("action", "tab-actions")
it = customTabActions(it)
}
}
if query != "" {
res := wf.Filter(query)
log.Printf("%d result(s) for %q", len(res), query)
for i, r := range res {
log.Printf("#%02d %5.2f %q", i+1, r.Score, r.SortKey)
}
}
wf.WarnEmpty("No tabs found", "Try a different query?")
wf.SendFeedback()
return nil
}
// doClose closes the specified tab(s).
// TODO: Activate tab after closing to left or right?
func doClose() error {
if !left && !right { // Close current tab
log.Printf("Closing tab %d of window %d ...", tabIdx, winIdx)
return safari.CloseTab(winIdx, tabIdx)
}
if left && right { // Close all other tabs
log.Printf("Closing all tabs in window %d except %d ...", winIdx, tabIdx)
return safari.CloseTabsOther(winIdx, tabIdx)
}
if left {
log.Printf("Closing all tabs in window %d to left of %d ...", winIdx, tabIdx)
return safari.CloseTabsLeft(winIdx, tabIdx)
}
if right {
log.Printf("Closing all tabs in window %d to right of %d ...", winIdx, tabIdx)
return safari.CloseTabsRight(winIdx, tabIdx)
}
return nil
}
// --------------------------------------------------------------------
// Helpers
// customTabActions adds user-specified actions/bookmarklets to tab Item.
func customTabActions(it *aw.Item) *aw.Item {
var (
// UID:title map
bkms = map[string]string{}
// Name:Type map
actions = map[string]string{}
)
for _, bm := range safari.FilterBookmarks(func(bm *safari.Bookmark) bool { return bm.IsBookmarklet() }) {
bkms[bm.UID()] = bm.Title()
}
for _, a := range TabActions() {
actions[a.Title()] = "tab"
}
for _, a := range URLActions() {
actions[a.Title()] = "url"
}
actionDetails := func(s string) (title, typ, action string) {
if strings.HasPrefix(s, "bkm:") {
action = s[4:]
title = bkms[action]
typ = "bookmarklet"
return
}
title = s
typ = actions[title]
action = title
return
}
altActions := []struct {
action string
key aw.ModKey
}{
{tabActionCtrl, "ctrl"},
{tabActionShift, "shift"},
{tabActionOpt, "alt"},
{tabActionFn, "fn"},
}
for _, a := range altActions {
if a.action == "" { // unset
continue
}
title, typ, action := actionDetails(a.action)
if typ == "" {
log.Printf("Unknown action %s", a.action)
continue
}
it.NewModifier(a.key).
Subtitle(title).
Valid(true).
Var("action", "tab-action").
Var("ALSF_ACTION", action).
Var("ALSF_ACTION_TYPE", typ)
}
return it
}
// runBookmarklet executes a bookmarklet in the current tab.
func runBookmarklet(bm *safari.Bookmark) error {
tab, err := safari.ActiveTab()
if err != nil {
return err
}
js, err := bm.ToJS()
if err != nil {
return err
}
return tab.RunJS(js)
}