2014-11-03 36 views
0

我有一個腳本:你可以在if else語句中重複嗎?

property firstRow : 2090 
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) 

    tell application "Microsoft Excel" 
     tell active sheet 
      set searchTerm to string value of range ("J" & r) of active sheet 
      set searchTerm1 to string value of range ("J" & r-1) of active sheet 
      if searchTerm1=searchTerm then 
      set r to r + 1 
      end tell 
      end tell 
      end repeat 
      else 
      set searchTerm to searchTerm1 
      end if 
     end tell 
    end tell 

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

它基本上作品除了第一,如果再else語句。我希望我在這裏做的事情很清楚。我對applescript很新,非常感謝你的幫助。如果發生這種情況,我試着去做,然後用下一個r值重新啓動腳本,但是如果沒有,那就繼續腳本。

回答

0

你不能在跟隨它的if子句中有一個tell塊。你可以這樣做:

tell app "Microsoft Excel" 
    If a = 1 
    set a to 2 
end tell 
else if... 

你必須使你的腳本看起來更像:

tell application "Microsoft Excel" 
    tell active sheet 
     set searchTerm to string value of range ("J" & r) of active sheet 
     set searchTerm1 to string value of range ("J" & r - 1) of active sheet 
    end tell 
end tell 

if searchTerm1 = searchTerm then 
    set r to r + 1 
    exit repeat 
else 
    set searchTerm to searchTerm1 
end if 

的另一個問題是你想有條件地退出復讀,我想。如果是這樣,則在重複塊中的條件的一部分中使用「退出重複」以退出重複。所以,試着用這兩樣東西來重寫。

+0

謝謝。但是如果我寫退出重複,那麼不會不會重複更高的r值?我希望它重複使用新的r值,而不是用舊的r值繼續執行腳本的其餘部分。 – Dan 2014-11-03 23:53:22