Skip to content

Commit

Permalink
bring FindMy to front on idle
Browse files Browse the repository at this point in the history
  • Loading branch information
ad committed Jan 23, 2024
1 parent 599aad8 commit 8cb6b89
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
Binary file modified build/FindMy ticker.app/Contents/MacOS/findmyticker
Binary file not shown.
18 changes: 10 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (

// Config ...
type Config struct {
Token string `json:"token"`
URL string `json:"url"`
Period int `json:"period"`
Ignore []string `json:"ignore"`
AllowItems bool `json:"allowItems"`
AllowDevices bool `json:"allowDevices"`
OpenFindMyOnStartup bool `json:"openFindMyOnStartup"`
Token string `json:"token"`
URL string `json:"url"`
Period int `json:"period"`
Ignore []string `json:"ignore"`
AllowItems bool `json:"allowItems"`
AllowDevices bool `json:"allowDevices"`
OpenFindMyOnStartup bool `json:"openFindMyOnStartup"`
BringFindMyToFrontOnIdle bool `json:"bringFindMyToFrontOnIdle"`
}

func InitConfig() (*Config, error) {
Expand Down Expand Up @@ -95,7 +96,8 @@ func OpenConfigEditor() error {
"ignore": [],
"allowItems": true,
"allowDevices": true,
"openFindMyOnStartup": true
"openFindMyOnStartup": true,
"bringFindMyToFrontOnIdle": true
}`

f, err := os.Create(path)
Expand Down
57 changes: 55 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var (
cancel context.CancelFunc

version = `0.1.2`
version = `0.1.3`

menuInfo *systray.MenuItem
menuError *systray.MenuItem
Expand All @@ -40,7 +40,15 @@ func main() {
config = cfg

if config.OpenFindMyOnStartup {
runFindMyApp()
errRunFindMyApp := runFindMyApp()
if errRunFindMyApp != nil {
fmt.Printf("runFindMyApp: %s\n", errRunFindMyApp.Error())
}

}

if config.BringFindMyToFrontOnIdle {
go bringFindMyToFrontOnIdle()
}

lruCache = lru.New[string, [2]float64]()
Expand Down Expand Up @@ -148,3 +156,48 @@ func runFindMyApp() error {

return nil
}

func bringFindMyToFrontOnIdle() error {
appleScript := `repeat
set num to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'")
set o to (offset of "." in num)
if ((o > 0) and (0.0 as text is "0,0")) then set num to (text 1 thru (o - 1) of num & "," & text (o + 1) thru -1 of num)
set idleTime to num as integer
if idleTime is greater than or equal to (1 * 10) then
log "is idle"
tell application "System Events"
tell process "Find My"
set frontmost to true
end tell
delay 16
tell process "Finder"
set frontmost to true
end tell
delay 16
end tell
end if
delay 1
end repeat`

cmd := exec.Command("/usr/bin/osascript", "-e", appleScript)
stderr, err := cmd.StderrPipe()

if err != nil {
return err
}

if err := cmd.Start(); err != nil {
return err
}

slurp, _ := io.ReadAll(stderr)
fmt.Printf("%s\n", slurp)

if err := cmd.Wait(); err != nil {
return err
}

return nil
}

0 comments on commit 8cb6b89

Please sign in to comment.