Question

Some applications have a check button for "start at login"...

Start on login http://b2.s3.p.quickshareit.com/files/growlstartup49525.png

How would you implement this?

Was it helpful?

OTHER TIPS

See my answer to the earlier question Register as Login Item in Cocoa.

Growl is open source.

Copy this part of code in .m file:

- (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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top