在我的 Cocoa 程序中,我想检查哪些程序注册为在启动时运行,并根据需要修改该列表。为了与 Tiger 兼容,我似乎需要通过 AppleScript 进行工作。我目前有以下代码:
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,它的文档很少。最好的引用在这里: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];
}
最佳答案
关于objective-c - 通过 AppleScript 在 Objective-C 中编辑 Mac OS X 登录项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2702961/