objective-c - 通过 AppleScript 在 Objective-C 中编辑 Mac OS X 登录项

标签 objective-c cocoa authentication applescript

在我的 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];
}

最佳答案

Apple 有一些源代码可以管理 Tiger 和更早版本的登录项。我相信你应该从 ADC 得到它,但我发现它漂浮在这里:

LoginItemAPI.h

LoginItemAPI.c

关于objective-c - 通过 AppleScript 在 Objective-C 中编辑 Mac OS X 登录项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2702961/

相关文章:

iphone - “NSString”可能无法响应 '-hexIntValue' 警告,功能正常

iphone - 修改后的 .m34 的 CATransform3D 破坏了 iOS 6 中的 View 层次结构/排序,但没有破坏它所应用的 View

ios - AFNetworking 发送 JSON 作为 POST 请求的参数

php - 如何获取服务器上用户的日历信息?

python - 如果我切换到自定义用户模型,是否必须迁移所有现有用户帐户?

security - Web-Api v2 个人用户身份验证 - 自定义并在网络场内

objective-c - 无法使用 [self theMethod :] 调用类方法

cocoa - Interface Builder 的隐藏功能?

cocoa - 我如何收到新窗口打开的通知?

objective-c - 用于距离的 objective-c 字符串格式化程序