-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaplock_mac.go
58 lines (50 loc) · 1.34 KB
/
caplock_mac.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
//go:build darwin
// +build darwin
package main
import (
"os"
"os/exec"
"os/user"
"path/filepath"
)
// ToggleCapsLock toggles the Caps Lock key on macOS using AppleScript
func ToggleCapsLock() error {
cmd := exec.Command("osascript", "-e", `tell application "System Events" to key code 57`)
err := cmd.Run()
if err != nil {
return err
}
return nil
}
// enableStartup creates a Launch Agent plist file to enable startup
func enableStartup() error {
// Get the absolute path of the current executable
exePath, err := os.Executable()
if err != nil {
return err
}
// Get the user's home directory
usr, err := user.Current()
if err != nil {
return err
}
// Define the path to the Launch Agent plist file
plistPath := filepath.Join(usr.HomeDir, "Library/LaunchAgents/com.gorandomcaps.plist")
// Define the content of the plist file
plistContent := `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.gorandomcaps</string>
<key>ProgramArguments</key>
<array>
<string>` + exePath + `</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>`
// Write the plist file with the desired content
return os.WriteFile(plistPath, []byte(plistContent), 0644)
}