在obj-c中向applescript添加变量

时间:2016-03-11 22:07:07

标签: objective-c applescript

在不将脚本作为文件并以此方式运行的情况下,不确定在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 \

使用:

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

并且

end tell", windowName];

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

1 个答案:

答案 0 :(得分:0)

像这样构建字符串;

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

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