Page 2 of 2

Re: Carbon API list for Mac

Posted: Tue Aug 25, 2015 11:12 pm
by Keya
Many thanks wilbert! All those examples work beautifully on my Mac64 :)
Im getting two programming books on Mac in a week or two both go into Cocoa so hopefully I won't have to ask too many stupid questions for long heehe
Reading the documentation as you suggested for NSRunningApplication Class i see that we need to check "executableURL" to get the actual path of the process, it's of type "NSURL" not string like "localizedName", how can we read that? My searched showed a couple other NSURL purebasic uses but i couldnt get it working with this, usually memory error saying incompatible type :(
ps. (no pun!) even when I run it with sudo it only lists about a quarter of the ones that "ps -x" lists?

Re: Carbon API list for Mac

Posted: Wed Aug 26, 2015 5:18 am
by wilbert
URL can be done like this

Code: Select all

RunningApps = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "runningApplications")
RunningAppsCount = CocoaMessage(0, RunningApps, "count")

OpenWindow(0, 100, 100, 600, 400, "Running applications", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListIconGadget(0, 10, 10, 580, 380, "PID", 60, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection | #PB_ListIcon_CheckBoxes)
AddGadgetColumn(0, 1, "Name", 280)
AddGadgetColumn(0, 2, "Executable", 1500)

Define i = 0
While i < RunningAppsCount
  
  RunningApp = CocoaMessage(0, RunningApps, "objectAtIndex:", i)
  PID.i = CocoaMessage(0, RunningApp, "processIdentifier")
  AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
  ExecutableURL.i = CocoaMessage(0, CocoaMessage(0, RunningApp, "executableURL"), "absoluteString")
  URL.s = PeekS(CocoaMessage(0, ExecutableURL, "UTF8String"), -1, #PB_UTF8)
  AddGadgetItem(0, i, Str(PID) + #LF$ + AppName + #LF$ + URL)
  SetGadgetItemImage(0, i, CocoaMessage(0, RunningApp, "icon"))
  If CocoaMessage(0, RunningApp, "isActive")
    SetGadgetItemState(0, i, #PB_ListIcon_Checked)
  EndIf
  
  i + 1
Wend

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
As for the difference with "ps -x", you are right.
I suppose those other items are not applications or owned by 'root' but am not sure.
You can show more processes another way but unfortunately the name of the process is truncated to the first 16 characters

Code: Select all

ImportC ""
  proc_listallpids(*buffer, buffersize)
  proc_name(pid, *buffer, buffersize)
  proc_pidpath(pid, *buffer, buffersize)
EndImport

cnt = proc_listallpids(#Null, 0)
Dim PID.l(cnt - 1)
cnt = proc_listallpids(@PID(), cnt << 2) 
ReDim PID(cnt - 1)

Dim buffer.a(1024)

For i = 0 To cnt - 1
  Debug PeekS(@Buffer(), proc_name(PID(i), @buffer(), 1024), #PB_Ascii)    
  Debug PeekS(@Buffer(), proc_pidpath(PID(i), @buffer(), 1024), #PB_Ascii) 
  Debug ""
Next

Re: Carbon API list for Mac

Posted: Wed Aug 26, 2015 8:53 am
by Keya
Thankyou! :) hmm well NSURL turned out a little different than i thought, lol :)

btw yes my searching has found at least two ways!
proc_listpids as youve just shown,
and also GetBSDProcessList function:

Code: Select all

int main(int argc, char *argv[]) {
  kinfo_proc *procs;
  size_t count;
  int err = GetBSDProcessList(&procs, &count);
  if (err) return err;
  FILE *f = fopen("./bsdlist", "w");
  for (size_t i=0; i!=count; ++i) {
    fprintf(f, "%d\n", procs[i].kp_proc.p_pid);
  }
  fclose(f);
  free(procs);
}
looking into kinfo_proc...

Re: Carbon API list for Mac

Posted: Wed Aug 26, 2015 8:56 am
by wilbert
I couldn't get GetBSDProcessList to work; didn't know the import location. :?

Edit:
I moved my code example to the Cocoa thread since it has little to do with Carbon
http://www.purebasic.fr/english/viewtop ... 87#p469687