3

In my Cocoa program, I want to examine what programs are registered to run at startup and modify that list as I feel appropriate. In order to be compatible with Tiger it seems like I need to work through AppleScript. I currently have the following code:

NSDictionary* errorDict;
NSAppleEventDescriptor* returnDescriptor = NULL;

NSString *appleSource = @"tell application \"System Events\"\n\
get every login item\n\
end tell";
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource: appleSource];

returnDescriptor = [appleScript executeAndReturnError: &errorDict];

If I run that command in AppleScript, I get back an array of login items. However, I can't figure out how to iterate through this array in Objective-C. More specifically, I want to examine the names and paths of the programs registered to run at startup.

Any ideas?

Edit: I figured this out. Here is some sample code. The key is using AEKeyword's, which are very poorly documented. The best reference is here: http://developer.apple.com/mac/library/releasenotes/AppleScript/ASTerminology_AppleEventCodes/TermsAndCodes.html

const AEKeyword aeName = 'pnam';
const AEKeyword aePath = 'ppth';

...

NSDictionary* errorDict;
NSAppleEventDescriptor* getLoginItemsRD = NULL;
NSString *getLoginItemsSrc = @"tell application \"System Events\"\n\
                               get properties of every login item\n\
                               end tell";
NSAppleScript *getLoginItemsScript = [[NSAppleScript alloc] initWithSource: getLoginItemsSrc];
getLoginItemsRD = [getLoginItemsScript executeAndReturnError: &errorDict];
[getLoginItemsScript release];

int i;
int numLoginItems = [getLoginItemsRD numberOfItems];
for (i = 1; i <= numLoginItems; i++)
{
    NSAppleEventDescriptor *loginItem = [getLoginItemsRD descriptorAtIndex:i];
    NSString *loginItemName = [[loginItem descriptorForKeyword:aeName] stringValue];
    NSString *loginItemPath = [[loginItem descriptorForKeyword:aePath] stringValue];
}
mon4goos
  • 1,569
  • 13
  • 24

1 Answers1

2

Apple has some source code which can manage login items for Tiger and earlier. I believe you're supposed to get it from ADC but I found it floating around here:

LoginItemAPI.h

LoginItemAPI.c

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134