微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AppleScript-复制文件夹内容显示错误-下载和解压缩作品

如何解决AppleScript-复制文件夹内容显示错误-下载和解压缩作品

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脚本来执行以下操作:

  1. 从'http://MyLink.com/XXX.zip'下载'XXX.ZIP'
  2. 在“下载”文件夹中下载并覆盖任何现有文件(如果已存在)
  3. 显示下载进度条(
  4. 下载后,将ZIP文件解压缩到相同的“下载”位置

到目前为止,一切正常。但是从现在开始,我一直在努力

  1. 将MainFolder的“所有内容”(不是主文件夹本身;只是其中的子文件夹和文本文件)从“下载”文件夹复制到“库/应用程序支持/ MyApp /资源”并替换任何现有文件
  2. 复制后,显示“过程已完成”的弹出对话框-[确定]

注释:

  • 我正在使用〜/ Folder位置,因为此脚本可供任何人使用,因此我无法对完整路径进行硬编码,即:MacHD / Users / USERNAME / Downloads..etc

PS -我是编码新手;因此,这段代码中的许多内容似乎“毫无意义”;但我正在尝试,所以请裸身。我经历了很多论坛来获得我所拥有的东西,但是编码之神对我不满意……我在尝试使所有这些工作变得困难重重。这是我到目前为止所拥有的脚本:

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

解决方法

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

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

有几种方法可以使用某些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文件名区分大小写。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。