-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (155 loc) · 3.63 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
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"syscall"
"time"
conf "github.com/ad/findmyticker/config"
"github.com/ad/lru"
"github.com/getlantern/systray"
"github.com/kardianos/osext"
)
var (
cancel context.CancelFunc
version = `0.1.6`
menuInfo *systray.MenuItem
menuError *systray.MenuItem
config *conf.Config
lruCache *lru.Cache[string, [2]float64]
)
func main() {
cfg, errInitConfig := conf.InitConfig()
if errInitConfig != nil {
log.Fatalf("InitConfig: %s", errInitConfig.Error())
return
}
config = cfg
if config.FindMyApp.OpenOnStartup {
errRunFindMyApp := runFindMyApp()
if errRunFindMyApp != nil {
fmt.Printf("runFindMyApp: %s\n", errRunFindMyApp.Error())
}
}
if config.FindMyApp.BringToFrontOnIdle {
go bringFindMyToFrontOnIdle()
}
lruCache = lru.New[string, [2]float64]()
_, cancel = context.WithCancel(context.Background())
defer func() {
cancel()
systray.Quit()
}()
go func() {
run()
for range time.Tick(time.Duration(config.Update.Period) * time.Second) {
run()
}
}()
systray.Run(onReady, onExit)
}
func onReady() {
systray.SetTitle("⚐")
mTitle := systray.AddMenuItem(fmt.Sprintf("FindMy ticker v%s", version), "App title")
mTitle.Disable()
menuInfo = systray.AddMenuItem(fmt.Sprintf("started at: %s", time.Now().Format("15:04:05")), "")
menuInfo.Disable()
menuError = systray.AddMenuItem(fmt.Sprintf("error: %s", "none"), "")
menuError.Disable()
menuError.Hide()
mConfig := systray.AddMenuItem("Config", "Open config")
mRestart := systray.AddMenuItem("Restart", "Restart app")
mQuit := systray.AddMenuItem("Quit", "Quit app")
for {
select {
case <-mRestart.ClickedCh:
fmt.Println("Requesting restart")
_ = Restart()
return
case <-mConfig.ClickedCh:
fmt.Println("Opening config editor")
_ = OpenConfig()
case <-mQuit.ClickedCh:
fmt.Println("Requesting quit")
systray.Quit()
return
}
}
}
func onExit() {
// clean up here
}
// Restart app
func Restart() error {
file, error := osext.Executable()
if error != nil {
return error
}
error = syscall.Exec(file, os.Args, os.Environ())
if error != nil {
return error
}
systray.Quit()
return nil
}
// Open config
func OpenConfig() error {
_ = conf.OpenConfigEditor()
return nil
}
func runFindMyApp() error {
cmd := exec.Command(`open`, "--hide", "--background", "/System/Applications/FindMy.app")
stderr, err := cmd.StderrPipe()
// log.SetOutput(os.Stderr)
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
slurp, _ := io.ReadAll(stderr)
fmt.Printf("%s\n", slurp)
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
func bringFindMyToFrontOnIdle() error {
appleScript := fmt.Sprintf(`repeat
set num to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'")
set o to (offset of "." in num)
if ((o > 0) and (0.0 as text is "0,0")) then set num to (text 1 thru (o - 1) of num & "," & text (o + 1) thru -1 of num)
set idleTime to num as integer
if idleTime is greater than or equal to (%d) then
log "is idle"
tell application "System Events"
tell process "Find My"
set frontmost to true
end tell
delay 16
tell process "Finder"
set frontmost to true
end tell
delay 16
end tell
end if
delay 1
end repeat`, config.FindMyApp.BringToFronDelay)
cmd := exec.Command("/usr/bin/osascript", "-e", appleScript)
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
slurp, _ := io.ReadAll(stderr)
fmt.Printf("%s\n", slurp)
if err := cmd.Wait(); err != nil {
return err
}
return nil
}