2011-09-07 162 views
0

這是我的原始腳本。它會返回Safari的當前網址如何使用[[NSAppleScript alloc] initWithSource來檢查應用程序是否正在運行:

NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to return URL of front document as string"]; 

如果我想在查詢腳本返回URL之前檢查Safari瀏覽器是否打開,該怎麼辦?

這是我在的AppleScript編輯器怎麼辦。所以這個腳本會檢查是否Safari瀏覽器正在運行或不..這適用於AppleScript的編輯

tell application "Safari" 
     if it is running then 

      //return url code here 
     end if 
    end tell 

我現在需要的是馬上叫從使用我的可可應用程序腳本「[[NSAppleScript頁頭] initWithSource:」

我已經試過,但它不工作

NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" if it is running to return URL of front document as string"]; 
+2

你有看'NSRunningApplication'?這將給你所需的信息,而不訴諸於AppleScript。 –

回答

4

爲什麼將這項工作?這是壞的AppleScript語法。

有辦法可以做到這一點,而不訴諸於AppleScript,但它現在會做。

NSString *source = @"tell application \"Safari\"\nif it is running then\nreturn URL of front document as string\nend if\nend tell"; 

您還可以通過將一個接一個,這使得它更易於閱讀分手字符串常量:您可以通過使用C轉義序列\n插入新行有一個嵌入的腳本多行:

NSString *source = 
    @"tell application \"Safari\"\n" 
     "if it is running then\n" 
      "return URL of front document as string\n" 
     "end if\n" 
    "end tell"; 

C編譯器會將這些字符串常量粘在一起成爲一個NSString對象。

1

OP的AppleScript單行代碼是錯誤的。在這個的AppleScript的文本應該工作:

NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to if it is running then return URL of front document as string"]; 
0

由於dougscripts(+1)所指出的那樣,但我想讓爲何NSAppleScript內的一個襯墊AppleScript的語法OP試過沒有工作,這一點更清晰。

而且說實話,我確實認爲這失去了三個減爲兩個

的OP的NSAppleScript代碼編輯:

NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" if it is running to return URL of front document as string"]; 

沒有工作,因爲語法是錯誤的。

的正確語法應該是:

NSAppleScript *scriptURL= [[NSAppleScript alloc] initWithSource:@"tell application \"Safari\" to if it is running then return URL of front document as string"]; 

有以下粗體所示的代碼的部分中的兩個的變化。

\ 「Safari瀏覽器\」 如果正在運行然後返回URL

相關問題