How to start a Mac application on startup?

PreranaPrerana INMember ✭✭

Hello All,


I need to start my application on startup. It has UI, window etc.

I went through couple of links suggesting to use CFPreferences API or to create a launchd property list file and place it in ~/Library/LaunchAgents or /Library/LaunchAgents but I am still not clear with this concept.


Can anyone guide me through this with a code snippet if possible and how exactly this has to be accomplished??

Any help will be greatly appreciated!!

Thanks a ton in advance!!

Best Answers

Answers

  • RemjithTkRemjithTk INMember

    Hi,

    You can create a ProcessStartInfo and use osascript to put your app in the log in item

    var args = String.Format (@-e 'tell application "System Events"" to make login item at end with properties {{path:""{0}"", hidden:false}}'",
    @/Applications/Your app.app);
    var processInfo = new ProcessStartInfo {
    FileName=@/usr/bin/osascript,
    Arguments=args,
    CreateNoWindow=false,
    UseShellExecute=false,
    ErrorDialog=true,
    WindowStyle= ProcessWindowStyle.Normal,
    RedirectStandardError=true,
    RedirectStandardInput=true,
    RedirectStandardOutput=true,
    };
    var prc = Process.Start (processInfo);
    prc.WaitForExit ();

  • PreranaPrerana INMember ✭✭

    Hi @RemjithTk‌ ,

    First of all Thanks a Ton for replying, I have been really struggling with this and had no idea where to start!!.. Thanks again :):)

    Now Following is what I exactly have to do to put my app in LoginItems right??

       var args = String.Format (@"-e 'tell application ""System Events"" to make login item at end with properties {{path:""{0}"", hidden:false}}'", @"/Applications/Your app.app"); 
    
       var processInfo = new ProcessStartInfo { [email protected]"/usr/bin/osascript", Arguments=args, CreateNoWindow=false, UseShellExecute=false, ErrorDialog=true, WindowStyle= ProcessWindowStyle.Normal, RedirectStandardError=true, RedirectStandardInput=true, RedirectStandardOutput=true, }; 
    
       var prc = Process.Start (processInfo); 
       prc.WaitForExit ();
    


    The args string, does it had to be exactly in the same way?? Or Have I confused it and mixed it with your explaination ?? Please do correct me if I am wrong.


    One more question, Is there a way to remove my Application from LoginItems??

  • PreranaPrerana INMember ✭✭

    Thanks a ton @RemjithTk for the answer ..:):) I will read about osascript .

  • PreranaPrerana INMember ✭✭

    @RemjithTk‌ , Now I am successfully able to add my app to LoginItems but enable to delete it. I used your code for deleting but the App does not get removed. Its neither throwing any error nor any exception, any idea why this might be happening??

    Thanks a ton in advance!

  • PreranaPrerana INMember ✭✭

    @RemjithTk‌ , Yup that did the trick :). Thanks a ton again!! :):)

  • sarahzohar88sarahzohar88 USMember

    good trick

  • I was able to use this trick. However, when a machine updates to Mojave, it seems to remove my program from the login items. Anyone else seeing this?

Sign In or Register to comment.