【发布时间】:2020-01-26 10:06:38
【问题描述】:
我想运行一个脚本来重命名所选文件夹中的文件,方法是获取所选文件夹的目录文件夹的名称和所选文件夹中的第二个单词,并在文件末尾添加一个序列号名字。
这是我目前所拥有的,但返回的只是 {}。请帮我解决这个问题。
property file_count : -1
set prefix to pad(file_count as string)
tell application "Finder"
set theSel to item 1 of (get selection)
set parDir to name of container of theSel
set theName to name of theSel
set issNum to word 2 of theName
end tell
set file_count to file_count + 1
on pad(s)
repeat while length of s < 4
set s to ("0" & s)
end repeat
end pad
if file_count < 10 then
set file_count to ("00" & file_count)
else if file_count < 100 then
set file_count to ("0" & file_count)
end if
tell application "Finder"
set theName to parDir & " " & issNum & "-" & file_count
set theSubs to every file in theSel
repeat with I from 1 to count of theSubs
set name of every file of item I of theSubs to theName & ".jpg"
end repeat
end tell
theName 结果为“(目录文件名)001-000”
提前谢谢你 l0g0p7
编辑:
根据要求提供一些文件命名前后的示例
之前:08-GenerationX-1.jpg 之后:X世代-001-000.jpg
(结果来自所选文件夹的父目录为“Generation X”,所选文件夹中的第二个单词为“-001”,然后是从000开始的编号顺序后缀加上文件扩展名)
【问题讨论】:
-
文件顺序对于索引是否重要?换句话说,文件夹的内容只是图像的任意集合,还是它们具有特定文件必须是“001”而下一个文件必须是“002”的序列?如果是后者,我如何确定顺序?
-
是的,确实如此。但无论如何,这些文件通常都是正确的顺序,或者已经有一个前缀数字序列。但无论如何我都想更改它们,以便它们都是相同的归档模式等
-
正如目前所写的那样,仅根据“theName 结果是“(目录文件名)001-000””来确定命名约定有点困难。如果您将edit 您的问题添加几个文件名称前后的示例,这可能会更有帮助。请查看How do I ask a good question? 和How to create a Minimal, Reproducible Example。
-
我问的原因是 system 看到的文件顺序不一定是你期望它看到的。我认为 Finder 将按照 Finder 窗口中设置的任何顺序迭代文件,并且 System Events.app(这是一个更好的应用程序)总是从最旧到最新迭代。更明智的做法是在重命名文件之前确保文件内部的顺序正确。我已经制定了脚本的一个版本;我只是在发布之前等待此信息。
-
什么是确保文件在内部按正确顺序排列的最佳方法,以便此过程按照我的预期工作?
标签: directory applescript subdirectory file-rename selected