Vikas Bansal
Vikas Bansal

Reputation: 11760

How to parse NSString value from __NSCFType?

I am trying to get all the startup(login) application of my OSX10. In order to do so I have written the this code (given below):

-(NSMutableArray*)getStartUpApplicaitonPaths{
    // Get the LoginItems list.
    LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    if (loginItemsRef == nil) return nil;
    // Iterate over the LoginItems.
    NSArray *loginItems = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);

    NSMutableArray* data = [NSMutableArray arrayWithArray:loginItems];

    return data;
}

From the above code I am getting an NSMutableArray of __NSCFType objects. When I am trying to get the path by converting a object of the array

NSString* file = [NSString stringWithFormat:@"%@", [startupFiles objectAtIndex:0]];

I am getting the result given below:

BundleBinding [0x103] URL: file:///Applications/iTunes.app/Contents/MacOS/iTunesHelper.app/ bundle identifier: com.apple.iTunesHelper iTunesHelper

I need to parse the URL of and Identifier from the string given above. Please help.

Upvotes: 0

Views: 677

Answers (1)

zaph
zaph

Reputation: 112857

The objects ar eof type: LSSharedFileListItem which is only documented in the header file.

Here is some code that may help, it will NSLog() all the file names:

NSURL *itemURL = [[NSBundle mainBundle] bundleURL];
CFURLRef URLToToggle = (__bridge CFURLRef)itemURL;

LSSharedFileListRef loginItems = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, /*options*/ NULL);
if (loginItems) {
    UInt32 seed = 0U;
    Boolean found;

    CFArrayRef currentLoginItems = LSSharedFileListCopySnapshot(loginItems,
                                                                &seed);
    const CFIndex count = CFArrayGetCount(currentLoginItems);
    for (CFIndex idx = 0; idx < count; ++idx) {
        LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(currentLoginItems, idx);
        CFURLRef outURL = NULL;

        const UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
#if (__MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10)
        outURL = LSSharedFileListItemCopyResolvedURL(item, resolutionFlags, /*outError*/ NULL);
        if (outURL == NULL) {
            if (outURL)
                CFRelease(outURL);
            continue;
        }
#else
        OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &outURL, /*outRef*/ NULL);
        if (err != noErr || outURL == NULL) {
            if (outURL)
                CFRelease(outURL);
            continue;
        }
#endif
        found = CFEqual(outURL, URLToToggle);
        NSLog(@"%@", outURL);
        CFRelease(outURL);
    }

    CFRelease(currentLoginItems);
    CFRelease(loginItems);
}

Output in my instance:

file:///Volumes/User/dgrassi/Library/PreferencePanes/MouseLocator.prefPane/Contents/Resources/MouseLocatorAgent.app/
file:///Applications/iTunes.app/Contents/MacOS/iTunesHelper.app/
file:///Applications/Dropbox.app/
file:///Library/PreferencePanes/Screens%20Connect.prefPane/Contents/MacOS/Screens%20Connect.app/
file:///Library/Application%20Support/EyeTV/EyeTV%20Helper.app/ file:///Applications/Carbon%20Copy%20Cloner.app/Contents/Library/LoginItems/CCC%20User%20Agent.app/

This came from seafile-client

Upvotes: 1

Related Questions