2014-01-10 40 views
0

使用腳本來下載文件並將它們放在文件夾中進行自動安裝。這是我第一次創建了「on」子例程,並且遇到了解決路徑問題以檢查文件是否存在的問題。該過程使用終端窗口來監視下載狀態。子例程POSIX路徑問題。

set theContentPath to POSIX path of (choose folder) 
    set toInstall to (theContentPath & "ToInstall/") 
    set inStalled to (theContentPath & "Installed/") 
    set theTemp to (theContentPath & "theTemp/") 
    do shell script "mkdir -p " & quoted form of toInstall 
    do shell script "mkdir -p " & quoted form of inStalled 
    do shell script "mkdir -p " & quoted form of theTemp 

    set theList to {"http://url1.pkg", 
    "http://url2.pkg", 
    "http://url3.pkg", 
    "http://url4.pkg", 
    "http://url5.pkg", 
    "http://url6.pkg"} 

    repeat with x from 1 to (count theList) 
     --display dialog item x of theList 
     set thisOne to item x of theList 
     getFileIfNeeded(thisOne, toInstall, theTemp) 
    end repeat 

    on getFileIfNeeded(theFileToGet, whereToSave, tempFiles) 
     set AppleScript's text item delimiters to "/" 
     set theItems to text items of theFileToGet 
     set theFile to item 5 of theItems 
     set theFileCheck to whereToSave & theFile 
     set theURL to quoted form of theFileToGet 
     tell application "Finder" 
      if not (exists file theFileCheck) then 
       tell application "Terminal" 
        if (count of windows) is 0 then 
         activate 
        end if 
        do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window 
        do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window 
       end tell 
      end if 
     end tell 
    end getFileIfNeeded 

嘗試添加以下set theFileCheck2 to (POSIX file theFileCheck) as string但結果不是我所期待的文件仍然得到的下載。

這是我試圖獲得正確路徑的例程。

 on getFileIfNeeded(theFileToGet, whereToSave, tempFiles) 
      set AppleScript's text item delimiters to "/" 
      set theItems to text items of theFileToGet 
      set theFile to item 5 of theItems 
      set theFileCheck to whereToSave & theFile 
      set theURL to quoted form of theFileToGet 
      tell application "Finder" 
       set theFileCheck2 to (POSIX file theFileCheck) as string 
       if not (exists file theFileCheck2) then 
        tell application "Terminal" 
         if (count of windows) is 0 then 
          activate 
         end if 
         do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window 
         do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window 
        end tell 
       end if 
      end tell 
     end getFileIfNeeded 

回答

1

這應該解決它。

它針對的是「系統事件」而不是「Finder」,以前在Finders字典中的許多功能在過去幾年中都轉移到了系統事件。

另外我簡化了提取文件的部分,在這種情況下可以使用'last'關鍵字而不是硬編碼。

on getFileIfNeeded(theFileToGet, whereToSave, tempFiles) 
    set AppleScript's text item delimiters to "/" 
    set theFile to the last text item of theFileToGet 
    set theFileCheck to whereToSave & theFile 
    set theURL to quoted form of theFileToGet 
    tell application "System Events" 
     if not (exists file theFileCheck) then 
      tell application "Terminal" 
       if (count of windows) is 0 then 
        activate 
       end if 
       do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window 
       do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window 
      end tell 
     end if 
    end tell 
end getFileIfNeeded 
+0

按預期工作。使用系統事件解決了另一個最近發現的Finder調用問題。所以謝謝你解釋這個轉變。 – davidt