It is currently Fri Jul 28, 2017 8:00 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Carbon API list for Mac
PostPosted: Tue Aug 25, 2015 11:12 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
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?

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
 Post subject: Re: Carbon API list for Mac
PostPosted: Wed Aug 26, 2015 5:18 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2937
Location: Netherlands
URL can be done like this
Code:
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:
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

_________________
MacOS 10.12, PB 5.51 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Carbon API list for Mac
PostPosted: Wed Aug 26, 2015 8:53 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
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:
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...

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Last edited by Keya on Wed Aug 26, 2015 8:57 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Carbon API list for Mac
PostPosted: Wed Aug 26, 2015 8:56 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 2937
Location: Netherlands
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
viewtopic.php?p=469687#p469687

_________________
MacOS 10.12, PB 5.51 x64


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye