-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplescriptHelpers.go
118 lines (102 loc) · 2.33 KB
/
applescriptHelpers.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
package main
import (
"bytes"
"errors"
"fmt"
"io"
"os/exec"
"regexp"
"strconv"
"strings"
)
func openFacetime() error {
_, err := execAppleScript(openFacetimeScript)
return err
}
func getFacetimeStatus() (bool, error) {
s, err := execAppleScript(checkFacetimeScript)
if err != nil {
return false, err
}
b, err := strconv.ParseBool(s)
return b, err
}
func makeNewLink() (string, error) {
newLink, err := execAppleScript(newLinkScript)
return newLink, err
}
func getAllLinks() ([]string, error) {
allLinksRaw, err := execAppleScript(getActiveLinksScript)
var allLinks []string
if allLinksRaw != "" {
allLinks = strings.Split(allLinksRaw, ", ")
}
return allLinks, err
}
// Join the latest Facetime Call.
func joinCall() error {
_, err := execAppleScript(joinLatestLinkScript)
if err != nil {
return err
}
return nil
}
// Join the latest Facetime Call and approve all requested entrants. Combining these two actions allows us to leverage the sidebar being automatically in focus.
func joinAndAdmitCall() error {
_, err := execAppleScript(joinLatestLinkAndApproveScript)
if err != nil {
return err
}
return nil
}
func admitActiveCall() error {
_, err := execAppleScript(approveJoinScript)
if err != nil {
return err
}
return nil
}
func leaveCall() error {
_, err := execAppleScript(leaveCallScript)
if err != nil {
return err
}
return nil
}
func deleteCall(id string) (bool, error) {
deleteScript := fmt.Sprintf(deleteLinkScript, id)
deletedStr, err := execAppleScript(deleteScript)
if err != nil {
return false, err
}
deleted, _ := strconv.ParseBool(deletedStr)
return deleted, nil
}
func execAppleScript(s string) (string, error) {
args := "-"
cmd := exec.Command("osascript", args)
var stdin io.WriteCloser
var outBuff, errorBuff bytes.Buffer
var err error
stdin, err = cmd.StdinPipe()
if err != nil {
return "", err
}
cmd.Stdout = &outBuff
cmd.Stderr = &errorBuff
cmd.Start()
io.WriteString(stdin, s)
stdin.Close()
err = cmd.Wait()
// prefer returning errors from script execution
if errorBuff.Len() != 0 {
return "", errors.New(errorBuff.String())
}
if err != nil {
return "", err
}
//osaScript output has a tailing newline, making any later parse logic difficult
var re = regexp.MustCompile(`\n$`)
parsedOutput := re.ReplaceAllString(outBuff.String(), "")
return parsedOutput, nil
}