2014-02-19 68 views
0

以下代碼有兩個列表:myURLs和myIMGs。我想遍歷myURL中列出的網站,截取屏幕截圖,然後將它們保存在我的Shared文件夾中,作爲myIMG中列出的文件名。因此,google.com的圖片將被保存爲「google.jpg」,等等。 myIMG中的列表對應於myURL中的列表。Applescript中的嵌套重複循環

這是我的,但它不工作。它從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 

回答

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字符串的文件名簡化多了,那麼你只需要保持一個列表。

+0

這很好,謝謝你的方向。我很樂意以這種方式進行簡化,但實際的網址很長且無法理解。 – Rory

+0

我想的很多。如果你確實只想要一個列表,你可以使它像{url1,filename1,url2,filename2,url3 ...}。然後你的重複循環將是:從1重複(count myList)2,然後處理項目n和項目n + 1 – jweaks