void ChangeOurApplicationsLoginSetting ( Boolean startWithMacOS) { /* Beginning of ChangeOurApplicationsLoginSetting */ CFArrayRef theListOfObjectsToLaunchDuringLogin; // Let's get the list of objects to launch when we log on. theListOfObjectsToLaunchDuringLogin = CFPreferencesCopyValue (CFSTR ("AutoLaunchedApplicationDictionary"),CFSTR ("loginwindow"),kCFPreferencesCurrentUser,kCFPreferencesAnyHost); if (NULL != theListOfObjectsToLaunchDuringLogin) { CFBundleRef ourBundle; ourBundle = CFBundleGetMainBundle (); if (NULL != ourBundle) { CFURLRef ourURL; ourURL = CFBundleCopyBundleURL (ourBundle); if (NULL != ourURL) { CFStringRef ourURLStringRef; // Let's get the URL to our bundle so we can see if it's already in the list of objects to install ourURLStringRef = CFURLCopyFileSystemPath (ourURL,kCFURLPOSIXPathStyle); if (NULL != ourURLStringRef) { CFIndex count; CFIndex theNumberOfItems; CFDictionaryRef theCurrentSetting; CFStringRef theCurrentPath; Boolean addOurURLToTheArray; Boolean removeOurURLToTheArray; theNumberOfItems = CFArrayGetCount (theListOfObjectsToLaunchDuringLogin); for (count = 0;count < theNumberOfItems;count++) { theCurrentSetting = CFArrayGetValueAtIndex (theListOfObjectsToLaunchDuringLogin,count); if ((NULL != theCurrentSetting) && (CFDictionaryGetTypeID () == CFGetTypeID (theCurrentSetting))) { theCurrentPath = CFDictionaryGetValue (theCurrentSetting,CFSTR ("Path")); if ((NULL != theCurrentPath) && (CFStringGetTypeID () == CFGetTypeID (theCurrentPath))) { if (kCFCompareEqualTo == CFStringCompare (ourURLStringRef,theCurrentPath,0)) { // It looks like we found our own URL, so there's no need to keep looking in the array. break; } } } } if (startWithMacOS && (count == theNumberOfItems)) { // It looks like we are supposed to start Mac OS with our application, but it's not in the set // of objects to launch when the Mac OS starts up. addOurURLToTheArray = true; removeOurURLToTheArray = false; } else if (!startWithMacOS && (count < theNumberOfItems)) { // It looks like we are not supposed to have our application start up with Mac OS and it happens // to be in the array of items to start up, so we'll have to remove it. addOurURLToTheArray = false; removeOurURLToTheArray = true; } else { // It looks like the array of objects to launch hold the correct information with respect to either // launching our application or not. addOurURLToTheArray = false; removeOurURLToTheArray = false; } if (addOurURLToTheArray || removeOurURLToTheArray) { CFMutableArrayRef theMutableArray; // In order to remove or add an item from the array, we need a mutable version of the array of // objects to launch, so let's create that. theMutableArray = CFArrayCreateMutableCopy (NULL,0,theListOfObjectsToLaunchDuringLogin); if (NULL != theMutableArray) { Boolean thePreferencesWereUpdated; if (addOurURLToTheArray) { const void* listOfKeys[2]; const void* listOfValues[2]; listOfKeys[0] = CFSTR ("Hide"); listOfValues[0] = kCFBooleanFalse; listOfKeys[1] = CFSTR ("Path"); listOfValues[1] = ourURLStringRef; theCurrentSetting = CFDictionaryCreate (NULL,listOfKeys,listOfValues,2,NULL,NULL); if (NULL != theCurrentSetting) { CFArrayAppendValue (theMutableArray,theCurrentSetting); } } else if (removeOurURLToTheArray) { CFArrayRemoveValueAtIndex (theMutableArray,count); } CFPreferencesSetValue (CFSTR ("AutoLaunchedApplicationDictionary"),theMutableArray,CFSTR ("loginwindow"),kCFPreferencesCurrentUser,kCFPreferencesAnyHost); thePreferencesWereUpdated = CFPreferencesSynchronize (theApplicationID,kCFPreferencesCurrentUser,kCFPreferencesAnyHost); CFRelease (theMutableArray); theMutableArray = NULL; } } CFRelease (ourURLStringRef); ourURLStringRef = NULL; } CFRelease (ourURL); ourURL = NULL; } } CFRelease (theListOfObjectsToLaunchDuringLogin); theListOfObjectsToLaunchDuringLogin = NULL; } } /* End of ChangeOurApplicationsLoginSetting */