Cocoa/Objective-c'系统();' 表现得很奇怪

Cal*_*l S 0 terminal cocoa applescript system objective-c

我正在尝试使用system()在我的Cocoa应用程序中运行一个applescript; function - 我传递给函数的字符串在终端工作,而且applescript本身很好,我认为它与NSString有关 - 有人可以帮忙吗?

        //add to login items
        NSLog(@"add to login");
        NSString *pathOfApp = [[NSBundle mainBundle] bundlePath];
        NSString *theASCommandLoginItem = [NSString stringWithFormat:@"/usr/bin/osascript -e 'tell application \"System Events\" to make login item at end with properties {path:\"%@\"}'", pathOfApp];
        system(theASCommandLoginItem);
        NSLog(theASCommandLoginItem);
Run Code Online (Sandbox Code Playgroud)

这是输出:

2009-10-11 20:09:52.803 Talking Cloud Notifier [3091:903]添加到登录sh:\ 340HH:命令未找到2009-10-11 20:09:52.813 Talking Cloud Notifier [3091:903]/usr/bin/osascript -e'告诉应用程序"系统事件"以使用属性{path:"/ Users/csmith/Desktop/Talking Cloud Notifier/build/Debug/Talking Cloud Notifier.app"}来结束登录项目"

在编译时我也会收到警告:

警告:从不兼容的指针类型传递'system'的参数1

Geo*_*lly 14

作为newacct的答案已经建议,你必须使用C字符串,而不是一个NSStringsystem()功能.

NSTask改用.

更好

对你来说更有用的是NSAppleScript班级:

NSAppleScript *script;
NSDictionary *errorDict;
NSAppleEventDescriptor *returnValue;
// multi line string literal
NSString *scriptText = @"tell application 'System Events'\n"
                        "make login item at end with properties {path:\"%@\"}\n"
                        "end tell";
scriptText = [NSString stringWithFormat:scriptText, pathOfApp];
script = [[[NSAppleScript alloc] initWithSource:scriptText] autorelease];
returnValue = [script executeAndReturnError:&errorDict];
if (returnValue) {
     // success
} else {
     // failure
}
Run Code Online (Sandbox Code Playgroud)

最好

查看Apple有关如何将您的应用注册为登录项目的文档.甚至有一些例子.