Applescript中的嵌套重复循环

时间:2014-02-19 19:13:39

标签: applescript nested-loops

以下代码有两个列表:myURL和myIMG。我想循环浏览myURL中列出的网站,截取屏幕截图,然后将它们保存在我的共享文件夹中作为myIMG中列出的文件名。因此,google.com的图片将保存为" google.jpg"等等。 myIMG中的列表对应于myURL中的列表。

这就是我所拥有的,但它并不是很有效。它从myURLs加载第一个URL,截取它的截图,然后循环遍历myIMG中的每个文件名,并保存一个屏幕截图以及所有这些不同的文件名。然后,它在myURLs中加载下一个URL并执行相同的操作。如何在myIMGs列表中设置文件名以对应于从myURLs加载的URL?我在使用嵌套循环时遇到了麻烦。

set myURLs to {"https://google.com", "https://wikipedia.org", "https://bing.com", "https://apple.com"}

set myIMGs to {"google.jpg", "wikipedia.jpg", "bing.jpg", "apple.jpg"}

-- Sets settings of Safari without interfering with user's settings.
repeat with myURL in myURLs
tell application "Safari"
    set myDoc to front document
    set windowID to id of window 1
    do JavaScript ("window.open('" & myURL & "','_blank','titlebar=0');") in document 1
    close window id windowID
    set the bounds of the front window to {0, 20, 915, 812}
    delay 5
end tell

-- Take screenshot, crop and save to Shared folder
repeat with myIMG in myIMGs

    do shell script "screencapture -o -l$(osascript -e 'tell app \"Safari\" to id of window 1') /Users/Shared/" & myIMG
    set this_file to "Macintosh HD:Users:Shared:" & myIMG
    try
        tell application "Image Events"
            -- start the Image Events application
            launch
            -- open the image file
            set this_image to open this_file
            -- get dimensions of the image
            copy dimensions of this_image to {W, H}
            -- Crops off the Safari header
            crop this_image to dimensions {W, H - 50}
            -- save the changes
            save this_image with icon
            -- purge the open image data
            close this_image
        end tell
    end try
end repeat
end repeat

1 个答案:

答案 0 :(得分:0)

您不想使用两个嵌套的重复循环...您想重复并增加变量n,然后使用每个列表的项目n。 这是改变后的代码......经过测试并按预期工作:

set myURLs to {"https://google.com", "https://wikipedia.org", "https://bing.com", "https://apple.com"}

set myIMGs to {"google.jpg", "wikipedia.jpg", "bing.jpg", "apple.jpg"}

-- Sets settings of Safari without interfering with user's settings.
repeat with n from 1 to (count myURLs)
    set myURL to item n in myURLs
    tell application "Safari"
        set myDoc to front document
        set windowID to id of window 1
        do JavaScript ("window.open('" & myURL & "','_blank','titlebar=0');") in document 1
        close window id windowID
        set the bounds of the front window to {0, 20, 915, 812}
        delay 5
    end tell

    -- Take screenshot, crop and save to Shared folder
    set myIMG to item n in myIMGs

    do shell script "screencapture -o -l$(osascript -e 'tell app \"Safari\" to id of window 1') /Users/Shared/" & myIMG
    set this_file to "Macintosh HD:Users:Shared:" & myIMG
    try
        tell application "Image Events"
            -- start the Image Events application
            launch
            -- open the image file
            set this_image to open this_file
            -- get dimensions of the image
            copy dimensions of this_image to {W, H}
            -- Crops off the Safari header
            crop this_image to dimensions {W, H - 50}
            -- save the changes
            save this_image with icon
            -- purge the open image data
            close this_image
        end tell
    end try
end repeat

您可以通过以编程方式根据URL字符串设置文件名来简化更多操作,然后您只需维护一个列表。