Objective c 在obj-c中向applescript添加变量

Objective c 在obj-c中向applescript添加变量,objective-c,applescript,Objective C,Applescript,不确定在objective-c中,如果不将脚本作为文件并以这种方式运行,是否可以执行此操作 NSAppleScript *appleScriptGetHH = [[NSAppleScript alloc] initWithSource:@\ "tell application \"System Events\"\n \ tell process \"Grabee\"\n

不确定在objective-c中,如果不将脚本作为文件并以这种方式运行,是否可以执行此操作

    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"];
这非常有效,但我想做的是用字符串替换“Demo Window 1”(因为它将在程序中动态更改)

当我用这种东西的时候

NSString *windowName = @"Green Window4";
然后替换此行:

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


我收到一个错误,有太多的参数,有没有办法在不分开脚本的情况下执行此操作?

像这样构建字符串

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

或者将字符串作为一个单独的步骤来构建,以提高可读性。

我得到“没有可见的@interface for'Nsapplescript”声明选择器“initwith…”hmmI遗漏了一个[NSStringOh我多么希望我能为每一个“代码注入101”不及格的木偶得到一美元。永远不要评估包含未初始化输入的生成代码。这是一场安全灾难和可靠性噩梦。通过NSAppleScript参数化AppleScript运行。或者,如果AppleScript代码是你的应用程序的一部分(与用户提供的相反),只需使用直接调用AS处理程序。
end tell", windowName];
  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]
  ];