在具有多个重复的if else语句中的applescript if else语句

时间:2014-11-03 02:30:15

标签: excel if-statement applescript repeat

我无法让此代码正常运行。这是代码:

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"到细胞。不知道为什么它不起作用。

2 个答案:

答案 0 :(得分:1)

所有end tellend if等都是一个很大的混乱。另一件事是我不明白为什么你需要第二个嵌套repeat - 循环......

但我想我明白了:你想要

  1. 从Excel工作表中读取值
  2. 假装在Safari的地址栏中输入读出值
  3. 使用自动填充并读出找到的网站的网址
  4. 将结果写入相邻的Excel单元格
  5. 在您的帖子编辑之后,我将代码编辑为:

    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
    

    问候,迈克尔/汉堡

答案 1 :(得分:0)

这是另一种解决方案。我在系统事件部分中测试了不同的延迟值,并找到了从Safari获取网址的更好方法。看看脚本的两个主要部分,其他一切都不需要改变:

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

问候,迈克尔/汉堡