2016-03-11 36 views
0

不知道在objective-c中是否可以在沒有腳本作爲文件並以此方式運行的情況下執行此操作。在obj-c中向applescript添加一個變量

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

這工作完全,但我希望做的是取代「示範窗口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]; 

我收到有太多的參數錯誤,有沒有辦法做到這一點,而無需腳本分開?

回答

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

或建立字符串作爲一個單獨的步驟更多的可讀性。

+0

我得到「沒有可見的@interface爲'Nsapplescript'聲明選擇器'initwith .......'hmm – bengerman

+0

我遺漏了一個[之前[NSString –

+0

完美,謝謝! – bengerman