Skip to content

Commit 19a8a5e

Browse files
committed
Add action field to Item
1 parent 1aabafe commit 19a8a5e

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

alfred.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ func (a *Alfred) SetTheme(name string) error {
8888
// with an empty type.
8989
func (a *Alfred) Action(value ...string) error { return a.ActionAsType("", value...) }
9090

91-
// Types understood by Alfred's `action` API call and item field.
92-
const (
93-
TypeFile = "file"
94-
TypeURL = "url"
95-
TypeText = "text"
96-
)
97-
9891
// ActionAsType tells Alfred to show Universal Actions for value(s). Type typ
9992
// may be one of "file", "url" or "text", or an empty string to tell Alfred
10093
// to guess the type.

feedback.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ const (
2929
ModFn string = "fn" // Alternate action for fn↩
3030
)
3131

32+
// Types understood by Alfred's `action` API call and item field. Added in Alfred 4.5.
33+
const (
34+
TypeFile = "file" // values are paths
35+
TypeURL = "url" // values are URLs
36+
TypeText = "text" // values are just text
37+
)
38+
3239
// Item is a single Alfred Script Filter result.
3340
// Together with Feedback & Modifier, Item generates Script Filter feedback
3441
// for Alfred.
@@ -48,6 +55,7 @@ type Item struct {
4855
ql *string
4956
vars map[string]string
5057
mods map[string]*Modifier
58+
actions map[string][]string
5159
icon *Icon
5260
noUID bool // Suppress UID in JSON
5361
}
@@ -142,6 +150,27 @@ func (it *Item) Icon(icon *Icon) *Item {
142150
return it
143151
}
144152

153+
// Action sets the value(s) to be passed to Alfred's Universal Actions if
154+
// the user actions this item. Alfred will auto-detect the type of the value(s).
155+
//
156+
// Added in Alfred 4.5.
157+
func (it *Item) Action(value ...string) *Item { return it.ActionForType("", value...) }
158+
159+
// ActionForType sets the value(s) to be passed to Alfred's Universal Actions if
160+
// the user actions this item. Type may be one of "file", "url" or "text".
161+
//
162+
// Added in Alfred 4.5.
163+
func (it *Item) ActionForType(typ string, value ...string) *Item {
164+
if typ == "" {
165+
typ = "auto"
166+
}
167+
if it.actions == nil {
168+
it.actions = map[string][]string{}
169+
}
170+
it.actions[typ] = value
171+
return it
172+
}
173+
145174
// Var sets an Alfred variable for subsequent workflow elements.
146175
func (it *Item) Var(k, v string) *Item {
147176
if it.vars == nil {
@@ -244,6 +273,7 @@ func (it *Item) MarshalJSON() ([]byte, error) {
244273
Quicklook string `json:"quicklookurl,omitempty"`
245274
Variables map[string]string `json:"variables,omitempty"`
246275
Mods map[string]*Modifier `json:"mods,omitempty"`
276+
Actions map[string][]string `json:"action,omitempty"`
247277
}{
248278
Title: it.title,
249279
Subtitle: it.subtitle,
@@ -257,6 +287,7 @@ func (it *Item) MarshalJSON() ([]byte, error) {
257287
Quicklook: ql,
258288
Variables: it.vars,
259289
Mods: it.mods,
290+
Actions: it.actions,
260291
}
261292
// serialise single arg as string
262293
if len(it.arg) == 1 {

feedback_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestItem_Icon(t *testing.T) {
2323

2424
func p(s string) *string { return &s }
2525

26-
// Feedback is empty.
26+
// TestFeedback_IsEmpty verifies empty feedback.
2727
func TestFeedback_IsEmpty(t *testing.T) {
2828
t.Parallel()
2929

@@ -115,6 +115,9 @@ func TestItem_MarshalJSON(t *testing.T) {
115115
// With quicklook
116116
{in: &Item{title: "title", ql: p("http://www.example.com")},
117117
x: `{"title":"title","valid":false,"quicklookurl":"http://www.example.com"}`},
118+
// With action
119+
{in: &Item{title: "title", actions: map[string][]string{"auto": {"one", "two"}}},
120+
x: `{"title":"title","valid":false,"action":{"auto":["one","two"]}}`},
118121
}
119122

120123
for i, td := range tests {

0 commit comments

Comments
 (0)