MCPcopy Create free account
hub / github.com/nikivdev/go / listRunningApplications

Function listRunningApplications

cli/flow/main.go:1931–1980  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1929}
1930
1931func listRunningApplications() ([]string, error) {
1932 script := `tell application "System Events"
1933 set appNames to {}
1934 repeat with proc in application processes
1935 if background only of proc is false then
1936 set procName to name of proc
1937 if procName is not missing value and procName is not "" then
1938 copy procName to end of appNames
1939 end if
1940 end if
1941 end repeat
1942end tell
1943
1944set AppleScript's text item delimiters to "\n"
1945return appNames as text`
1946
1947 cmd := exec.Command("osascript", "-")
1948 cmd.Stdin = strings.NewReader(script)
1949 output, err := cmd.CombinedOutput()
1950 if err != nil {
1951 trimmed := strings.TrimSpace(string(output))
1952 if trimmed != "" {
1953 return nil, fmt.Errorf("osascript list apps: %s", trimmed)
1954 }
1955 return nil, fmt.Errorf("osascript list apps: %w", err)
1956 }
1957
1958 trimmed := strings.TrimSpace(string(output))
1959 if trimmed == "" {
1960 return nil, nil
1961 }
1962
1963 rawNames := strings.Split(trimmed, "\n")
1964 seen := make(map[string]struct{}, len(rawNames))
1965 var apps []string
1966 for _, name := range rawNames {
1967 candidate := strings.TrimSpace(name)
1968 if candidate == "" {
1969 continue
1970 }
1971 if _, ok := seen[candidate]; ok {
1972 continue
1973 }
1974 seen[candidate] = struct{}{}
1975 apps = append(apps, candidate)
1976 }
1977
1978 sort.Strings(apps)
1979 return apps, nil
1980}
1981
1982func listApplicationWindows(appName string) ([]string, error) {
1983 script := `on run argv

Callers 1

runListWindowsOfAppFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected