Applescript 代码不适用于大批量文件
Applescript code doesn't not work on big batch of files
我编写的代码适用于包含 750 个文件的文件夹,但是当我将它用于包含 5000 个文件的文件夹时,它不起作用,代码是:
set filenamelist to choose file with prompt "Please select files name list to
process:" of type {"csv"}
set checkfolder to (choose folder with prompt "Choose the source folder") as text
set destinationfolder to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set listOfbarcods to {}
set listtttt to paragraphs of (read filenamelist as «class utf8»)
repeat with nextLine in listtttt
if length of nextLine is greater than 0 then
set nextLine1 to text 2 thru -2 of nextLine
copy nextLine1 & ".pdf" as text to the end of listOfbarcods
end if
end repeat
set noitem to "0"
with timeout of 3600 seconds
repeat with theItem in listOfbarcods
try
do shell script "mv " & quoted form of POSIX path of checkfolder & quoted form of theItem & space & quoted form of POSIX path of destinationfolder
end try
delay 0.1
set noitem to (noitem + 1)
end repeat
end timeout
display dialog noitem & " Files Copied" as text
源代码包含整个错误链。它之所以能够正常工作,是因为 AppleScript 语言能够在运行时修复许多错误。我看到的最大问题是,您不是检查文件是否已完全移动,而是硬编码给 shell 命令移动文件的时间量(delay 0.1).我试图修复代码。一些强制转换和括号是可选的,但最好让您更清楚一切是如何工作的。
set filenamelist to choose file with prompt "Please select files name list to
process:" of type {"csv"}
set listtttt to paragraphs of (read filenamelist as «class utf8»)
set checkfolder to (choose folder with prompt "Choose the source folder")
set destinationfolder to (choose folder with prompt "Choose your destination folder for the copied files.")
set listOfbarcods to {}
repeat with nextLine in listtttt
if length of nextLine is greater than 1 then -- EDITED
set nextLine1 to text 2 thru -2 of nextLine
copy ((contents of nextLine1) & ".pdf") to the end of listOfbarcods -- EDITED
end if
end repeat
set noitem to 0 -- EDITED
set destinationQuotedPath to quoted form of POSIX path of destinationfolder -- ADDED
repeat with theItem in listOfbarcods
-- append the item to the path, then quote full path at once
set itemQuotedPath to quoted form of ((POSIX path of checkfolder) & (contents of theItem)) -- ADDED
do shell script "mv " & itemQuotedPath & space & destinationQuotedPath -- EDITED
repeat 30 times -- ADDED the loop (maximum - about 3 seconds)
delay 0.1
try -- check if the item is moved completly
alias ((checkfolder as text) & (contents of theItem))
set noitem to noitem + 1 -- EDITED
exit repeat
end try
end repeat
end repeat
display dialog (noitem as text) & " Files Copied" -- EDITED
我编写的代码适用于包含 750 个文件的文件夹,但是当我将它用于包含 5000 个文件的文件夹时,它不起作用,代码是:
set filenamelist to choose file with prompt "Please select files name list to
process:" of type {"csv"}
set checkfolder to (choose folder with prompt "Choose the source folder") as text
set destinationfolder to (choose folder with prompt "Choose your destination folder for the copied files.") as alias
set listOfbarcods to {}
set listtttt to paragraphs of (read filenamelist as «class utf8»)
repeat with nextLine in listtttt
if length of nextLine is greater than 0 then
set nextLine1 to text 2 thru -2 of nextLine
copy nextLine1 & ".pdf" as text to the end of listOfbarcods
end if
end repeat
set noitem to "0"
with timeout of 3600 seconds
repeat with theItem in listOfbarcods
try
do shell script "mv " & quoted form of POSIX path of checkfolder & quoted form of theItem & space & quoted form of POSIX path of destinationfolder
end try
delay 0.1
set noitem to (noitem + 1)
end repeat
end timeout
display dialog noitem & " Files Copied" as text
源代码包含整个错误链。它之所以能够正常工作,是因为 AppleScript 语言能够在运行时修复许多错误。我看到的最大问题是,您不是检查文件是否已完全移动,而是硬编码给 shell 命令移动文件的时间量(delay 0.1).我试图修复代码。一些强制转换和括号是可选的,但最好让您更清楚一切是如何工作的。
set filenamelist to choose file with prompt "Please select files name list to
process:" of type {"csv"}
set listtttt to paragraphs of (read filenamelist as «class utf8»)
set checkfolder to (choose folder with prompt "Choose the source folder")
set destinationfolder to (choose folder with prompt "Choose your destination folder for the copied files.")
set listOfbarcods to {}
repeat with nextLine in listtttt
if length of nextLine is greater than 1 then -- EDITED
set nextLine1 to text 2 thru -2 of nextLine
copy ((contents of nextLine1) & ".pdf") to the end of listOfbarcods -- EDITED
end if
end repeat
set noitem to 0 -- EDITED
set destinationQuotedPath to quoted form of POSIX path of destinationfolder -- ADDED
repeat with theItem in listOfbarcods
-- append the item to the path, then quote full path at once
set itemQuotedPath to quoted form of ((POSIX path of checkfolder) & (contents of theItem)) -- ADDED
do shell script "mv " & itemQuotedPath & space & destinationQuotedPath -- EDITED
repeat 30 times -- ADDED the loop (maximum - about 3 seconds)
delay 0.1
try -- check if the item is moved completly
alias ((checkfolder as text) & (contents of theItem))
set noitem to noitem + 1 -- EDITED
exit repeat
end try
end repeat
end repeat
display dialog (noitem as text) & " Files Copied" -- EDITED