2016-07-13 24 views
0

我有這個腳本應該逐行寫入文本文件,並且如果每行中有一個URL,它應該在Safari的單獨標籤中打開它 沒有URL有效性的部分檢查工作,但它不劑量時,我使用CURL,請任何幫助嗎?在Applescript中測試URL的有效性不起作用

set myDelay to 0.2 
set errorLog to {} 
set urlText to read POSIX file "/USERS/KAMEL/transfer.txt" as «class utf8» 
set urlList to paragraphs of urlText 
repeat with aUrl in urlList 
set aUrl to contents of aUrl 
    if aUrl ≠ "" then 
    try 
     do shell script "curl " & aUrl 
      tell application "Safari" to open location aUrl 
      -- open location aUrl 
      delay myDelay 
      on error 
      set end of errorLog to aUrl 
     exit repeat 
    end try 
    end if 
end repeat 

回答

0

Safari有專門的API來創建新的選項卡。

試試這個,它使用捲曲返回HTTP狀態代碼

set urlText to read file ((path to home folder as text) & "transfer.txt") as «class utf8» 
set urlList to paragraphs of urlText 
set errorLog to {} 
tell application "Safari" to if (count documents) is 0 then make new document 
repeat with aUrl in urlList 
    set theScript to "curl -o /dev/null -s -I -w '%{http_code}' " & aUrl 
    try 
     set theStatus to (do shell script theScript) as integer 
     if theStatus < 400 then 
      tell application "Safari" 
       tell window 1 to make new tab with properties {URL:aUrl} 
      end tell 
     end if 
    on error e 
     set end of errorLog to (e & space & aUrl) 
    end try 
end repeat