0

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!

Community
  • 1
  • 1
  • Hm, if using my solution the spaces should not be the problem, they should be passed to the shell script correctly. The problem could occur inside the `myCommand.command` and how the given path is handled there! – ShooTerKo Mar 04 '15 at 20:16
  • @ShooTerKo That is correct your space solution worked well! There may be a second problem occurring in the path too, however, the current issue is that my file path is being cut off due to a space in the file path (Applescript cuts off file paths with spaces as shown above) – mmilutinovic1313 Mar 05 '15 at 12:00
  • 1
    **Don't do code generation via string mashing. It is evil and wrong.** The web's never-ending proliferation of SQL injection attacks are a testament to that. If you have to use `NSAppleScript`, e.g. to run user-supplied scripts, use `-executeAppleEvent:error:` to pass any variables to the script as parameters (http://appscript.sourceforge.net/nsapplescript.html). Alternatively, if the AppleScript code is part of your app, just call its handlers directly from your ObjC code via the AppleScript-ObjC bridge (http://appscript.sourceforge.net/asoc.html). – foo Mar 06 '15 at 14:56

1 Answers1

0

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

ShooTerKo
  • 2,242
  • 1
  • 13
  • 18
  • Hi @Michael. When I put those values as the full script, I get this as an output result: "/Users/lab/Library/Developer/Xcode/DerivedData/Branch_Copier_GUI-fqaphewyolyltbehjgtknknowmrr/Build/Products/Debug/Branch Copier GUI.app/Contents/Resources/myCommand.command 'Dev1.4' '/Users/lab/Desktop/untitled folder'" with administrator privileges It doesn't seem to hook the parameters into the shell script with that syntax either. Do you know what may have caused this? – mmilutinovic1313 Mar 04 '15 at 11:32
  • Hi @ShooTerKo. The reason I'm trying to use an applescript instead of just using a shell script because I needed to run the (shell / terminal) script as an administrator and tutorials that I looked at to use sudo with a shell script inside an Obj-C project seemed not to work / too involved for my use-case. If you have any advice on trying to do it that way, I am all ears! :) – mmilutinovic1313 Mar 04 '15 at 11:42
  • Clarification: The output in my first comment in this post is from your suggested output. The output from my code was indeed what you had thought it would be. – mmilutinovic1313 Mar 04 '15 at 11:55