Page 1 of 1

Lazarus Pascal – Unit to Add or Remove Autorun items (Windows/MacOSX)

Lazarus Pascal – Unit to Add or Remove Autorun items (Windows/MacOSX)
   0

When developing an application, sometimes it can be useful to offer the user a way to add this application to the LoginItems (Mac) or Autorun items (Windows), so that the application starts when MacOS X or Windows starts.

Since I have developed a couple applications for which this would be helpful, and as of lately even one for both platforms, I figured, why not place this in a unit so the code can be resused easily and quickly. Sharing the code would make life easier for others as well.

So here we are – feel free to post improvements – a unit that Adds, Removes and Checks if an application build with Lazarus Pascal, from the Loginitem/Autorun items.




Functions to Add, Remove or Check Autorun items

The idea is to add or remove an application in a userinterface to the items that start when the Operating System (Windows or Mac) starts.

With that comes the need to check if such an item already exists, so the user interface can reflect that this has been set, or not.

Adding, Checking and Removing items is done based on the project Title (application.Title) as set in the “Project Options”, in Lazarus Pascal, of your project.
The path is derived from where you started your application from.

Basics

These functions have been written to work for Windows and MacOS X.

For Windows we use the registry to add, remove or check startup items.
For MacOS X we use “osascript” to add, remove or check login items.

In both cases it’s for the current user only!

As you can see, I’ve used a lot of {$IFDEF} statements to encapsulate what is needed, depending on the platform you’re using.

If anyone knows of a valid, one-fits-all method for Linux: please let me know!

To keep things simple, none of the functions require a parameter.
Don’t forget: The name is taken from “Project Options” “Title”, and the path is taken from where the program was exceuted.

Installation

Simply copy the file t4a_loginitems.pas into your project directory and add it to the “Uses” clause.

Function: CheckLoginItem : boolean

This function checks if your application is already in the list of items that should start automatically.

The result will be a boolean, so you can use it right away to assign this to a TCheckBox.

Function: AddLoginItem : boolean

With “AddLoginItem” you can add the application to the auto start items.
Application name and Application path are determined automatically.

The result will be TRUE if the adding succeeded.

Function: RemoveLoginItem : boolean

This function removes you application from the startup items, and returns TRUE if it succeeded.

Example of use


1
2
3
4
5
6
7
8
  // Assign to TCheckBox if item is in AutoRun
  cbItemInAutostart.checked := CheckLoginItem;
 
  // Add or Remove your application from AutoRun
  if cbItemInAutostart.checked then
    AddLoginItem
  else
    RemoveLoginItem;

Source

You can download the source or copy it from below …

DOWNLOAD - T4a Loginitems Pas 

Platform: Windows, Mac OS X
Filename: t4a_loginitems.pas_.zip
Version: 1.1
Size: 1.3 KiB
Date: May 10, 2016
 Download Now 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
   Unit   : t4a_loginitems

   Version: 1.1
   Author : Hans Luijten, Tweaking4All.com

   Purpose: This unit provides 3 simple functions, that work for MacOS X and Windows,
            to add, check or remove your application for Login Items (MacOSX) or
            the registry (Windows), for auto startup when the OS starts.

   Copyright Hans Luijten, Tweaking4All.com, you're free to use this code as long
   as you keep this copyright statement in tact.
}

unit t4a_loginitems;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils {$IFDEF Darwin}, process{$ENDIF} , Forms {$IFDEF Windows}, registry {$ENDIF};

function  CheckLoginItem  :Boolean;
function  AddLoginItem    :Boolean;
function  RemoveLoginItem :Boolean;

implementation


{
  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;

end.

 

Donation options


Donations are very much appreciated, but not required. Donations will be used for web-hosting expenses, project hardware or a motivational boost (a drink or snack). Thank you very much for those have donated already! It's truly AwEsOmE to see that folks like our articles and small applications.

Comments


There are no comments yet.
You can post your own comments by using the form below, or reply to existing comments by using the "Reply" button.



Your Comment …

Friendly request to not post large files here (like source codes, log files or config files). Please use the Forum for that purpose.

Please share:
*
*
Notify me about new comments (email).
       You can also use your RSS reader to track comments.


Tweaking4All uses the free Gravatar service for Avatar display.
Tweaking4All will never share your email address with others.