Translate Xcode

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Translate Xcode

Post by spacebuddy »

I am trying to translate some code from Xcode to PB



NSDictionary *activeApp = [[NSWorkspace sharedWorkspace] activeApplication];
NSLog(@"Active application is: %@", (NSString *)[activeApp objectForKey:@"NSApplicationName"]);

Define activeApp.i=CocoaMessage(0,0,"NSWorkspace sharedWorkspace")

Not sure how to do the second line of code, any help :D
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Translate Xcode

Post by wilbert »

activeApplication is deprecated starting with OS X 10.7 .
For OS X 10.6 and above, you can do it like this

Code: Select all

RunningApps = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "runningApplications")
RunningAppsCount = CocoaMessage(0, RunningApps, "count")
i = 0
While i < RunningAppsCount
  RunningApp = CocoaMessage(0, RunningApps, "objectAtIndex:", i)
  AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
  If CocoaMessage(0, RunningApp, "isActive")
    AppName + "  ** Active **"
  EndIf
  Debug AppName
  i + 1
Wend
Windows (x64)
Raspberry Pi OS (Arm64)
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: Translate Xcode

Post by spacebuddy »

Thanks Wilbert, what I was trying to do with that code is get the foreground application
that is currently running :D
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Translate Xcode

Post by wilbert »

NSWorkspace also has a notification NSWorkspaceDidActivateApplicationNotification
It's a bit more complicated to use but allows you to get notified when the active application changes.

Code: Select all

EnableExplicit

; *** import the required functions ***

ImportC ""
  sel_registerName(str.p-ascii)
  class_addMethod(class, selector, imp, types.p-ascii)
EndImport

; *** required variables ***

Global notificationCenter = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "notificationCenter")
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")

Define selector


; *** Active application changed callback ***

ProcedureC ApplicationActivated(obj, sel, notification)
  Protected RunningApp = CocoaMessage(0, CocoaMessage(0, notification, "userInfo"), "objectForKey:$", @"NSWorkspaceApplicationKey")
  Protected AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
  Protected IconSize.NSSize, Icon = CocoaMessage(0, RunningApp, "icon")
  IconSize\width = 64 : IconSize\height = 64 : CocoaMessage(0, Icon, "setSize:@", @IconSize)
  SetGadgetState(0, Icon)
  SetGadgetText(1, AppName)
EndProcedure


; *** main code ***

If OpenWindow(0, 0, 0, 320, 84, "Active application demo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StickyWindow(0, 1)
  
  ImageGadget(0, 10, 10, 64, 64, 0)
  StringGadget(1, 84, 20, 226, 20, "", #PB_String_ReadOnly)
  
  selector = sel_registerName("ApplicationActivated:")
  class_addMethod(delegateClass, selector, @ApplicationActivated(), "v@:@")
  CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf

CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
PB 5.40+

Code: Select all

EnableExplicit

; *** required variables ***

Global notificationCenter = CocoaMessage(0, CocoaMessage(0, 0, "NSWorkspace sharedWorkspace"), "notificationCenter")
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")

Define selector


; *** Active application changed callback ***

ProcedureC ApplicationActivated(obj, sel, notification)
  Protected RunningApp = CocoaMessage(0, CocoaMessage(0, notification, "userInfo"), "objectForKey:$", @"NSWorkspaceApplicationKey")
  Protected AppName.s = PeekS(CocoaMessage(0, CocoaMessage(0, RunningApp, "localizedName"), "UTF8String"), -1, #PB_UTF8)
  Protected IconSize.NSSize, Icon = CocoaMessage(0, RunningApp, "icon")
  IconSize\width = 64 : IconSize\height = 64 : CocoaMessage(0, Icon, "setSize:@", @IconSize)
  SetGadgetState(0, Icon)
  SetGadgetText(1, AppName)
EndProcedure


; *** main code ***

If OpenWindow(0, 0, 0, 320, 84, "Active application demo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StickyWindow(0, 1)
  
  ImageGadget(0, 10, 10, 64, 64, 0)
  StringGadget(1, 84, 20, 226, 20, "", #PB_String_ReadOnly)
  
  selector = sel_registerName_("ApplicationActivated:")
  class_addMethod_(delegateClass, selector, @ApplicationActivated(), "v@:@")
  CocoaMessage(0, notificationCenter, "addObserver:", appDelegate, "selector:", selector, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf

CocoaMessage(0, notificationCenter, "removeObserver:", appDelegate, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply