<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Lazarus Pascal - macOS - Use SMAppService to add a login item (macOS 13+) - Delphi, Lazarus, Free Pascal				            </title>
            <link>https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pscal-macos-use-smappservice-to-add-a-login-item-macos-13/</link>
            <description>Tweaking4All.com Discussion Board</description>
            <language>en-US</language>
            <lastBuildDate>Tue, 09 Jun 2026 03:23:53 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>Lazarus Pascal - macOS - Use SMAppService to add a login item (macOS 13+)</title>
                        <link>https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pscal-macos-use-smappservice-to-add-a-login-item-macos-13/#post-5795</link>
                        <pubDate>Tue, 25 Mar 2025 07:11:32 +0000</pubDate>
                        <description><![CDATA[As of macOS 13, Apple introduced SMAppService as a replacement for SMLoginItemSetEnabled.Figuring out how this works was very confusing with the contradicting information I ran into - probab...]]></description>
                        <content:encoded><![CDATA[<p>As of macOS 13, Apple introduced <a href="https://developer.apple.com/documentation/servicemanagement/smappservice" target="_blank" rel="noopener">SMAppService</a> as a replacement for <a href="https://developer.apple.com/documentation/servicemanagement/smloginitemsetenabled(_:_:)" target="_blank" rel="noopener">SMLoginItemSetEnabled</a>.<br />Figuring out how this works was very confusing with the contradicting information I ran into - probably due to the difference between starting an application and starting a service or daemon.</p>
<p>Anywho, long story short, it took me a ton of work to finally figure it out. Special thanks to <strong>The Eskimo</strong> in the Apple Developer forum who quick and concise showed me how to use SMAppService properly. Without his help I'd still be bumbling around trying to figure it out.</p>
<p>So what I want to do: Have the ability to add my application, distributed through the App Store, to the Login Items.</p>
<p>For this I'll use SMAppService by calling 3 basic functions:</p>
<ul>
<li><strong>registerAndReturnError</strong> - Register/add login item</li>
<li><strong>unRegisterAndReturnError</strong> - unregister/remove login item</li>
<li><strong>status</strong> - is my app already a login item or not?</li>
</ul>
<p>These 3 functions can be called within your application, and there is no need to create a helper tool for this.<br />We do however need a link to the headers of macOS 13 (or newer) since Lazarus is still shipping with links to the macOS 10.10 SDK (if I'm recalling that correctly). Took me a lot of time and effort, but I got it to work (Lazarus /FPC can be quite the miracle if you as ask me(.</p>
<p>I created this unit, <strong>ServiceManagementAppService.pas</strong> (feel free to name it something else), basically only looking at the 3 functions I need for now:</p>
<pre contenteditable="false">unit ServiceManagementAppService;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$linkframework ServiceManagement}

interface

uses Classes, SysUtils, CocoaAll;

const
  SMAppServiceStatusNotRegistered	=0;
  SMAppServiceStatusEnabled		=1; 
  SMAppServiceStatusRequiresApproval	=2; 
  SMAppServiceStatusNotFound		=3;

  SmAppServiceStatusResult :
    Array  of string = 
                            ('Not registered',
                             'Enabled',
                             'Requires Approval',
                             'Not found' );  
type
  SMAppServiceStatus = NSInteger;
  
type
  SMAppService = objcclass external (NSObject)
  public
    function  registerAndReturnError(error: NSErrorPtr): objcbool;  message 'registerAndReturnError:';
    function  unregisterAndReturnError(error: NSErrorPtr): objcbool;  message 'unregisterAndReturnError:';
    function  status:SMAppServiceStatus; message 'status';
  end;

implementation

end.</pre>
<p> </p>
<p>Now include this unit in your application's uses clause of course and you can call register, unregister and status as showns in these example calls:</p>
<pre contenteditable="false">var
  appService: SMAppService;
  StatusReturn : SMAppServiceStatus;
  Registered:boolean;
  UnRegistered:boolean;
begin
  try
    // Create a service
    appService := SMAppService.new;

    // Resgister service
    Registered := appService.registerAndReturnError(nil);

    // Unregister service
    UnRegistered := appService.unregisterAndReturnError(nil);

    // Get current status
    // StatusReturn:
    // SMAppServiceStatusNotFound      - your app was never registered as login item
    // SMAppServiceStatusEnabled       - your app is registered as a login item
    // SMAppServiceStatusNotRegistered - your app is registered as a login item
    
    StatusReturn := appService.status;
    
  except
    Showmessage('fail');
  end;
end; </pre>
<p><span style="color: #ff0000"><strong>Caution</strong></span>: as I understand, per Apple guidelines, your app is not allowed to just add itself to the Login items without user interaction. So handle this call with care,</p>
<p> </p>
<p><em>Note</em>: Not sure if it is required, but I did compile with the '<strong>-WM13.0</strong>' compiler (<strong>Lazarus</strong> -&gt; <strong>Project</strong> -&gt; <strong>Options</strong> -&gt; <strong>Compiler Options</strong> -&gt; <strong>Custom Options</strong>) - not sure if this is a requirement.</p>
672]]></content:encoded>
						                            <category domain="https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/">Delphi, Lazarus, Free Pascal</category>                        <dc:creator>Hans</dc:creator>
                        <guid isPermaLink="true">https://www.tweaking4all.com/forum/delphi-lazarus-free-pascal/lazarus-pscal-macos-use-smappservice-to-add-a-login-item-macos-13/#post-5795</guid>
                    </item>
							        </channel>
        </rss>
		