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”。不知道为什么它不工作。
感谢您的帮助。我喜欢你的调整。然而,我的主要问题是,如果页面没有加载,因此无法将URL设置为变量thisURL,那么脚本将崩溃而不会转到下一个r值。我正在尝试编写一个else语句,这意味着如果某个延迟时间后URL无法访问,那么该脚本将循环到下一个r值,将该单元格(“K”&r)留空并且不会使脚本崩溃。对此有何想法?谢谢你的帮助。 – Dan 2014-11-03 18:54:35
最简单的方法应该是将'set thisURL设置为文档1的URL'的try/error块。在该行之前用'try'写一行,并在错误处写入'',在该行之后将thisURL设置为“”和“结束尝试”。在**告诉Excel **块之前,你可以写'如果thisURL≠'“,那么'和**结束后告诉**'结束如果'。 – ShooTerKo 2014-11-03 19:09:04
我已经调整了主要问题以反映您的调整所做的更改。但是,它仍然崩溃。它经常说thisURL没有被定义,然后它停止脚本进入下一个r值,而不是向单元添加“NO SITE”。不知道为什么它不工作。任何想法将不胜感激。谢谢你的帮助。我现在正在使用的代码是在问题中,因为我编辑了问题以反映我现在拥有的。 – Dan 2014-11-03 20:33:14