【问题标题】:Batch Renaming Files in AppleScript, Only Changes Names of Every Other File在 AppleScript 中批量重命名文件,仅更改每个其他文件的名称
【发布时间】:2013-09-23 11:36:00
【问题描述】:

我正好有 11 个文件。这些被命名为 1.jpg、2.jpg 到 11.jpg。我想出了以下一半的作品。

但问题是只有一半的文件被处理。具体来说,仅处理奇数文件,即跳过每个第二个文件。有什么想法吗?

我注意到这个人在重命名文件夹时遇到了类似的问题,但是我没有足够的知识将它应用到我的场景中。 Batch Renaming Folders in AppleScript, Will Only Change Names of Every Other Folder

display dialog "Person ID number. (numerals only)?" default answer ""
tell application "Finder"
set personid to text returned of result
set home_path to home as text
set file1name to "_front"
set file2name to "_prof"
set file3name to "_neck"
set file4name to "_no_phon"
set file5name to "_phon"
set file6name to "_max"
set file7name to "_prof_max"
set file8name to "_relax"
set file9name to "_prof_relax"
set file10name to "_depress_no_phon"
set file11name to "_depress_phon"

tell folder (home_path & "Downloads:sagicphotos")
    set name of file 1 to "PRN" & personid & file1name & "." & name extension of file 1
    set name of file 2 to "PRN" & personid & file2name & "." & name extension of file 2
    set name of file 3 to "PRN" & personid & file3name & "." & name extension of file 3
    set name of file 4 to "PRN" & personid & file4name & "." & name extension of file 4
    set name of file 5 to "PRN" & personid & file5name & "." & name extension of file 5
    set name of file 6 to "PRN" & personid & file6name & "." & name extension of file 6
    set name of file 7 to "PRN" & personid & file7name & "." & name extension of file 7
    set name of file 8 to "PRN" & personid & file8name & "." & name extension of file 8
    set name of file 9 to "PRN" & personid & file9name & "." & name extension of file 9
    set name of file 10 to "PRN" & personid & file10name & "." & name extension of file 10
    set name of file 11 to "PRN" & personid & file11name & "." & name extension of file 11
end tell
end tell

【问题讨论】:

    标签: applescript


    【解决方案1】:

    对 user:495470 的回答稍作改动。

    正如我在 cmets 中指出的那样。如果您不考虑返回的文件列表的排序顺序和文件名列表的顺序,则文件名”和文件的顺序(即 1、2、3、4...)将不匹配。

    set theHomeF to ((path to home folder) & "Desktop:test:test1" as text) as alias
    set personid to "1234"
    set filenames to paragraphs of "_front
    _prof
    _neck
    _no_phon
    _phon
    _max
    _prof_max
    _relax
    _prof_relax
    _depress_no_phon
    _depress_phon"
    
    tell application "Finder"
        set file_namesList to ""
        set AppleScript's text item delimiters to {"
    "}
        set thefiles to files of theHomeF
    
        set theNames to (name of files of theHomeF)
    
        set file_namesList to text items of theNames as string
        set AppleScript's text item delimiters to {""}
        set file_namesList to paragraphs of (do shell script " echo  " & quoted form of file_namesList & "|sort -n")
        repeat with i from 1 to number of items in file_namesList
            set this_item to item i of file_namesList
            set thisFile to (some file of theHomeF whose name is this_item)
            set name of thisFile to "PRN" & personid & item i of filenames & "." & name extension of thisFile
        end repeat
    end tell
    

    注意将 AppleScript 的文本项分隔符设置为 {" "} 有一个 \n (引号之间的新行)

    我确信有更好的方法可以做到这一点,但正如他们所说的时间就是金钱......

    【讨论】:

      【解决方案2】:

      重命名文件时,它们的顺序会发生变化,因此某些文件永远不会被重命名。只需在重命名文件之前获取文件列表:

      set personid to "1234"
      
      set filenames to paragraphs of "_front
      _prof
      _neck
      _no_phon
      _phon
      _max
      _prof_max
      _relax
      _prof_relax
      _depress_no_phon
      _depress_phon"
      
      set filelist to paragraphs of (do shell script "cd ~/Downloads/sagicphotos; ls | sort -n")
      tell application "Finder"
          set i to 1
          repeat with f in filelist
              tell file ((home as text) & "Downloads:sagicphotos:" & f)
                  set name to "PRN" & personid & item i of filenames & "." & name extension
              end tell
              set i to i + 1
          end repeat
      end tell
      

      或者使用 shell 脚本:

      personid=1234
      
      filenames=(_front
      _prof
      _neck
      _no_phon
      _phon
      _max
      _prof_max
      _relax
      _prof_relax
      _depress_no_phon
      _depress_phon)
      
      cd ~/Downloads/sagicphotos
      for f in $(ls | sort -n); do
        mv $f PRN$personid${filenames[i++]}.${f##*.}
      done
      

      【讨论】:

      • 我看到的一个问题是“文件名”的顺序和文件的顺序,即 1,2,3,4... 将不匹配。例如,文件 10 的名称为 _prof。
      • 非常好,求救。我总是忘记那个小宝石。知道有比我的纠结更好的方法。
      猜你喜欢
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      相关资源
      最近更新 更多