【问题标题】:How to rename files within selected folder using directory folder and selected folder using Applescript如何使用目录文件夹重命名所选文件夹中的文件,并使用 Applescript 重命名所选文件夹
【发布时间】: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


【解决方案1】:

注意事项:请在您的文件的副本上测试此脚本,而不是在原件上。如果您犯了错误,批量重命名文件是一个 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

附:我已经用更有效的方法重做了你的零填充例程。我还尽可能将事情切换到 System Events.app。 Finder 有把事情搞砸的倾向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2012-08-10
    • 2020-05-17
    • 2013-04-24
    相关资源
    最近更新 更多