Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - MacOS X - How to Add, Detect and Remove your app from Login Items

4 Posts
1 Users
0 Reactions
1,431 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2815
Topic starter  

I've been working on an application that users might want to add to their login items ... so how do we do that in Lazarus?

Even better: how do we detect if it's already there, and how do we remove it if the user wants it removed as well?

Based on info I found (see this forum post), an example code how you could implement this in your application (please add "Process" to your uses clause);

View: Is your App already in the Login Items?

function CheckLoginItem:Boolean;
var s:ansistring;
begin
  RunCommand('/usr/bin/osascript', ['-e','tell application "System Events" to get the name of every login item'],s);
  CheckLoginItem := (Pos('YourAppName',s)>0);
end;

Add our app to the login items:

procedure AddLoginItem;
var s:ansistring;
    appName:string;
begin
  appName := Copy(Application.ExeName,0,pos('.app/',Application.ExeName)+3);
  RunCommand('/usr/bin/osascript', ['-e','tell app "System Events" to make login item at end with properties {path:"'+appName+'", hidden:true}'],s);
end;

Remove your app from the login items:

procedure RemoveLoginItem;
var s:ansistring;
begin
  RunCommand('/usr/bin/osascript', ['-e','tell app "System Events" to delete login item "YourAppName"'],s);
end;                     

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2815
Topic starter  

Improvement on adding Login Items:

procedure AddLoginItem;
var s:ansistring;
    appName:string;
begin
  appName := Copy(Application.ExeName,0,pos('.app/',Application.ExeName)+3);
  RunCommand('/usr/bin/osascript', ['-e','tell app "System Events" to make login item at end with properties {name:"'++Application.Title'",path:"'+appName+'", hidden:true}'],s);
end;


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2815
Topic starter  

Here are the functions that I use for Windows and macOS to add, check and remove login items:

// ********************************************************************************************
// MacOSX and Windows LoginItems/AutoRun functions
//
********************************************************************************************
{
  CheckLoginItems
  Returns TRUE if your application already exists in the MacOSX LoginItems or Windows Registry (Current User).
  Application name is derived from the Title in project options.
  Example: if not CheckLoginItems then AddLoginItem;
}
function CheckLoginItem:Boolean;
{$IFDEF Darwin}
var s:ansistring;
{$ENDIF}
{$IFDEF Windows}
var tmpRegistry:TRegistry;
{$ENDIF}
begin
  {$IFDEF Darwin}
  RunCommand('/usr/bin/osascript',
             ['-e','tell application "System Events" to get the path of every login item'],s);
  CheckLoginItem := (Pos(Application.Title,s)>0);
  {$ENDIF}
  {$IFDEF Windows}
  tmpRegistry:=TRegistry.Create;
  CheckLoginItem := false;
  if (Application.Title<>'') and (Application.ExeName<>'') then
    begin
      tmpRegistry.RootKey := HKEY_CURRENT_USER; //Current User settings are stored Here
      if tmpRegistry.OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\', False) then
        CheckLoginItem := tmpRegistry.ReadString(Application.Title)<>'';
      tmpRegistry.Free;
    end;
  {$ENDIF}
end;
{
  AddLoginItem
  Adds your application as a login item for MacOSX or registry entry fot Windows and returns the appName as seen in the LoginItems/Registry.
  Application name is derived from the Title in project options.
  Example: AppName := AddLoginItem;
}
function AddLoginItem:boolean;
{$IFDEF Darwin}
var s:ansistring;
    appName:string;
{$ENDIF}
{$IFDEF Windows}
var tmpRegistry:TRegistry;
{$ENDIF}
begin
  {$IFDEF Darwin}
  appName := Copy(Application.ExeName,0,pos('.app/',Application.ExeName)+3);
  if not CheckLoginItem then
    RunCommand('/usr/bin/osascript',
               ['-e',
                'tell app "System Events" to make login item at end with properties {name: "'+Application.title+
                '", path:"'+appName+'", hidden:true}'],s);
  AddLoginItem:= CheckLoginItem;
  {$ENDIF}
  {$IFDEF Windows}
  tmpRegistry := TRegistry.Create;
  AddLoginItem := CheckLoginItem;
  if (not AddLoginItem) and (Application.Title<>'') and (Application.ExeName<>'') then
    begin
      tmpRegistry.RootKey := HKEY_CURRENT_USER; //Current User only
      if tmpRegistry.OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\', False) then
        begin
          tmpRegistry.WriteString(Application.Title,Application.ExeName);
          AddLoginItem := tmpRegistry.ReadString(Application.Title)=Application.ExeName;
        end;
      tmpRegistry.Free;
    end;
  {$ENDIF}
end;
{
  RemoveLoginItem
  Removes your application as a login item for MacOSX or registry entry for Windows.
  Application name is derived from the Title in project options (Windows) or .app package (MacOSX).
  Example: RemoveLoginItem;
}
function RemoveLoginItem:boolean;
{$IFDEF Darwin}
var s:ansistring;
{$ENDIF}
{$IFDEF Windows}
var tmpRegistry:TRegistry;
{$ENDIF}
begin
  {$IFDEF Darwin}
  RunCommand('/usr/bin/osascript',
             ['-e',
              'tell app "System Events" to delete login item "'+Application.Title+'"'],s);
  RemoveLoginItem := (s='');
  {$ENDIF}
  {$IFDEF Windows}
  tmpRegistry:=TRegistry.Create;
  RemoveLoginItem := false;
  if Application.Title<>'' then
    begin
      tmpRegistry.RootKey := HKEY_CURRENT_USER; //Current User settings are stored Here
      if tmpRegistry.OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\', False) then
        RemoveLoginItem:=tmpRegistry.DeleteValue(Application.Title);
      tmpRegistry.Free;
    end;
  {$ENDIF}
end;

Hope this is helpful ... 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2815
Topic starter  

Warning: for macOS store that are to be submitted in the Apple App Store, this method (using AppleScript) will not work as the application will be sandboxed.


   
ReplyQuote
Share: