【发布时间】:2018-11-28 11:57:28
【问题描述】:
我有一个用于根据数字电子表格提供的名称重命名文件的小程序。
每张图片都有三种不同的尺寸,每种尺寸都放在一个文件夹中。所以有
/50/exportedImage-01.svg
/33/exportedImage-01.svg
/25/exportedImage-01.svg
该脚本运行良好,并且执行了它应该做的事情,但是它非常缓慢并且在 50 多个文件处停止。因为我有几百个文件要重命名,所以问题是:
有没有办法改变脚本以使其更高效/更强大?
工作流程如下:
- 读取数字文件 - B 列中的新名称 (featuredImage),C 列中的旧名称 (exportedImage)
- 遍历第一个文件夹中的文件并根据 B 列重命名它们
- 遍历第二个文件夹中的文件并根据 B 列重命名它们
- 遍历第三个文件夹中的文件并根据 B 列重命名它们
这是我目前得到的:
set numbersFile to choose file with prompt "Choose the Numbers file" of type {"numbers", "XLS7", "XLS8", "XLSX"}
set theFolder to choose folder with prompt "Choose the folder 50 containing the files to rename"
--get a list of the old and the new filenames
set exportedImage to {}
set featuredImage to {}
tell application "Numbers"
open numbersFile
tell table 1 of sheet 1 of document 1
--tell document 1
--tell sheet 1
repeat with i from 1 to row count
if value of cell ("C" & i as text) > "" then
set exportedImage to exportedImage & value of cell ("C" & i as text)
set featuredImage to featuredImage & value of cell ("B" & i as text)
else
exit repeat
end if
end repeat
--end tell
end tell
close window 1
end tell
--loop through the files in folder 50 and rename them
tell application "Finder"
repeat with k from 1 to (count of (get every item of theFolder))
repeat with i from 1 to count of exportedImage
if (name of item k of theFolder) as text = (item i of exportedImage) as text then
set name of (item k of theFolder) to (item i of featuredImage as text)
end if
end repeat
end repeat
end tell
--loop through the files and rename them 33
set theFolder to choose folder with prompt "Choose the folder 33 containing the files to rename"
tell application "Finder"
repeat with k from 1 to (count of (get every item of theFolder))
repeat with i from 1 to count of exportedImage
if (name of item k of theFolder) as text = (item i of exportedImage) as text then
set name of (item k of theFolder) to (item i of featuredImage as text)
end if
end repeat
end repeat
end tell
--loop through the files and rename them 25
set theFolder to choose folder with prompt "Choose the folder 25 containing the files to rename"
tell application "Finder"
repeat with k from 1 to (count of (get every item of theFolder))
repeat with i from 1 to count of exportedImage
if (name of item k of theFolder) as text = (item i of exportedImage) as text then
set name of (item k of theFolder) to (item i of featuredImage as text)
end if
end repeat
end repeat
end tell
我想坚持使用 applescript,因为这个脚本是更大的自动化工作流程的一部分。
任何帮助表示赞赏,谢谢!
【问题讨论】:
-
我建议你使用'do shell script'指令来运行shell命令。对于 shell 命令,您可以使用“查找”来查找具有您要查找的名称的文件,并使用管道来重命名找到的文件。这样做,您应该在脚本顶部请求 3 个文件夹(50、33 和 25)。然后你只循环遍历你的行数,并在该循环内使用 do shell 脚本查找/重命名处理 3 个文件夹中的每一个。使用这些 shell 命令重命名 100 个文件只需几秒钟。
标签: applescript spreadsheet automator