2014-11-03 112 views
0

HI我無法讓此代碼正常工作。代碼如下:Applescript if else語句內部有多個重複的if else語句

property firstRow : 2051 
property lastRow : 5584 

set r to firstRow 

-- highly recommended 
-- close all of Safari's windows before... 
tell application "Safari" 
close windows 
end tell 


-- loop through the given row numbers 
repeat until r is (lastRow + 1) 

-- get the search value for Safari auto completion (column J) 
try 
    tell application "Microsoft Excel" 
     tell active sheet 
      set searchTerm to string value of range ("J" & r) of active sheet 
     end tell 
    end tell 
on error 
    -- no document open, exit the script 
    return 
end try 

-- open Safari and make a new window 
tell application "Safari" 
    activate 
    make new document with properties {URL:""} 
    delay 0.5 
    set pageLoaded to false 
end tell 

-- type the search value into the address field and hit return (aka select and open the first proposal) 
tell application "System Events" 
    -- here with Safari 6.1 text field 1 of group 2 of tool bar 1 of window 1 points to the URL field 
    set focused of text field 1 of group 2 of toolbar 1 of window 1 of process "Safari" to true 
    delay 0.5 
    keystroke searchTerm 
    delay 1.5 
    keystroke return 
end tell 

-- let open Safari the suggested web page and read out the finally used URL 
tell application "Safari" 
    repeat while not pageLoaded -- keep doing this loop until loading complete 
     delay 5 
     if (do JavaScript "document.readyState" in document 1) is "complete" then 
      set pageLoaded to true 
     else 
      -- not sure if this second else is needed, why not just wait until the first access has finished... 

      -- close document 1 
      -- make new document with properties {URL:""} 
      -- tell application "System Events" 
      -- delay 1.5 
      -- set focused of text field 1 of group 2 of tool bar 1 of window 1 of process "Safari" to true 
      -- delay 1.5 
      -- keystroke searchTerm 
      -- delay 1.5 
      -- keystroke return 
      -- end tell 
     end if 
     set thisULR to "NO SITE" 
    end repeat 
    try 
     set thisURL to URL of document 1 
    on error 
     set thisURL to "NO SITE" 
    end try 
    close document 1 
end tell 

-- write the result into the cell next to the key word (column K) 
tell application "Microsoft Excel" 
    if thisURL ≠ "NO SITE" then 
     tell active sheet 
      make new hyperlink of cell ("K" & r) with properties {address:thisURL, name:thisURL} 
     end tell 
    else 
     tell active sheet 
      make new cell ("K" & r) with properties {name:"NO SITE"} 
     end tell 
    end if 

end tell 

set r to r + 1 
end repeat 

如果沒有將URL保存爲變量thisURL,我無法讓代碼不會崩潰。

但是,它仍然崩潰。它經常說thisURL沒有被定義,然後它停止腳本進入下一個r值,而不是向單元添加「NO SITE」。不知道爲什麼它不工作。

回答

1

這是一個很大的混亂與所有的end tellend if等。另一件事是,我不明白爲什麼你需要第二個嵌套repeat -loop ...

但我認爲我想通了:你想

  1. 從一個Excel工作表中讀取值
  2. 假裝鍵入讀出值到Safari瀏覽器的地址欄中
  3. 使用自動填寫並讀出發現的網址網站
  4. 把結果寫入到相鄰的Excel單元格中

您的文章編輯後,我編輯的代碼,以這樣的:

property firstRow : 62 
property lastRow : 5584 

set r to firstRow 

-- highly recommended 
-- close all of Safari's windows before... 
tell application "Safari" 
    close windows 
end tell 

-- loop through the given row numbers 
repeat until r is (lastRow + 1) 

    -- get the search value for Safari auto completion (column J) 
    try 
     tell application "Microsoft Excel" 
      tell active sheet 
       set searchTerm to string value of range ("J" & r) of active sheet 
      end tell 
     end tell 
    on error 
     -- no document open, exit the script 
     return 
    end try 

    -- open Safari and make a new window 
    tell application "Safari" 
     activate 
     make new document with properties {URL:""} 
     set pageLoaded to false 
    end tell 

    -- type the search value into the address field and hit return (aka select and open the first proposal) 
    tell application "System Events" 
     -- here with Safari 6.1 text field 1 of group 2 of tool bar 1 of window 1 points to the URL field 
     set focused of text field 1 of group 2 of tool bar 1 of window 1 of process "Safari" to true 
     keystroke searchTerm 
     delay 1.5 
     keystroke return 
    end tell 

    -- let open Safari the suggested web page and read out the finally used URL 
    tell application "Safari" 
     try 
      repeat while not pageLoaded -- keep doing this loop until loading complete 
       delay 10 
       if (do JavaScript "document.readyState" in document 1) is "complete" then 
        set pageLoaded to true 
       end if 
      end repeat 
      set thisURL to URL of document 1 
      close document 1 
     on error 
      set thisURL to "NO SITE" 
      try 
       close windows 
      end try 
     end try 
    end tell 

    -- write the result into the cell next to the key word (column K) 
    tell application "Microsoft Excel" 
     if thisURL ≠ "NO SITE" then 
      tell active sheet 
       make new hyperlink of cell ("K" & r) with properties {address:thisURL, name:thisURL} 
      end tell 
     else 
      tell active sheet 
       make new cell ("K" & r) with properties {name:"NO SITE"} 
      end tell 
     end if 
    end tell 
    set r to r + 1 
end repeat 

問候,邁克爾/漢堡

+0

感謝您的幫助。我喜歡你的調整。然而,我的主要問題是,如果頁面沒有加載,因此無法將URL設置爲變量thisURL,那麼腳本將崩潰而不會轉到下一個r值。我正在嘗試編寫一個else語句,這意味着如果某個延遲時間後URL無法訪問,那麼該腳本將循環到下一個r值,將該單元格(「K」&r)留空並且不會使腳本崩潰。對此有何想法?謝謝你的幫助。 – Dan 2014-11-03 18:54:35

+0

最簡單的方法應該是將'set thisURL設置爲文檔1的URL'的try/error塊。在該行之前用'try'寫一行,並在錯誤處寫入'',在該行之後將thisURL設置爲「」和「結束嘗試」。在**告訴Excel **塊之前,你可以寫'如果thisURL≠'「,那麼'和**結束後告訴**'結束如果'。 – ShooTerKo 2014-11-03 19:09:04

+0

我已經調整了主要問題以反映您的調整所做的更改。但是,它仍然崩潰。它經常說thisURL沒有被定義,然後它停止腳本進入下一個r值,而不是向單元添加「NO SITE」。不知道爲什麼它不工作。任何想法將不勝感激。謝謝你的幫助。我現在正在使用的代碼是在問題中,因爲我編輯了問題以反映我現在擁有的。 – Dan 2014-11-03 20:33:14

0

這裏是另一種解決方案。我在系統事件部分中測試了不同的延遲值,並找到了從Safari獲取URL的更好方法。看一看在兩個主要部分腳本的,一切並不需要改變:

-- type the search value into the address field and hit return (aka select and open the first proposal) 
tell application "System Events" 
    tell process "Safari" 
     -- give time to prepare the window 
     delay 0.5 
     set focused of text field 1 of group 2 of toolbar 1 of window 1 to true 
     -- give time to prepare the address field 
     delay 0.5 
     keystroke searchTerm 
     -- give time to get the proposals 
     delay 0.5 
     keystroke return 
    end tell 
end tell 

-- let Safari open the suggested web page and read out the finally used URL 
tell application "Safari" 
    -- setting the default value 
    set thisURL to "NO SITE" 
    try 
     -- 6 tries with 5 seconds pause (-> max. 30 sec.) 
     repeat 6 times 
      try 
       -- give time to load 
       delay 5 
       -- try to access the URL, if it is not available, an error occurs and the repeat loop starts again 
       set thisURL to URL of document 1 
       -- close the document 
       close document 1 
       -- no error till now, exit the repeat loop 
       exit repeat 
      end try 
     end repeat 
    on error 
     -- just to be sure: close all windows 
     try 
      close windows 
     end try 
    end try 
end tell 

問候,邁克爾/漢堡