(ctx *snap.Context)
| 1882 | } |
| 1883 | |
| 1884 | func runListWindowsOfApp(ctx *snap.Context) error { |
| 1885 | if ctx.NArgs() != 0 { |
| 1886 | fmt.Fprintf(ctx.Stderr(), "Usage: %s listWindowsOfApp\n", commandName) |
| 1887 | return fmt.Errorf("expected 0 arguments, got %d", ctx.NArgs()) |
| 1888 | } |
| 1889 | |
| 1890 | apps, err := listRunningApplications() |
| 1891 | if err != nil { |
| 1892 | return reportError(ctx, fmt.Errorf("list running applications: %w", err)) |
| 1893 | } |
| 1894 | if len(apps) == 0 { |
| 1895 | fmt.Fprintln(ctx.Stdout(), "No foreground applications found.") |
| 1896 | return nil |
| 1897 | } |
| 1898 | |
| 1899 | idx, err := fuzzyfinder.Find( |
| 1900 | apps, |
| 1901 | func(i int) string { |
| 1902 | return apps[i] |
| 1903 | }, |
| 1904 | fuzzyfinder.WithPromptString("listWindowsOfApp> "), |
| 1905 | ) |
| 1906 | if err != nil { |
| 1907 | if errors.Is(err, fuzzyfinder.ErrAbort) { |
| 1908 | return nil |
| 1909 | } |
| 1910 | return reportError(ctx, fmt.Errorf("select application: %w", err)) |
| 1911 | } |
| 1912 | |
| 1913 | selectedApp := apps[idx] |
| 1914 | windows, err := listApplicationWindows(selectedApp) |
| 1915 | if err != nil { |
| 1916 | return reportError(ctx, fmt.Errorf("list windows for %s: %w", selectedApp, err)) |
| 1917 | } |
| 1918 | |
| 1919 | if len(windows) == 0 { |
| 1920 | fmt.Fprintf(ctx.Stdout(), "%s has no visible windows.\n", selectedApp) |
| 1921 | return nil |
| 1922 | } |
| 1923 | |
| 1924 | fmt.Fprintf(ctx.Stdout(), "Windows for %s:\n", selectedApp) |
| 1925 | for _, title := range windows { |
| 1926 | fmt.Fprintf(ctx.Stdout(), " %s\n", title) |
| 1927 | } |
| 1928 | return nil |
| 1929 | } |
| 1930 | |
| 1931 | func listRunningApplications() ([]string, error) { |
| 1932 | script := `tell application "System Events" |
no test coverage detected