Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: NSWorkSpaceDidWake on OS/X, how do you detect it


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Oct 10, 2017 2:10 AM Last Post By: Erick Engelke
Erick Engelke

Posts: 101
Registered: 12/5/02
NSWorkSpaceDidWake on OS/X, how do you detect it  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 9, 2017 8:36 PM
I need to detect the workstation waking up after a power event. It looks like I need to observe NSWorkspaceDidWake, but I can't find the constant for its message, or how to detect it.

Has someone figured this out already?

--
Erick Engelke
Enterprise Delphi Databases Book
Using Elevate Web Builder Book
HTML5 Builder Book
Read my Delphi blog
http://www.erickengelke.com
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: NSWorkSpaceDidWake on OS/X, how do you detect it  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 10, 2017 1:55 AM   in response to: Erick Engelke in response to: Erick Engelke
N/A Programmer wrote:

Has someone figured this out already?

I threw this together just now:

unit WorkspaceNotifications.Mac;
 
interface
 
implementation
 
uses
  System.TypInfo,
  Macapi.AppKit, Macapi.Foundation, Macapi.ObjectiveC, Macapi.ObjCRuntime, Macapi.Helpers;
 
const
  libAppKit: string = '/System/Library/Frameworks/AppKit.framework/AppKit';
 
function NSWorkspaceDidWakeNotification: Pointer;
begin
  Result := NSObjectToID(CocoaNSStringConst(libAppKit, 'NSWorkspaceDidWakeNotification'));
end;
 
function NSWorkspaceWillSleepNotification: Pointer;
begin
  Result := NSObjectToID(CocoaNSStringConst(libAppKit, 'NSWorkspaceWillSleepNotification'));
end;
 
function NotificationCenter: NSNotificationCenter;
begin
  Result := TNSWorkspace.Wrap(TNSWorkspace.OCClass.sharedWorkspace).notificationCenter;
end;
 
type
  IWorkspaceNotifications = interface(NSObject)
    ['{5DAFEC83-28B1-4F7E-BA87-18BC5B0F0CB2}']
    procedure onDidWakeNotification(notification: Pointer); cdecl;
    procedure onWillSleepNotification(notification: Pointer); cdecl;
  end;
 
  TWorkspaceNotifications = class(TOCLocal)
  private
    procedure AddObservers;
  protected
    function GetObjectiveCClass: PTypeInfo; override;
  public
    constructor Create;
    procedure onDidWakeNotification(notification: Pointer); cdecl;
    procedure onWillSleepNotification(notification: Pointer); cdecl;
  end;
 
{ TWorkspaceNotifications }
 
constructor TWorkspaceNotifications.Create;
begin
  inherited;
  AddObservers;
end;
 
procedure TWorkspaceNotifications.AddObservers;
begin
  NotificationCenter.addObserver(GetObjectID, sel_getUid('onDidWakeNotification:'), NSWorkspaceDidWakeNotification,
nil);
  NotificationCenter.addObserver(GetObjectID, sel_getUid('onWillSleepNotification:'), NSWorkspaceWillSleepNotification,
nil);
end;
 
function TWorkspaceNotifications.GetObjectiveCClass: PTypeInfo;
begin
  Result := TypeInfo(IWorkspaceNotifications);
end;
 
procedure TWorkspaceNotifications.onDidWakeNotification(notification: Pointer);
begin
  // Do whatever you need to, here
end;
 
procedure TWorkspaceNotifications.onWillSleepNotification(notification: Pointer);
begin
  // Do whatever you need to, here
end;
 
var
  WorkspaceNotifications: TWorkspaceNotifications;
 
initialization
  WorkspaceNotifications := TWorkspaceNotifications.Create;
 
end.


Use, modify, etc at own risk :-)

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Erick Engelke

Posts: 101
Registered: 12/5/02
Re: NSWorkSpaceDidWake on OS/X, how do you detect it  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 10, 2017 2:10 AM   in response to: Erick Engelke in response to: Erick Engelke
Wow, thanks for the detailed solution
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02