某些应用程序有一个“登录时开始”的检查按钮......
Start on login http://b2.s3.p.quickshareit.com/files/growlstartup49525.png
你会如何实现这个?
答案 0 :(得分:4)
请参阅我之前问题Register as Login Item in Cocoa的答案。
答案 1 :(得分:3)
Growl是open source。
答案 2 :(得分:2)
根据贾斯汀·威廉姆斯的说法,这将做到这一点:http://carpeaqua.com/2008/03/01/adding-an-application-to-login-items-in-mac-os-x-leopard/
答案 3 :(得分:0)
将此部分代码复制到.m文件中:
- (void)awakeFromNib
{
[self setShouldStartGrowlAtLogin:1];
}
- (BOOL) shouldStartGrowlAtLogin {
Boolean foundIt = false;
//get the prefpane bundle and find GHA within it.
NSBundle *prefPaneBundle = [NSBundle bundleWithIdentifier:@"com.yourcompany.Menu_Item"];
NSString *pathToGHA = [prefPaneBundle bundlePath ];
//pathToGHA = [pathToGHA stringByAppendingPathComponent: @"/Contents/MacOS/Menu\ Item"];
NSLog(@"%@", pathToGHA);
if(pathToGHA) {
//get the file url to GHA.
CFURLRef urlToGHA = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)pathToGHA, kCFURLPOSIXPathStyle, true);
UInt32 seed = 0U;
NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
for (id itemObject in currentLoginItems) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef URL = NULL;
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, /*outRef*/ NULL);
if (err == noErr) {
foundIt = CFEqual(URL, urlToGHA);
CFRelease(URL);
if (foundIt)
break;
}
}
CFRelease(urlToGHA);
}
else {
NSLog(@"APP: your install is corrupt, you will need to reinstall\nyour prefpane bundle is:%@\n your pathToGHA is:%@", prefPaneBundle, pathToGHA);
}
return foundIt;
}
- (void) setShouldStartGrowlAtLogin:(BOOL)flag {
//get the prefpane bundle and find GHA within it.
NSBundle *prefPaneBundle = [NSBundle bundleWithIdentifier:@"com.yourcompany.Menu_Item"];
NSString *pathToGHA = [prefPaneBundle bundlePath ];
[self setStartAtLogin:pathToGHA enabled:1];
}
- (void) setStartAtLogin:(NSString *)path enabled:(BOOL)enabled {
OSStatus status;
CFURLRef URLToToggle = (CFURLRef)[NSURL fileURLWithPath:path];
LSSharedFileListItemRef existingItem = NULL;
UInt32 seed = 0U;
NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
for (id itemObject in currentLoginItems) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;
UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
CFURLRef URL = NULL;
OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, /*outRef*/ NULL);
if (err == noErr) {
Boolean foundIt = CFEqual(URL, URLToToggle);
CFRelease(URL);
if (foundIt) {
existingItem = item;
break;
}
}
}
if (enabled && (existingItem == NULL)) {
NSString *displayName = [[NSFileManager defaultManager] displayNameAtPath:path];
IconRef icon = NULL;
FSRef ref;
Boolean gotRef = CFURLGetFSRef(URLToToggle, &ref);
if (gotRef) {
status = GetIconRefFromFileInfo(&ref,
/*fileNameLength*/ 0, /*fileName*/ NULL,
kFSCatInfoNone, /*catalogInfo*/ NULL,
kIconServicesNormalUsageFlag,
&icon,
/*outLabel*/ NULL);
if (status != noErr)
icon = NULL;
}
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, (CFStringRef)displayName, icon, URLToToggle, /*propertiesToSet*/ NULL, /*propertiesToClear*/ NULL);
} else if (!enabled && (existingItem != NULL))
LSSharedFileListItemRemove(loginItems, existingItem);
}