子例程POSIX路径问题。

时间:2014-01-10 19:45:54

标签: macos unix applescript

处理下载文件的脚本并将其放在文件夹中以进行自动安装。这是我第一次创建" on"子例程,我遇到解决路径以检查文件是否存在的问题。该过程使用终端窗口来监视下载状态。

    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 个答案:

答案 0 :(得分:1)

这应该解决它。

它的目标是“系统事件”而不是“Finder”,Finders字典中的许多功能在过去几年中已转移到系统事件。

此外,我简化了提取 theFile 的部分,在这种情况下,您可以使用'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