Download AppleScript-复制文件夹内容显示错误-下载&;解压工程

Download AppleScript-复制文件夹内容显示错误-下载&;解压工程,download,copy,applescript,unzip,Download,Copy,Applescript,Unzip,UPDATE01:已删除已清理的代码-Zip下载,当前的问题是在“解压”完成之前执行“复制”命令;导致将空文件夹的副本转移到目标文件夹。当作为.app运行时,但在AppleScriptEditor中作为脚本运行时。。它运行良好:/ property DownloadsFolder : path to downloads folder property appSupport : path to application support from user domain property ZIPNam

UPDATE01:已删除已清理的代码-Zip下载,当前的问题是在“解压”完成之前执行“复制”命令;导致将空文件夹的副本转移到目标文件夹。当作为.app运行时,但在AppleScriptEditor中作为脚本运行时。。它运行良好:/

property DownloadsFolder : path to downloads folder
property appSupport : path to application support from user domain
property ZIPName : "ResourcesOffline.zip" -- downloaded ZIP file
property AppName : "MyApp" -- name of App in Application Support
property ExtractedFolderName : "MyContent" -- name for folder in Downloads where ZIP is saved
property ExtractedFolderPath : ((DownloadsFolder as text) & ExtractedFolderName & ":")
property DataFolder : ":UserBottle:FFApp:D_C:PData:LP:App:Data"

-- inform user of process --
display dialog "
IMPORTANT:
Before running this App, please be sure you have downloaded the 'ResourcesOffline.zip', and it is in your 'Downloads' Folder.
Press [OK] when you are ready to start.
" buttons {"Ok"}

-- Set up DSResources folders in Downloads and User's Bottle --
do shell script "mkdir -p " & quoted form of (POSIX path of ExtractedFolderPath) & space & quoted form of POSIX path of {(appSupport as text) & AppName & (DataFolder as text) & ":DSResources"}

display dialog "Check! Directories in Downloads and Data" buttons {"Ok"}

-- Extract to the folder created in Downloads --
try
    do shell script "unzip -u " & quoted form of POSIX path of ((DownloadsFolder as text) & ZIPName) & " -d " & quoted form of POSIX path of ExtractedFolderPath
on error
    display dialog "
    Process failed.
    Could not find 'ResourcesOffline.zip' in 'Downloads' folder.
    Please be sure that the file exists in the specified location.
    " buttons {"Quit"} with icon 0
    if button returned of the result is "Quit" then error number -128
end try

display dialog "Check! UnZipped in MyContent" buttons {"Ok"}

-- Copy items to the folder created in Application Support --
tell application "Finder"
    set SourceFolder to folder (ExtractedFolderPath as text)
    set DestinationFolder to folder ((appSupport as text) & AppName & (DataFolder as text))
    duplicate (entire contents of first folder of SourceFolder) to DestinationFolder with replacing
    display dialog "
    All content was copied successfully. Thank you!
    " buttons {"Ok"}
end tell

display dialog "Check! All done - About to Delete TEMP Extracted files" buttons {"Ok"}

do shell script "rm -rf " & quoted form of POSIX path of ExtractedFolderPath

quit
==========================================================================

一般来说,我不熟悉脚本编写。具备基本的理解能力,但不是一个程序员

我正在尝试编写AppleScript脚本以执行以下操作:

  • 从下载'XXX.ZIP'from'http://MyLink.com/XXX.zip"
  • 在“Downloads”文件夹中下载,并覆盖任何现有文件(如果已存在)

  • 显示下载进度条(您的大纲和脚本示例有点不同,所以我选择了大纲:

    • 根据需要在用户的下载应用程序支持文件夹中创建文件夹
    • 将zip文件下载到在下载中创建的文件夹中,并将其解压缩到该文件夹中-zip文件包含一个主文件夹,其中包含一个子文件夹(或多个文件夹),其中包含文件
    • 将主文件夹的全部内容复制到应用程序的应用程序支持中创建的文件夹中的资源文件夹中
    有几种方法可以使用一些AppleScriptObjC创建进度条或下载状态,但为了更好地控制,这些方法可能应该从应用程序中完成

    脚本的主要问题是,查找程序不理解POSIX路径,并且您错过了在应用程序支持中创建文件夹结构。有标准命令可获取各种系统文件夹的路径,因此无需进行字符串操作即可使脚本在其他机器上运行。在下面的脚本中,我跟踪常规HFS路径,只是将它们强制为shell脚本的POSIX,并为各种名称添加属性,使它们位于一个位置

    property downLoads : path to downloads folder
    property appSupport : path to application support from user domain
    property webPage : "HTTPS://MYWEBSITE.COM/STUFF/DOWNLOADABLECONTENT/"
    property webResource : "SOME_RESOURCES.ZIP" -- name for the downloaded file
    property myApp : "MYAPPLICATION" -- name for folder in Application Support (bundle identifier would be better)
    property baseName : "MYCONTENT" -- name for folder in Downloads
    property basePath : ((downLoads as text) & baseName & ":")
    
    -- Set up folders in Downloads and Application Support as needed --
    do shell script "mkdir -p " & quoted form of (POSIX path of basePath) & space & quoted form of POSIX path of ((appSupport as text) & myApp & ":Resources")
    
    -- Download and progress - more error handling is needed --  
    do shell script "curl -L " & quoted form of (webPage & webResource) & " -o " & quoted form of POSIX path of (basePath & webResource) & " > " & quoted form of POSIX path of (basePath & "status") & " 2>&1 &"
    set fileSize to 0
    set curTransferred to 0
    
    set curProgress to 0
    repeat until curProgress = "100"
       try
          set lastLine to paragraph -1 of (do shell script "cat " & quoted form of POSIX path of (basePath & "status"))
          set curProgress to word 1 of lastLine
          set fileSize to word 2 of lastLine
          set curTransferred to word 4 of lastLine
          tell me
             display dialog "Downloading; Please wait, this will take a while.
                    Status: " & curTransferred & " of " & fileSize & " (" & curProgress & "%)" buttons {"Refresh", "Cancel"} giving up after 1
             if the button returned of the result is "Cancel" then return
          end tell
       on error
          display dialog "Download failed. To restart the download, please press the 'Retry' button" buttons {"Quit", "Retry"} with icon 0
          if button returned of the result is "Quit" then error number -128
       end try
    end repeat
    display dialog "Download is complete. Press [OK] to continue"
    
    -- Extract to the folder created in Downloads --
    do shell script "unzip -u " & quoted form of POSIX path of (basePath & webResource) & " -d " & quoted form of POSIX path of basePath -- Main Folder > Sub Folder > text file
    
    -- Copy items to the folder created in Application Support --
    tell application "Finder"
       set downLoadFolder to folder basePath
       set DestinationFolder to folder ((appSupport as text) & myApp & ":Resources")
       duplicate (entire contents of first folder of downLoadFolder) to DestinationFolder with replacing -- contents of "Main Folder" -- 'duplicate' is the command to use for files
       display dialog "All content has been copied. Thank you!"
    end tell
    

    根据需要更改属性中的占位符项-请注意,shell脚本和web文件名区分大小写。

    您只是想直接解决您的问题,还是想修改代码以使脚本更干净?@Ted Wrigley;刚开始编码并喜欢它一点……因此非常感谢您的帮助。straight sol注意/修订/清理,并对正在执行的操作及其原因进行一些解释……以便我可以从中学习:)感谢您的指导和清理,您的示例中编写代码的方式非常容易理解代码的具体操作。我只是想知道这一行:属性appSupport:从用户域到应用程序支持的路径。。。这是不是告诉代码在任何系统上运行,而不管下载/AppSupport文件夹的路径如何?我已经读到我们将在路径中使用$HOME来告诉代码在UsersHome文件夹中查找该路径。
    $HOME
    是shell脚本中有用的常量之一。在AppleScript中,还有标准添加脚本中的
    path to
    命令,该命令提供到各种macOS系统位置的路径
    path to Downloads folder
    是当前用户的下载文件夹,
    path to application support from user domain
    是用户的应用程序支持文件夹(该文件夹有系统域、本地域和用户域)。混合使用Finder和shell脚本命令时,我发现从AppleScript转换更容易,所以我倾向于使用这些命令;明白了。。。似乎AppleScripting相当接近人类的行话;也遵循基本的逻辑结构。非常感谢您花时间解释和解决我的问题-今晚晚些时候我将尝试您建议的编辑,并向您更新我的发现。再次感谢您的努力和帮助;所以我按照你的建议工作&做了很多改变;感谢你布置基地的方式;我对代码所做的一切都起了作用。然而;一件小事;在你的建议中;您声明了这一行:--提取到下载中创建的文件夹--提取ZIP需要一段时间,代码跳转到下一个“复制”命令;意思是当解压发生时;拷贝开始;所以不是所有的东西都被复制。。。有没有办法让代码等待解压完成?最好是过程检查;如果不是计时器?除非将其设置为在后台运行(就像您使用
    curl
    命令所做的那样),
    do shell脚本
    在命令完成之前不会返回。您可以在解压后添加一个短暂的延迟,以确保一切都已解决。