如何在objective-c中调整AppleScript?

时间:2018-02-04 07:10:59

标签: macos applescript

作为标题,我可以在脚本编辑器中运行脚本,但是Objective-C程序里面出来并且每次出错都会使用它,我找了很长时间的互联网,尝试了三次调用方法,但是无济于事,AppleScript如下:

tell application "Numbers"
set thisDocument to open alias "Macintosh HD:Users:csw:Desktop:Folder:ExcelTest.xlsx"
tell thisDocument
    save thisDocument in file "Macintosh HD:Users:csw:Desktop:Folder:NumbersTest10000.numbers"
end tell
close thisDocument

告诉

objective-c中调用的方法如下:

方法1:

NSAppleEventDescriptor *eventDescriptor = nil;
NSAppleScript *appleScript = nil;
NSDictionary *errorDict;

NSString *scriptSource = @"tell application \"Numbers\" \r"
"set thisDocument to open alias \"Macintosh HD:Users:csw:Desktop:Folder:ExcelTest.xlsx\" \r"
"set thisFilePath to \"Macintosh HD:Users:csw:Desktop:Folder:NumbersTest10000.numbers\" \r"
"tell thisDocument\" \r"
"save thisDocument in file thisFilePath\" \r"
"end tell\" \r"
"close thisDocument\" \r"
"end tell";

eventDescriptor = [appleScript executeAndReturnError:&errorDict];
if (eventDescriptor)
{
    NSLog(@"%@", [eventDescriptor stringValue]);
}

方法2:

NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"ExcelToNumbers" ofType:@"scpt"];
NSLog(@"scriptPath:%@", scriptPath);

if (scriptPath)
{
    NSDictionary *errorInfo;
    appleScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:scriptPath] error:&errorInfo];
    //perror("script");
    NSLog(@"errorInfo:%@", errorInfo);

    if (appleScript)
    {
        eventDescriptor = [appleScript executeAndReturnError:nil];

        if (eventDescriptor)
        {
            NSLog(@"%@", [eventDescriptor stringValue]);
        }
    }
}

而且方法3:

NSTask *task = nil;
NSBundle *bundle = [NSBundle mainBundle];
NSString *scriptPath = [bundle pathForResource:@"ExcelToNumbers" ofType:@"scpt" inDirectory:nil];
if (scriptPath) {
    NSArray *array = [NSArray arrayWithObjects:scriptPath, @"hello", nil];
    task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/osascript" arguments:array];
    NSLog(@"task:%@", task);
}

第一种方法不会打印错误日志。

在第二种方法中,错误是:

2018-02-04 14:56:33.343060+0800 Storm[5077:223513] AppleEvents failed to encode extension for /Users/csw/Desktop/Folder/ExcelTest.xlsx, err=Operation not permitted/1
2018-02-04 14:56:35.553607+0800 Storm[5077:223513] AppleEvents failed to encode extension for /Users/csw/Desktop/Folder/ExcelTest.xlsx, err=Operation not permitted/1

在第三种方法中,错误是:

2018-02-04 14:26:02.399776+0800 Storm[4675:205128] task:<NSConcreteTask: 0x6040000e3a00>
2018-02-04 14:26:02.772100+0800 osascript[4678:205194] AppleEvents failed to encode extension for /Users/csw/Desktop/Folder/ExcelTest.xlsx, err=Operation not permitted/1
2018-02-04 14:26:02.779600+0800 osascript[4678:205194] AppleEvents failed to encode extension for /Users/csw/Desktop/Folder/ExcelTest.xlsx, err=Operation not permitted/1
/Users/csw/Library/Developer/Xcode/DerivedData/Storm-dubdmupogfxhyvdftizqofiwkzrn/Build/Products/Debug/Storm.app/Contents/Resources/ExcelToNumbers.scpt: execution error: “Numbers”遇到一个错误:应用程序没有运行。 (-600)

这是什么原因?它是我的AppleScript问题或调用方法。我可以确定它不是路径问题,脚本文件已添加到项目中。 请帮帮我,谢谢。

0 个答案:

没有答案