Objective-C 文件中的 Applescript 语法(多个参数)

Applescript Syntax (Multiple Args) Inside Objective-C File

我一直在尝试从 Objective-C 中 运行 Applescript 向终端查询 运行 某些 shell 命令。我正在 运行 解决如何在 Objective-C 中格式化命令的问题,以便正确引用脚本对象。

这就是脚本作为 applescript 的样子

set thisFile to "Dev1.4"
set testTarget to "/Users/lab/Desktop/untitled"
do shell script     "/Users/lab/Desktop/TempRoot/myCommand.command " & thisFile & space & testTarget with administrator privileges

这个脚本已经在我之前关于将多个命令从 applescript 文件传递​​到终端命令文件的问题之一中进行了测试和验证 ()。

我的 Objective-C 编码尝试如下:

NSObject *arrayObj = arguments[0];

NSString *tmpString1 = [arguments objectAtIndex:0];
NSString *tmpString2 = [arguments objectAtIndex:1];

NSString * fullScript = [NSString stringWithFormat:@"'%@' & get quoted form of %@ & space & get quoted form of %@", scriptPath, tmpString1, tmpString2];

NSDictionary *errorInfo = [NSDictionary new];
NSString *script =  [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];

NSLog(script);

NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script];
NSAppleEventDescriptor * eventResult = [appleScript executeAndReturnError:&errorInfo];

如果有人能告诉我如何正确格式化创建完整脚本的这一行,将不胜感激!

NSString * fullScript = [NSString stringWithFormat:@"'%@' & get quoted form of %@ & space & get quoted form of %@", scriptPath, tmpString1, tmpString2];

如果您有任何问题,或者需要更多信息,请告诉我。

-------------------------------------------- ------------------------------------

-------------------------------------------- ------------------------------------

编辑:

我发现了一些关于该问题的新信息。似乎有了@ShooTerKo 的回答,语法问题就解决了。然而,导致 applescript 失败的原因是 Applescript 无法识别文件路径,因为 applescript 不喜欢文件路径名中的空格。

例如:

set testTarget to "/Users/lab/Desktop/Problem Folder"
set thisFile to "Dev1.4"
do shell script "/Users/lab/Desktop/TempRoot/myCommand.command " & thisFile & space & testTarget with administrator privileges

将导致 applescript 只保存

“/Users/lab/Desktop/Problem”

作为变量,因此没有得到正确的路径。这可以在我的调试模式下找到,错误如下:

Printing description of errorDescription->*errorDescription:
/bin/sh: /Users/lab/Library/Developer/Xcode/DerivedData/Branch_Copier_GUI-fqaphewyolyltbehjgtknknowmrr/Build/Products/Debug/Branch: No such file or directory

我会考虑重命名我的项目以包含下划线,以便更恰当地由 applescript 解释,并对这个新错误进行更多研究。再次感谢您的帮助,我真的很感激!

NSLog(fullScript); 表示什么值?我想你会得到一个

[...] get quoted form of Dev1.4 [...]

什么不是有效的 Applescript 语法。您还应该添加另一个 space。顺便说一句,为什么要在构建 shell 脚本中编写 Applescript?你只是在那里不需要它,所以请试试

NSString * fullScript = [NSString stringWithFormat:@"%@ '%@' '%@'", scriptPath, tmpString1, tmpString2];

希望对您有所帮助,Michael / Hamburg