Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to find running applications under Mac OS
Message
De
04/09/2019 08:00:23
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
03/09/2019 12:41:11
Information générale
Forum:
Go
Catégorie:
Code, syntax et commandes
Divers
Thread ID:
01670494
Message ID:
01670567
Vues:
34
>>>>Thanks but I already said I can get all the processes. If you know which keyword and\or flag I should use to get applications from ps then that is fine.
>>>
>>>One of the links I provided for you explains which flags to look for. Using the ps command allows for those flags to be conveyed on each process line, which can then be parsed so only the subset you seek is there.
>>>
>>>I do not have a Mac machine to code or test it for you or I would.
>>>
>>>>PS: I can get them using an applescript, I would prefer doing programmatically instead if I can rather than parsing output of a shell command.
>>
>>Which link is that. I think I have already read what is in those links + the ps man page included in Mac, still cannot figure how.
>>
>>PS: Currently I can get what I want with an apple script, instead of ps. I would like to have a way doing all these programmatically (I can use "ps" without actually using shell ps BTW via low level libraries) but anyway, looks like I will live with calling and parsing applescript output.
>
>If I'm reading it correctly, it was the superuser link. It showed the [RDS][s]+ flags would indicate a foreground process:
>
>
R+
>Rs+
>D+
>Ds+
>S+
>Ss+
>
>The man pages may show additional detail. You might be able to coordinate the flags seen in a snapshot with what you see in the Apple tool and apply some filters to figure out what you're looking for.

Unfortunately almost all processes have Ss for that :) That is what I am trying to do, seeking for a difference. The closest I could come up with is XSTAT flag. I cannot find any documentation about this flag anywhere on the internet, but what I figured out is Applications have value 13 in this flag. It is not only the applications on desktop but also those running as a TSR in toolbar (like systray in windows) but not so big deal, getting only a few more (better than getting 400+ processes).

Here is the code I came up with (Go):
package main

import (
	"bytes"
	"fmt"
	"github.com/ianlopshire/go-fixedwidth"
	"log"
	"os/exec"
	"strings"
)

func main() {
	args := "-o pid,lstart,xstat,comm"
	cmd := exec.Command("ps", "-Aww", args)
	var b bytes.Buffer
	cmd.Stdout = &b
	cmd.Stderr = &b
	err := cmd.Run()
	if err != nil {
		log.Fatalf("cmd.Run() failed with %s\n", err)
	}

	lines := strings.Split(string(b.Bytes()), "\n")
	data := strings.Join(lines[1:], "\n")

	var processes []struct {                         // A structure with tags
		Pid     int    `fixed:"1,5"`
		Lstart  string `fixed:"6,35"`
		Xstat   int    `fixed:"36,40"`
		Comm    string `fixed:"41,300"`
	}
	err = fixedwidth.Unmarshal([]byte(data),&processes)
	if err != nil {
		fmt.Println(err)
	}
    i := 0
	for _,p := range processes  {
		if p.Xstat == 13 {
			i++
			fmt.Println(i, p.Pid, p.Comm)
		}
	}
}
And its sample output as of now:
1 376 /Applications/Parallels Toolbox.app/Contents/MacOS/Parallels Toolbox
2 402 /Library/Input Methods/ParallelsIM.app/Contents/MacOS/ParallelsIM
3 411 /Applications/DrCleaner.app/Contents/MacOS/DrCleaner
4 498 /Applications/Postgres.app/Contents/MacOS/PostgresMenuHelper.app/Contents/MacOS/PostgresMenuHelper
5 2699 /Applications/JetBrains Toolbox.app/Contents/MacOS/jetbrains-toolbox
6 27679 /Applications/Safari.app/Contents/MacOS/Safari
7 27802 /Applications/Parallels Desktop.app/Contents/MacOS/prl_client_app
8 28505 /Applications/Slack.app/Contents/MacOS/Slack
9 28844 /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
10 28876 /Applications/Sublime Text.app/Contents/MacOS/Sublime Text
11 28924 /Applications/Utilities/Script Editor.app/Contents/MacOS/Script Editor
12 28928 /Applications/Utilities/Activity Monitor.app/Contents/MacOS/Activity Monitor

This is what I get from the apple script I was talking about:
{"Finder", "Safari", "prl_client_app", "WinAppHelper", "WinAppHelper", "WinAppHelper", "WinAppHelper", "Slack", "Terminal", "Sublime Text", "Script Editor", "Activity Monitor"}
More or less, I have what I need (winAppHelpers are applications running in virtual PC - "prl-client-app" and Activity monitor have a better list even showing details about those with their names such as "Microsoft Visual Foxpro 9.0"). I can live with this if I can't find a better way.
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform