| 2532 | } |
| 2533 | |
| 2534 | func safariFrontmostURL() (string, error) { |
| 2535 | script := `tell application "System Events" |
| 2536 | set safariRunning to (name of processes) contains "Safari" |
| 2537 | end tell |
| 2538 | if not safariRunning then error "Safari is not running" |
| 2539 | tell application "Safari" |
| 2540 | if not (exists front document) then error "Safari has no front document" |
| 2541 | return URL of front document |
| 2542 | end tell` |
| 2543 | |
| 2544 | cmd := exec.Command("osascript", "-e", script) |
| 2545 | output, err := cmd.CombinedOutput() |
| 2546 | if err != nil { |
| 2547 | trimmed := strings.TrimSpace(string(output)) |
| 2548 | if trimmed != "" { |
| 2549 | return "", fmt.Errorf("osascript: %s", trimmed) |
| 2550 | } |
| 2551 | return "", fmt.Errorf("osascript failed: %w", err) |
| 2552 | } |
| 2553 | |
| 2554 | url := strings.TrimSpace(string(output)) |
| 2555 | if url == "" { |
| 2556 | return "", fmt.Errorf("front Safari tab URL is empty") |
| 2557 | } |
| 2558 | |
| 2559 | return url, nil |
| 2560 | } |
| 2561 | |
| 2562 | type commitPayload struct { |
| 2563 | message string |