AppleScript - 复制文件夹内容显示错误 - 下载并解压缩有效

AppleScript - Copying Folder Content shows error - Download & UnZip Works

UPDATE01: 清理代码 - Zip 下载已删除,当前问题是 'Copy' 命令在 'unzip' 完成之前执行;导致一个空文件夹的副本到目标文件夹。当 运行 作为 .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 脚本来执行以下操作:

  1. 从 'http://MyLink.com/XXX.zip'
  2. 下载 'XXX.ZIP'
  3. 在 'Downloads' 文件夹中下载并覆盖任何现有文件(如果已存在)
  4. 显示下载进度条(<- 我知道进度条很难,所以这是一个很好的选择
  5. 下载后,在相同的 'Downloads' 位置解压 ZIP

到目前为止一切正常;然而从这一点开始我就在挣扎

  1. 将 'All Contents' 的 MainFolder(不是主文件夹本身;只是其中的子文件夹和文本文件)从 'Downloads' 文件夹复制到 'Library/Application Support/MyApp/Resources' 并替换任何现有文件
  2. 复制后,显示'process is completed'-[确定]
  3. 的弹出对话框

备注:

PS - 我是编码新手;所以这段代码中的很多东西可能看起来 'senseless';但我正在努力所以请和我一起裸露。我浏览了很多论坛来获取我所拥有的东西,但是编码之神对我不满意……我在尝试完成所有这些工作时遇到了问题;这是我到目前为止的脚本:

set newFolderPath to quoted form of (expandPath("~/Downloads/MYCONTENT"))
set cmdStr to "if [[ ! -d " & newFolderPath & " ]]; then
    mkdir -m 755 " & newFolderPath & "; fi"
do shell script cmdStr
on expandPath(pPathStr)
    local fullPath
    set fullPath to pPathStr
    if fullPath = "~" then
        set fullPath to (POSIX path of (path to home folder))
    else if fullPath starts with "~/" then
        set fullPath to (POSIX path of (path to home folder)) & text 3 thru -1 of fullPath
    end if
    return fullPath
end expandPath
-- Download --  
tell application "Finder"
    do shell script "curl -L -o ~/Downloads/MYCONTENT/SOME_RESOURCES.ZIP 'https://MyWebsite.com/Stuff/DownloadableContent/SOME_RESOURCES.ZIP' > ~/Downloads/MYCONTENT/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 ~/Downloads/MYCONTENT/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 5
                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
        end try
    end repeat
    set theDialogText to "Download is complete. Press [OK] to continue"
    display dialog theDialogText
    -- Extract --
    do shell script "unzip -u  ~/Downloads/MYCONTENT/SOME_RESOURCES.ZIP -d ~/Downloads/MYCONTENT/"
    do shell script "/bin/sleep 10"
    -- ** FROM HERE ONWARDS I AM GETTING AN ERROR **
    -- Copy --
    set DownloadFolder to "~/Downloads/MYCONTENT/RESOURCES/"
    set DestinationFolder to "~/Library/Application Support/MYAPPLICATION/RESOURCES/"
    copy every file of folder (DownloadFolder's entire contents) to folder DestinationFolder
    set theDialogText to "All content has been copied. Thank you!"
    display dialog theDialogText
end tell

你的大纲和脚本示例有点不同,所以我选择了大纲:

  • 根据需要在用户的下载Application Support文件夹中创建文件夹
  • 将 zip 文件下载到在 下载 中创建的文件夹,并将其解压缩到该文件夹​​ - zip 文件包含一个主文件夹,该主文件夹包含一个(或多个)包含文件的子文件夹
  • 将主文件夹的全部内容复制到在 Application Support 中为应用程序创建的文件夹中的 Resources 文件夹

有几种方法可以使用一些 AppleScriptObjC 来制作进度条或下载状态,但为了更好地控制这些可能应该从应用程序中完成。

您的脚本的主要问题是 Finder 不理解 POSIX 路径,并且您错过了在 Application Support 中创建文件夹结构。有标准命令可以获取各种系统文件夹的路径,因此不需要字符串操作就可以让脚本在其他机器上运行。在下面的脚本中,我跟踪了常规的 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 文件名区分大小写。