AppleScript:如何删除文件,然后将 1% 添加到进度条?
AppleScript: How to delete file and then add 1% to the progress bar?
所以我有这个代码:
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"
它应该删除一个文件,然后将 1% 添加到进度条,但它首先将它们全部删除,然后进度条才这样做。我该如何解决这个问题?
有两个致命问题:
您正在删除循环内的所有文件,因此在第一次迭代结束时没有文件要处理。
即使您每次迭代只删除一个文件,您也会 运行 进入 索引外 错误,因为您正在处理这些文件按升序排列。假设您有两个文件,删除第一个文件后,索引 2 处不再有项目。
解决方法是每次迭代删除一个文件,往后删除文件。
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"
旁注:严格来说你不添加 1%,你添加 (100/theImageCount)%
所以我有这个代码:
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"
它应该删除一个文件,然后将 1% 添加到进度条,但它首先将它们全部删除,然后进度条才这样做。我该如何解决这个问题?
有两个致命问题:
您正在删除循环内的所有文件,因此在第一次迭代结束时没有文件要处理。
即使您每次迭代只删除一个文件,您也会 运行 进入 索引外 错误,因为您正在处理这些文件按升序排列。假设您有两个文件,删除第一个文件后,索引 2 处不再有项目。
解决方法是每次迭代删除一个文件,往后删除文件。
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"
旁注:严格来说你不添加 1%,你添加 (100/theImageCount)%