我怎樣才能得到一個過程,它是UI(用戶界面)工藝或非UI信息?如何確定是否在用戶界面進程的進程?
隨着UI過程中,我的意思是,搜索,碼頭,系統UI服務器,或具有UI界面,它由Window服務器的任何其他Mac應用程序。
我要確定從該的ProcessID信息。
我使用Mac OS X的。
我怎樣才能得到一個過程,它是UI(用戶界面)工藝或非UI信息?如何確定是否在用戶界面進程的進程?
隨着UI過程中,我的意思是,搜索,碼頭,系統UI服務器,或具有UI界面,它由Window服務器的任何其他Mac應用程序。
我要確定從該的ProcessID信息。
我使用Mac OS X的。
沒有辦法純粹基於將PID 數什麼具體過程是確定。這樣做的原因:進程ID分配(有點)在啓動時從PID = 1順序和啓動可以是不同的系統不同。進程ID也將被如果,例如,搜索或Dock崩潰和必須重新啓動重新分配。
如果你可以運行你有,但具體pid
終端命令,這樣做:
ps -p <pid> -o ucomm=
你會得到這個過程中,你可以對證的人的列表的文件名你知道是UI的過程。例如,下面是某些ps
命令在我的系統輸出爲我的當前登錄會話:
> ps -p 110 -o ucomm=
Dock
> ps -p 112 -o ucomm=
Finder
而下面的命令會給你一個進程列表中的進程ID的順序,只有名稱:
> ps -ax -o pid=,ucomm=
1 launchd
10 kextd
11 DirectoryService
...
編輯:您可以做你問什麼,儘管它被卷積。 This答案提到:
從CGWindow.h函數CGWindowListCopyWindowInfo()將返回的字典的陣列,一個用於每個匹配設置了標準,其中包括那些在其他應用程序窗口。它只允許您通過給定窗口上方的窗口,給定窗口下方的窗口和「屏幕上」窗口進行過濾,但返回的字典包含擁有應用程序的進程ID,您可以使用該進程ID將窗口與應用程序進行匹配。
如果你能獲得所有CGWindow
S和它們各自的pid
S,那麼你就會知道pid
S的所有UI應用程序,而無需運行ps
可言。
拉胡爾已經實施了這種方法下面的代碼,他要求我將添加到我的回答:
CFArrayRef UiProcesses()
{
CFArrayRef orderedwindows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
CFIndex count = CFArrayGetCount (orderedwindows);
CFMutableArrayRef uiProcess = CFArrayCreateMutable (kCFAllocatorDefault , count, &kCFTypeArrayCallBacks);
for (CFIndex i = 0; i < count; i++)
{
if (orderedwindows)
{
CFDictionaryRef windowsdescription = (CFDictionaryRef)CFArrayGetValueAtIndex(orderedwindows, i);
CFNumberRef windowownerpid = (CFNumberRef)CFDictionaryGetValue (windowsdescription, CFSTR("kCGWindowOwnerPID"));
CFArrayAppendValue (uiProcess, windowownerpid);
}
}
return uiProcess;
}
嘗試以下。
#include <unistd.h>
if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO))
// Process associated with a terminal
else
// No terminal - probably UI process
在darvidsOn的線,下面是回答你的問題。
CFArrayRef UiProcesses()
{
CFArrayRef orderedwindows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
CFIndex count = CFArrayGetCount (orderedwindows);
CFMutableArrayRef uiProcess = CFArrayCreateMutable (kCFAllocatorDefault , count, &kCFTypeArrayCallBacks);
for (CFIndex i = 0; i < count; i++)
{
if (orderedwindows)
{
CFDictionaryRef windowsdescription = (CFDictionaryRef)CFArrayGetValueAtIndex(orderedwindows, i);
CFNumberRef windowownerpid = (CFNumberRef)CFDictionaryGetValue (windowsdescription, CFSTR("kCGWindowOwnerPID"));
CFArrayAppendValue (uiProcess, windowownerpid);
}
}
return uiProcess;
}
只是比較您對數組項目的processid以獲得所需的結果。
我已更新問題。謝謝 – MacGeek
我建議用編程語言替換你的'process'標籤,鑑於你的以mac爲中心的問題,Objective-C似乎是合適的。許多電力用戶只能掃描其專業問題。祝你好運。 – shellter
大概有一個目的是得到這個。如果你解釋了你實際想要達到的目標(爲什麼你需要知道這一點,以及爲什麼從過程ID?),你可能會得到更有用的答案。 :) –