如何使用目录文件夹和使用 Applescript 的选定文件夹重命名选定文件夹中的文件
How to rename files within selected folder using directory folder and selected folder using Applescript
我想要 运行 一个脚本,通过获取所选文件夹的目录文件夹的名称和所选文件夹中的第二个单词并添加序号来重命名所选文件夹中的文件到文件名的末尾。
这是我目前所拥有的,但所有返回的都是 {}。请帮我解决这个问题。
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。我用更有效的方法重做了你的零填充例程。我还尽可能将事情切换到系统 Events.app。 Finder 倾向于把事情搞砸。
我想要 运行 一个脚本,通过获取所选文件夹的目录文件夹的名称和所选文件夹中的第二个单词并添加序号来重命名所选文件夹中的文件到文件名的末尾。
这是我目前所拥有的,但所有返回的都是 {}。请帮我解决这个问题。
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。我用更有效的方法重做了你的零填充例程。我还尽可能将事情切换到系统 Events.app。 Finder 倾向于把事情搞砸。