【问题标题】:Sub-Routine POSIX path issue.子例程 POSIX 路径问题。
【发布时间】:2014-01-11 05:09:29
【问题描述】:

使用脚本下载文件并将它们放置在文件夹中以进行自动安装。这是我第一次创建“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

【问题讨论】:

    标签: macos unix applescript


    【解决方案1】:

    这应该可以解决它。

    它针对“系统事件”而不是“查找器”,过去几年中,查找器字典中的许多功能已转移到系统事件。

    我还简化了提取 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
    

    【讨论】:

    • 按预期工作。最近遇到了另一个与tell Finder 调用有关的问题,该问题也已使用系统事件解决。所以感谢您解释这种转变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多