简体   繁体   English

AppleScript:如何删除文件然后在进度条上加1%?

[英]AppleScript: How to delete file and then add 1% to the progress bar?

So I have this code:所以我有这个代码:

set theImages to choose file with prompt "Please select some images to process:" of type {"public.image"} with multiple selections allowed

-- Update the initial progress information
set theImageCount to length of theImages
set progress total steps to theImageCount
set progress completed steps to 0
set progress description to "Processing..."
set progress additional description to "Preparing to process your images..."

repeat with a from 1 to length of theImages
    -- Update the progress detail
    set progress additional description to "Processing image " & a & "/" & theImageCount
    
    -- Increment the progress
    set progress completed steps to a
    
    -- Pause for demonstration purposes, so progress can be seen
    delay (random number from 0.01 to 0.7)
    
    repeat with b in theImages
        tell application "Finder"
            try
                delete b
            end try
        end tell
    end repeat
end repeat

-- Reset the progress information
set progress total steps to 0
set progress completed steps to 0
set progress description to ""
set progress additional description to ""

display notification "All images were processed succesfully!" with title "Image Processer" subtitle "Images processed succesfully!" sound name "Frog"

And it is supposed to delete a file then add 1% to the progress bar but instead it deletes them all first then the progress bar does it's thing.它应该删除一个文件,然后将 1% 添加到进度条,但它首先将它们全部删除,然后进度条就可以了。 How do I fix this?我该如何解决?

There are two fatal issues:有两个致命的问题:

  1. You are deleting all files inside the loop so at the end of the first iteration there is no file left to be processed.您正在删除循环内的所有文件,因此在第一次迭代结束时没有文件需要处理。

  2. Even if you deleted only one file per iteration you will run into an out-of-index error because you are processing the files in ascending order.即使每次迭代只删除一个文件,您也会遇到索引外错误,因为您正在按升序处理文件。 Imagine you have two files, after deleting the first one there is no item at index 2 anymore.想象一下,您有两个文件,删除第一个文件后,索引 2 处不再有项目。

The solution is to delete one file per iteration and to delete the files backwards.解决方案是每次迭代删除一个文件并向后删除文件。

set theImages to choose file with prompt "Please select some images to process:" of type {"public.image"} with multiple selections allowed

-- Update the initial progress information
set theImageCount to length of theImages
set progress total steps to theImageCount
set progress completed steps to 0
set progress description to "Processing..."
set progress additional description to "Preparing to process your images..."

set repeatCount to 1
repeat with a from theImageCount to 1 by -1 -- count backwards
    -- Update the progress detail
    set progress additional description to "Processing image " & repeatCount & "/" & theImageCount
    
    -- Increment the progress
    set progress completed steps to repeatCount
    
    -- Pause for demonstration purposes, so progress can be seen
    delay (random number from 0.01 to 0.7)
    tell application "Finder"
        delete item a of theImages
    end tell
    set repeatCount to repeatCount + 1
end repeat

-- Reset the progress information
set progress total steps to 0
set progress completed steps to 0
set progress description to ""
set progress additional description to ""

display notification "All images were processed succesfully!" with title "Image Processer" subtitle "Images processed succesfully!" sound name "Frog"

Side note: Strictly spoken you don't add 1%, you add (100/theImageCount)%旁注:严格来说,您不添加 1%,而是添加 (100/theImageCount)%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM