Cocoa Tip: Enabling “Launch on Startup”

Cocoa Tip: Enabling “Launch on Startup”,第1张

概述转自: http://www.bdunagan.com/2010/09/25/cocoa-tip-enabling-launch-on-startup/ When I start up my Mac, I have a number of apps launch, including Jing, Evernote, LaunchBar, and BusySync. Each registers i

转自: http://www.bdunagan.com/2010/09/25/cocoa-tip-enabling-launch-on-startup/


When I start up my Mac,I have a number of apps launch,including Jing,Evernote,Launchbar,and BusySync. Each registers itself as a “Login Item”,Listed in System Preferences under Accounts. Developers have different names for this option,like “Launch on Startup” or “Launch at Login”.

In 10.4 and older,Mac OS X enabled this feature through AppleScript,and Apple has sample code for that in LoginItemsAE. In 10.5 and 10.6,the API is in LSSharedfileList.h,burIEd in the LaunchServices framework:

/System/library/Frameworks/CoreServices.framework/
Frameworks/LaunchServices.framework/headers/

I played around with this API a bit today and came up with a couple wrapper methods to abstract away the details: isLaunchAtStartup andtoggleLaunchAtStartup:. Feel free to use this code.

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 // MIT license - ( BOol )isLaunchAtStartup {      // See if the app is currently in LoginItems.      LSSharedfileListItemRef itemRef = [ self itemRefInLoginItems]; // Store away that boolean.      isInList = itemRef != nil ; // Release the reference if it exists.      if (itemRef != ) CFRelease(itemRef);   return isInList; }   - ( IBAction )toggleLaunchAtStartup:( ID )sender { @H_403_245@ // Toggle the state. shouldBetoggled = ![ isLaunchAtStartup]; // Get the LoginItems List. LSSharedfileListRef loginItemsRef = LSSharedfileListCreate( NulL ,kLSSharedfileListSessionLoginItems, ); (loginItemsRef == ) ; (shouldBetoggled) {          // Add the app to the LoginItems List.          CFURLRef appUrl = (CFURLRef)[ NSURL fileURLWithPath:[[ NSBundle mainBundle] bundlePath]]; @H_306_301@ LSSharedfileListItemRef itemRef = LSSharedfileListInsertItemURL(loginItemsRef,kLSSharedfileListItemLast,appUrl,monospace!important; Font-size:1em!important; direction:ltr!important; display:inline!important">);          (itemRef) CFRelease(itemRef); } else { // Remove the app from the LoginItems List. itemRefInLoginItems]; LSSharedfileListItemRemove(loginItemsRef,itemRef); ) CFRelease(itemRef); } CFRelease(loginItemsRef); }   - (LSSharedfileListItemRef)itemRefInLoginItems { LSSharedfileListItemRef itemRef = ; *itemUrl = ;   // Get the app's URL. *appUrl = [ mainBundle] bundlePath]]; // Get the LoginItems List. ); return ; // Iterate over the LoginItems. NSArray *loginItems = ( *)LSSharedfileListcopySnapshot(loginItemsRef,monospace!important; Font-size:1em!important; direction:ltr!important; display:inline!important">); for ( int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) { // Get the current LoginItem and resolve its URL. @H_359_419@ LSSharedfileListItemRef currentItemRef = (LSSharedfileListItemRef)[loginItems objectAtIndex:currentIndex]; (LSSharedfileListItemResolve(currentItemRef,(CFURLRef *) &itemUrl,monospace!important; Font-size:1em!important; direction:ltr!important; display:inline!important">) == noErr) {              // Compare the URLs for the current LoginItem and the app.              ([itemUrl isEqual:appUrl]) {                  // Save the LoginItem reference.                  itemRef = currentItemRef;              } } } // Retain the LoginItem reference. ) CFRetain(itemRef); // Release the LoginItems Lists. [loginItems release]; CFRelease(loginItemsRef);   itemRef; }
总结

以上是内存溢出为你收集整理的Cocoa Tip: Enabling “Launch on Startup”全部内容,希望文章能够帮你解决Cocoa Tip: Enabling “Launch on Startup”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1061932.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存