-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
139 lines (127 loc) · 3.32 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
package main
import (
"encoding/json"
"fmt"
"github.com/deanishe/awgo"
"io/ioutil"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
)
const CustomActionsVar = "CUSTOM_ACTIONS"
const CustomActionsFileVar = "CUSTOM_ACTIONS_FILE"
const CleanAmazonVar = "CLEAN_AMAZON"
var wf *aw.Workflow
func init() {
wf = aw.New()
}
type Action struct {
ActionTitle string `json:"action_title"`
ActionSubtitle string `json:"action_subtitle"`
Output string `json:"output"`
Encode bool `json:"encode"`
}
var defaultActions = []Action{
{
ActionTitle: "Copy as Markdown link",
Output: "[{title}]({url})",
},
{
ActionTitle: "Copy as Markdown list item",
Output: "- [{title}]({url})",
},
{
ActionTitle: "Add to OmniFocus",
ActionSubtitle: "",
Output: "omnifocus:///add?name={title}¬e={url}",
Encode: true,
},
{
ActionTitle: "Copy Title",
Output: "{title}",
},
{
ActionTitle: "Copy URL",
Output: "{url}",
},
{
ActionTitle: "Copy HTML link",
Output: "<a href=\"{url}\">{title}</a>",
},
}
func runAppleScript(script string) string {
out, err := exec.Command("osascript", "-l", "JavaScript", "-e", script).Output()
if err != nil {
wf.FatalError(err)
}
return strings.TrimSpace(string(out[:]))
}
func getAppId() string {
return runAppleScript("Application('System Events').applicationProcesses.where({frontmost:true}).bundleIdentifier()[0]")
}
func getTitle(appId string) string {
return runAppleScript(fmt.Sprintf("Application('%s').windows[0].name()", appId))
}
func cleanAmazonUrl(url string) string {
return regexp.MustCompile(`(^.*/(?:dp|gp/product)/([^/?]+)).*$`).ReplaceAllString(url, `$1`)
}
func run() {
var inputUrl string
args := wf.Args()
switch len(args) {
case 0:
inputUrl = ""
default:
inputUrl = args[0]
}
if strings.ToLower(os.Getenv(CleanAmazonVar)) == "true" {
inputUrl = cleanAmazonUrl(inputUrl)
}
var actions []Action
var jsonErr error
if customActionsFile := os.Getenv(CustomActionsFileVar); customActionsFile != "" {
customActionsFilePath := fmt.Sprintf("%s/%s", wf.DataDir(), customActionsFile)
content, readErr := ioutil.ReadFile(customActionsFilePath)
if len(content) == 0 {
wf.Fatalf("No actions in %s!", customActionsFile)
}
if readErr != nil {
wf.Fatalf("Error reading %s!", customActionsFile)
}
jsonErr = json.Unmarshal(content, &actions)
if jsonErr != nil {
wf.Fatalf("%s is malformed!", customActionsFile)
}
} else if customActions := os.Getenv(CustomActionsVar); customActions != "" {
jsonErr = json.Unmarshal([]byte(customActions), &actions)
if jsonErr != nil {
wf.Fatalf("%s is malformed!", CustomActionsVar)
}
} else {
actions = defaultActions
}
appId := getAppId()
title := getTitle(appId)
for _, action := range actions {
var formattedTitle string
if action.Encode {
formattedTitle = url.PathEscape(title)
} else {
formattedTitle = title
}
replacer := strings.NewReplacer("{title}", formattedTitle, "{url}", inputUrl)
formattedOutput := replacer.Replace(action.Output)
// Use the formatted string as subtitle if it's not specified in the json
subtitle := action.ActionSubtitle
if subtitle == "" {
subtitle = formattedOutput
}
wf.NewItem(action.ActionTitle).Subtitle(subtitle).Arg(formattedOutput).Valid(true)
}
wf.SendFeedback()
}
func main() {
wf.Run(run)
}