【问题标题】:Automator/Applescript rename files ifAutomator/Applescript 重命名文件,如果
【发布时间】:2012-04-19 04:28:12
【问题描述】:

我有大量图片被我的艺术家错误命名。我希望通过使用 Automator 来避免给他更多的工作,但我是新手。现在它们按顺序命名为what001a 和what002a,但应该是what001a 和what001b。所以基本上奇数编号是A,偶数编号是B。所以我需要一个脚本,将偶数编号更改为B图像并将它们全部重新编号为正确的顺序编号。我将如何编写该脚本?

【问题讨论】:

  • 我是 Stack Overflow 上的热心用户和问题解答者。我不是想获得免费工作。我以前从未使用过 Applescript,也不知道从哪里开始。通常对于这种功能,我使用 Automator,但 automator 无法执行 if..then 语句。
  • 所有 02 都去 01b,所有 03 去 02b?
  • what001a 很好。 what002a 应该是 what001b。 what003a 应该是 what002a,what004a 应该是 what002b。
  • 如果what003a应该是what002a,那么原来的what002a会发生什么?
  • 我有一个要发布的答案,但我必须等待一小时才能发布。

标签: python applescript automator


【解决方案1】:

嵌入在 AppleScript 中的小型 Rub​​y 脚本提供了一个非常舒适的解决方案,允许您在 Finder 中选择要重命名的文件并显示信息性成功或错误消息。

算法重命名文件如下:

number = first 3 digits in filename                # e.g. "006"
letter = the letter following those digits         # e.g. "a"
if number is even, change letter to its successor  # e.g. "b"
number = (number + 1)/2                            # 5 or 6 => 3
replace number and letter in filename

这里是:

-- ask for files
set filesToRename to choose file with prompt "Select the files to rename" with multiple selections allowed

-- prepare ruby command
set ruby_script to "ruby -e \"s=ARGV[0]; m=s.match(/(\\d{3})(\\w)/); n=m[1].to_i; a=m[2]; a.succ! if n.even?; r=sprintf('%03d',(n+1)/2)+a; puts s.sub(/\\d{3}\\w/,r);\" "


tell application "Finder"

    -- process files, record errors
    set counter to 0
    set errors to {}
    repeat with f in filesToRename
        try
            do shell script ruby_script & (f's name as text)
            set f's name to result
            set counter to counter + 1
        on error
            copy (f's name as text) to the end of errors
        end try
    end repeat

    -- display report
    set msg to (counter as text) & " files renamed successfully!\n"
    if errors is not {} then
        set AppleScript's text item delimiters to "\n"
        set msg to msg & "The following files could NOT be renamed:\n" & (errors as text)
        set AppleScript's text item delimiters to ""
    end if
    display dialog msg
end tell

请注意,当文件名包含空格时,它失败。

【讨论】:

  • 这很棒。非常感谢。我已经用 Python 中的一个朋友的脚本回答了这个问题。但我已将其保存以供将来参考。再次感谢。
【解决方案2】:

我的一个朋友写了一个 Python 脚本来做我需要的事情。想我会把它贴在这里作为任何偶然发现类似问题寻求帮助的人的答案。虽然它是在 Python 中,所以如果有人想将它转换为 AppleScript 以供可能需要它的人使用。

import os
import re
import shutil

def toInt(str):
  try:
    return int(str)
  except:
    return 0

filePath = "./"
extension = "png"

dirList = os.listdir(filePath)

regx = re.compile("[0-9]+a")

for filename in dirList:
  ext = filename[-len(extension):]
  if(ext != extension): continue
  rslts = regx.search(filename)
  if(rslts == None): continue
  pieces = regx.split(filename)
  if(len(pieces) < 2): pieces.append("")
  filenumber = toInt(rslts.group(0).rstrip("a"))
  newFileNum = (filenumber + 1) / 2
  fileChar = "b"
  if(filenumber % 2): fileChar = "a"
  newFileName = "%s%03d%s%s" % (pieces[0], newFileNum, fileChar, pieces[1])
  shutil.move("%s%s" % (filePath, filename), "%s%s" % (filePath, newFileName))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多