我想运行一个脚本,通过获取所选文件夹的目录文件夹的名称和所选文件夹的第二个单词,并在文件名的末尾添加序列号,来重命名所选文件夹中的文件。
这是我到目前为止所拥有的,但返回的只是{}。请帮助我解决此问题。
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开始的编号后缀加上文件扩展名)
注意事项:请在文件的[[副本,而不是原始文件上测试此脚本。批量重命名文件是一种PITA,如果您输入有误,则可以撤消该操作;将错误抛出来并从原始集中复制一个新副本要容易得多。
此脚本应该执行您想要的(我认为)。它使用所选文件夹名称的第二个单词,即其父文件夹的名称,并将它们与索引结合在一起以重命名该文件夹中的文件。在第5行中,确保在重命名文件之前按名称对文件进行排序,以保留文件顺序。让我知道它是如何工作的。set file_index to 0
tell application "Finder"
set selected_folder to item 1 of (get selection)
set selected_folder_path to POSIX path of (selected_folder as alias)
set files_to_rename to (sort files of selected_folder by name) as alias list
end tell
tell application "System Events"
set partition_directory to name of container of folder selected_folder_path
set section_name to name of folder selected_folder_path
set iss_number to word 2 of section_name
repeat with a_file in files_to_rename
set new_file_name to partition_directory & " " & iss_number & "-" & my zeroPad(file_index) & ".jpg"
set name of a_file to new_file_name
set file_index to file_index + 1
end repeat
end tell
on zeroPad(a_num)
return text -4 through -1 of ("0000" & a_num as string)
end zeroPad
P.s。我已经用一些更有效的方法重做了零填充程序。在可能的情况下,我还将内容切换到System Events.app。 Finder倾向于使事物变粘。