3
在我的Cocoa程序中,我想检查哪些程序已注册在启动时运行,并根据需要修改该列表。为了与Tiger兼容,似乎需要通过AppleScript进行工作。目前,我有以下代码:通过AppleScript在Objective-C中编辑Mac OS X登录项目
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];
如果我在运行该AppleScript的命令,我回来的登录项目的数组。但是,我无法弄清楚如何在Objective-C中迭代这个数组。更具体地说,我想检查在启动时运行的程序的名称和路径。
任何想法?
编辑:我明白了这一点。这里是一些示例代码。关键是使用AEKeyword's,这是非常糟糕的记录。最好的参考就是在这里: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];
}