Sign in Join
Wiki copytemporary

Bounce Application Icon in Dock

Overview

Bouncing your application's icon in the dock can be useful to attract a user's attention when, for example, a long-running task has finished.

To do this you use the requestUserAttention method. There are two types of request:

Activating the application cancels the user attention request. A spoken notification ("Excuse me, MyApp dot app needs your attention") will occur if spoken notifications are enabled. Sending requestUserAttention to an application that is already active has no effect.

Example code

This example will bounce the application's dock icon every second (Timer1.Interval default) while the application is not in focus.

pascal
unit Unit1;

{$mode objfpc}{$H+}
{$modeswitch ObjectiveC1}

interface

uses
  Classes, SysUtils, Forms, Controls, Dialogs, ExtCtrls, StdCtrls, CocoaAll;

type

  { TForm1 }

  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

  FRequestUserAttentionID : Int64;    // Use an Integer if 32 bit application

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  NsApp := NSApplication.sharedApplication;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  If(Form1.Active) then
    // no point trying to bounce if application has focus
    exit
  else
    // one shot bounce, no voice notification
    FRequestUserAttentionID := NSApp.requestUserAttention(NSInformationalRequest);
end;

end.

While the FRequestUserAttentionID variable is not used in this demonstration, it is useful if you need to cancel the requestUserAttention which you can do with:

pascal
   NSApp.cancelUserAttentionRequest(FRequestUserAttentionID);

If you are never going to use it in your application, you can omit declaring the variable and omit its assignment.

See also

External links


From the Free Pascal wiki, page "Bounce Application Icon in Dock", as it stood on 2021-12-15 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.