Objective-C 文件中的 Applescript 语法(多个参数) [英] Applescript Syntax (Multiple Args) Inside Objective-C File

查看:29
本文介绍了Objective-C 文件中的 Applescript 语法(多个参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力尝试从 Objective-C 中运行 Applescript,该脚本向终端查询以运行一些 shell 命令.我遇到了如何在 Objective-C 中格式化命令的问题,以便正确引用脚本对象.

I have been working on trying to run an Applescript from within Objective-C that queries out to the terminal to run some shell commands. I am running into an issue with how to format the command within Objective-C, so that the script object gets referenced properly.

这就是苹果脚本的样子

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 文件传递​​到终端命令文件的问题之一中进行了测试和验证 (将多个参数从 Applescript 传递到终端命令脚本).

This script has been tested and verified in one of my prior questions about passing multiple commands from an applescript file to a terminal command file (Passing Multiple Parameters from Applescript to Terminal Command Script).

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

My Objective-C coding attempt is below:

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];

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

If anyone could tell me how to properly format this line that creates the full script, that would be greatly appreciated!

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

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

Please let me know if you have any questions, and or, require more information.

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

I have discovered some new Information in regards to the problem. It seems that with @ShooTerKo 's answer the syntax problem is resolved. However what is happening to cause the applescript to fail is that the file path is not recognized in Applescript because applescript doesn't like spaces in it's file path names.

例如:

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只保存

will cause the applescript to only save

"/Users/lab/Desktop/Problem"

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

as the variable and therefore not get the correct path. This can be found in my debug mode with this error below:

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 更恰当地解释,并对这个新错误进行更多研究.再次感谢您的帮助,我真的很感激!

I will consider renaming my project to include underscores to be interpreted by applescript more appropriately and do some more research on this new error. Again thank you for the help, I really appreciate it!

推荐答案

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

What value is shown by a NSLog(fullScript);? I think you'll get a

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

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

what isn't a valid Applescript syntax. You should add another space also. BTW why do you want to write Applescript inside building a shell script? You just don't need it there, so please try just

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

希望它有所帮助,迈克尔/汉堡

Hope it helps, Michael / Hamburg

这篇关于Objective-C 文件中的 Applescript 语法(多个参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆