-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.go
74 lines (65 loc) · 1.88 KB
/
script.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
package main
//#cgo CFLAGS: -x objective-c
//#cgo LDFLAGS: -framework Foundation
//#include "script.h"
import "C"
import "errors"
const (
// эплскрипт кринж, оно запускает прилу при отправке/получении данных, и это никак нельзя изменить
source = `
if application "Music" is running then
tell application "Music"
set a to ""
set n to ""
set p to 0
set d to 0
try
set tr to current track
set a to artist of tr
set n to name of tr
set p to player position
set d to duration of tr
end try
return {player state as string, a, n, p, d}
end tell
end if
`
StatePlaying = "playing"
StatePaused = "paused"
StateStopped = "stopped"
)
var script *C.NSAppleScript
func init() {
if script = C.compileScript(C.CString(source)); script == nil {
panic("failed to compile script")
}
}
type result struct {
state string
artist string
name string
position float64
duration float64
}
func executeScript() (*result, error) {
descriptor := C.executeScript(script)
if descriptor == nil {
return nil, errors.New("failed to execute script")
}
return &result{
state: getStringFromDescriptor(descriptor, 1),
artist: getStringFromDescriptor(descriptor, 2),
name: getStringFromDescriptor(descriptor, 3),
position: getFloatFromDescriptor(descriptor, 4),
duration: getFloatFromDescriptor(descriptor, 5),
}, nil
}
func getStringFromDescriptor(descriptor *C.NSAppleEventDescriptor, index int) string {
return C.GoString(C.getStringFromDescriptor(descriptor, C.int(index)))
}
func getIntFromDescriptor(descriptor *C.NSAppleEventDescriptor, index int) int {
return int(C.getIntFromDescriptor(descriptor, C.int(index)))
}
func getFloatFromDescriptor(descriptor *C.NSAppleEventDescriptor, index int) float64 {
return float64(C.getFloatFromDescriptor(descriptor, C.int(index)))
}