Register as Login Item with Cocoa?

后端 未结 7 974
花落未央
花落未央 2020-11-27 03:13

Google gave me: http://developer.apple.com/samplecode/LoginItemsAE/index.html

And I figured there must be a better way than using AppleScript Events.

So I do

相关标签:
7条回答
  • 2020-11-27 03:37

    There's an API that's new in Leopard called LSSharedFileList. One of the things it lets you do is view and edit the Login Items list (called Session Login Items in that API).

    BTW, I'm the lead developer of Growl. We haven't switched away from AE yet because we still require Tiger, but I'm thinking of dropping that for 1.2 (haven't talked it over with the other developers yet). When we do drop Tiger, we'll drop LoginItemsAE as well, and switch to the Shared File List API.


    EDIT from the year 2012: Since 2009, when I originally wrote this answer, Growl has switched to LSSharedFileList and I've left the project.

    0 讨论(0)
  • 2020-11-27 03:45

    I do this in an app I'm writing:

    Check out UKLoginItemRegistry for an easy way to do this pragmatically. Afaik, there is no way in Tiger to do this without Apple Events; in Leopard there's a better way, but if you use UKLoginItemRegistry it really is no problem. Here's the complete code for implementing an "Open at Logon" menu item

    + (bool)isAppSetToRunAtLogon {
      int ret = [UKLoginItemRegistry indexForLoginItemWithPath:[[NSBundle mainBundle] bundlePath]];
      NSLog(@"login item index = %i", ret);
      return (ret >= 0);
    }
    
    - (IBAction)toggleOpenAtLogon:(id)sender {
      if ([PopupController isAppSetToRunAtLogon]) {
        [UKLoginItemRegistry removeLoginItemWithPath:[[NSBundle mainBundle] bundlePath]];
      } else {
        [UKLoginItemRegistry addLoginItemWithPath:[[NSBundle mainBundle] bundlePath] hideIt: NO];
      }
    }
    
    0 讨论(0)
  • 2020-11-27 03:48

    Check here an open source example: https://github.com/invariant/rhpnotifier (LoginItem.m, LoginItem.h)

    0 讨论(0)
  • 2020-11-27 03:50

    I've refactored some of the answers here to provide a category on NSApplication that provides a launchAtLogin property.

    https://gist.github.com/joerick/73670eba228c177bceb3

    0 讨论(0)
  • 2020-11-27 03:59

    I stumbled across Ben Clark-Robinson's LaunchAtLoginController. A very elegant solution to a very common problem.

    0 讨论(0)
  • 2020-11-27 04:02

    SMLoginItemSetEnabled is another modern option, see Modern Login Items article by Cory Bohon where he explains that you have to create a helper application whose sole purpose is to launch the main application. There's also a full step by step explanation in SMLoginItemSetEnabled - Start at Login with App Sandboxed on Stack Overflow.

    0 讨论(0)
提交回复
热议问题