Applescript Syntax (Multiple Args) Inside Objective-C File

谁说我不能喝 提交于 2019-12-25 05:31:39

问题


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.

This is what the script looks like as an 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

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).

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.

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

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

EDIT:

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.

For example:

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

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

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!


回答1:


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

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

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



来源:https://stackoverflow.com/questions/28840814/applescript-syntax-multiple-args-inside-objective-c-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!