Skip to content

Commit

Permalink
refactor to put AS inline
Browse files Browse the repository at this point in the history
  • Loading branch information
holysoles committed Apr 21, 2024
1 parent 2c8c15e commit e30f755
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 393 deletions.
405 changes: 248 additions & 157 deletions applescript.go

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions applescriptHelpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
30 changes: 0 additions & 30 deletions lib/approveJoin.applescript

This file was deleted.

4 changes: 0 additions & 4 deletions lib/checkFacetime.applescript

This file was deleted.

33 changes: 0 additions & 33 deletions lib/createLink.applescript

This file was deleted.

48 changes: 0 additions & 48 deletions lib/deleteLink.applescript

This file was deleted.

35 changes: 0 additions & 35 deletions lib/getLinks.applescript

This file was deleted.

29 changes: 0 additions & 29 deletions lib/joinFirstLink.applescript

This file was deleted.

47 changes: 0 additions & 47 deletions lib/joinFirstLinkApproveJoin.applescript

This file was deleted.

8 changes: 0 additions & 8 deletions lib/leaveCall.applescript

This file was deleted.

2 changes: 0 additions & 2 deletions lib/openFacetime.applescript

This file was deleted.

0 comments on commit e30f755

Please sign in to comment.