Skip to content

Commit 9106e9d

Browse files
committed
Breaks applescript func into smaller funcs
- runAppleScript() returns a track instead of long string
1 parent 66fb10e commit 9106e9d

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed

applescript.go

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,74 @@ import (
77
"github.com/everdev/mack"
88
)
99

10-
const isRunningCmd = `
11-
if it is running then
12-
return true
13-
else
14-
return false
15-
end if
16-
`
17-
const artistAndAlbumCmd = `
18-
set ctrack to ""
19-
set ctrack to ctrack & (current track's artist) & "-"
20-
set ctrack to ctrack & (current track's album)
21-
`
22-
23-
// Runs AppleScript "tell" and returns string shaped Artist-Album
24-
func runAppleScript() (string, error) {
10+
// Runs AppleScript "tell" and returns a Track{artist, album}
11+
func runAppleScript() (Track, error) {
12+
err := isRunning()
13+
if err != nil {
14+
return Track{}, err
15+
}
16+
17+
artist, err := artist()
18+
if err != nil {
19+
return Track{}, err
20+
}
21+
22+
album, err := album()
23+
if err != nil {
24+
return Track{}, err
25+
}
26+
27+
track := newTrack(artist, album)
28+
return track, nil
29+
}
30+
31+
func isRunning() error {
32+
const isRunningCmd = `
33+
if it is running then
34+
return true
35+
else
36+
return false
37+
end if
38+
`
39+
2540
isRunning, err := mack.Tell("Spotify", isRunningCmd)
2641
if err != nil {
27-
return "", errors.New("Could not exectute AppleScript properly")
42+
return errors.New("Could not exectute AppleScript properly")
2843
}
44+
2945
if isRunning == "false" {
30-
return "", errors.New("Spotify is not running, please open the app")
46+
return errors.New("Spotify is not running, please open the app")
47+
}
48+
49+
return nil
50+
}
51+
52+
func artist() (string, error) {
53+
const artistCmd = `
54+
set ctrack to ""
55+
set ctrack to ctrack & (current track's artist)
56+
`
57+
58+
artist, err := mack.Tell("Spotify", artistCmd)
59+
if err != nil {
60+
e := fmt.Sprintf("Could not exectute AppleScript properly: %s", err)
61+
return "", errors.New(e)
3162
}
3263

33-
output, err := mack.Tell("Spotify", artistAndAlbumCmd)
64+
return artist, nil
65+
}
66+
67+
func album() (string, error) {
68+
const albumCmd = `
69+
set ctrack to ""
70+
set ctrack to ctrack & (current track's album)
71+
`
72+
73+
album, err := mack.Tell("Spotify", albumCmd)
3474
if err != nil {
3575
e := fmt.Sprintf("Could not exectute AppleScript properly: %s", err)
3676
return "", errors.New(e)
3777
}
3878

39-
return output, nil
79+
return album, nil
4080
}

main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66
)
77

88
func main() {
9-
output, err := runAppleScript()
9+
track, err := runAppleScript()
1010
if err != nil {
1111
fmt.Println(err)
1212
os.Exit(1)
1313
}
1414

15-
track := newTrack(string(output))
1615
info, err := getLastfmInfo(track)
1716
if err != nil {
1817
fmt.Println(err)

track.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ type Track struct {
88
Album string
99
}
1010

11-
func newTrack(appleScriptInfo string) Track {
12-
info := strings.Split(string(appleScriptInfo), "-")
13-
artist := info[0]
14-
album := info[1]
11+
func newTrack(artist, album string) Track {
1512
albumTitle := cleanAlbumTitle(album)
1613
return Track{artist, albumTitle}
1714
}

0 commit comments

Comments
 (0)