Register as Login Item with Cocoa?

后端 未结 7 973
花落未央
花落未央 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: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];
      }
    }
    

提交回复
热议问题