0

Not sure if this one is possible in objective-c without having the script as a file and running it that way.

    NSAppleScript *appleScriptGetHH = [[NSAppleScript alloc] initWithSource:@\
                              "tell application \"System Events\"\n \
                              tell process \"Grabee\"\n \
                              with timeout of 0 seconds\n \
                              get value of attribute \"AXValue\" of text area 1 of scroll area 1 of window \"Demo Window 1" \n \
                              end timeout \n \
                              end tell \n \
                              end tell"];

This works perfectly, but what I would like to do is replace "Demo Window 1" with a string (as it will be changed dynamically in the program)

When I use something like this

NSString *windowName = @"Green Window4";

And then replace this line:

get value of attribute \"AXValue\" of text area 1 of scroll area 1 of window \"Demo Window 1" \n \

With:

get value of attribute \"AXValue\" of text area 1 of scroll area 1 of window \"%@" \n \

And

end tell", windowName];

I receive an error that there are too many arguments, is there a way to do this without having the script separate?

bengerman
  • 113
  • 2
  • 10

1 Answers1

0

Build the string like this;

  NSAppleScript *appleScriptGetHH = [[NSAppleScript alloc] initWithSource:
         [[NSString alloc] initWithFormat:@\
                          "tell application \"System Events\"\n \
                          tell process \"Grabee\"\n \
                          with timeout of 0 seconds\n \
                          get value of attribute \"AXValue\" of text area 1 of scroll area 1 of window \"%@"  \n \
                          end timeout \n \
                          end tell \n \
                          end tell", windowName]
  ];

or build the string as a separate step for a bit more readability.

john elemans
  • 2,578
  • 2
  • 15
  • 26
  • I get "No visible @interface for 'Nsapplescript' declares the selector 'initwith.......' hmm – bengerman Mar 11 '16 at 22:57
  • I left out one [ before [NSString – john elemans Mar 11 '16 at 23:21
  • Oh how I wish I had a dollar for every muppet who flunked 'Code Injection 101'. **Never eval generated code containing unsanitized inputs.** It is a security disaster and a reliability nightmare. [Here's the correct, safe way](http://stackoverflow.com/questions/19759149/cocoa-run-applescript-that-already-contains-double-quotes/19774696#19774696) to parameterize an AppleScript run via NSAppleScript. Or, if the AppleScript code is part of your app (as opposed to user-supplied), just use the [AppleScriptObjC bridge](http://appscript.sourceforge.net/asoc.html) to call your AS handlers directly. – foo Mar 14 '16 at 09:17