Domanda

Alcune applicazioni hanno un pulsante di spunta per " inizio al login " ...

Avvia l'accesso http: //b2.s3.p.quickshareit. com / file / growlstartup49525.png

Come lo implementeresti?

È stato utile?

Altri suggerimenti

Vedi la mia risposta alla domanda precedente Registrati come elemento di accesso in Cocoa .

Growl è open source .

Copia questa parte di codice nel file .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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top