简体   繁体   English

创建用于OSX Automator的批处理文件重命名脚本

[英]Creating a batch file-renaming script for use in OSX Automator

Apologies in advance. 提前致歉。 I am a complete newbie when it comes to programming. 我是编程方面的新手。 I am hoping that someone out there might be able to help me. 我希望外面有人可以帮助我。

Based on the nature of my work, I have to constantly rename a bunch of images. 根据工作的性质,我必须不断重命名一堆图像。 The filenames sometimes vary, but are often some form of chronological numbers displayed in sequential order in their own separate directory (think image001.jpg, etc. but not always that naming format). 文件名有时会有所不同,但通常是某种顺序的时间顺序编号,它们以顺序的顺序显示在它们自己的单独目录中(请考虑image001.jpg等,但并不总是这种命名格式)。 Anyway, I have to rename all of the files in this particular directory to "000-001.jpg", "002-003.jpg", "004-005.jpg", etc. for every jpg file existing in that single directory. 无论如何,对于该单个目录中存在的每个jpg文件,我都必须将该特定目录中的所有文件重命名为“ 000-001.jpg”,“ 002-003.jpg”,“ 004-005.jpg”等。 。 There are usually between 10-999 images in a given directory, but I am rarely given some directories with 1000+ images (in which case I begin naming all files "0000-0001.jpg", "0002-0003.jpg", etc.). 给定目录中通常有10-999张图片,但是很少给我提供1000张以上图片的目录(在这种情况下,我开始将所有文件命名为“ 0000-0001.jpg”,“ 0002-0003.jpg”等)。

I have been manually renaming probably hundreds of thousands of these images over the course of the last several years. 在过去的几年中,我一直在手动重命名数十万张这样的图像。 Now I'm finally looking for a way to batch rename them so I don't have to waste hours doing so. 现在,我终于在寻找一种方法来批量重命名它们,这样我就不必浪费时间了。 It seems that Automator on OSX is the easiest way to go, but Automator cannot rename files in the way I need them on its own. 似乎OSX上的Automator是最简单的方法,但是Automator无法以我自己需要它们的方式来重命名文件。

I did notice that I can employ shell scripts or applescripts as part of an Automator function, so I was wondering if someone might be willing to write a short script for use in Automator (I work much better with a GUI) that would provide the above function? 我确实注意到我可以将Shell脚本或Applescript用作Automator函数的一部分,所以我想知道是否有人愿意编写一个简短的脚本以在Automator中使用(我在GUI上工作得更好)可以提供上述功能。功能? Unfortunately, I have no experience scripting. 不幸的是,我没有脚本编写经验。 The absolute best ideal outcome would be for me to use that script within an Automator Application and just drag the files I need renamed onto it. 对于我来说,绝对最佳的理想结果是在Automator应用程序中使用该脚本,然后将需要重命名的文件拖到该脚本上。 This would bypass my need to open the OSX Terminal, with which I also have zero experience. 这将绕开我打开OSX终端的需要,而我对OSX终端也没有任何经验。

As an added note, I am using an older laptop running OSX 10.6.8. 作为补充,我使用的是运行OSX 10.6.8的旧笔记本电脑。 I might not have the most updated versions of Applescript, etc. to work from. 我可能没有Applescript等的最新版本。 I would be greatly appreciative if anyone were willing to help me out. 如果有人愿意帮助我,我将不胜感激。 Thanks! 谢谢!

-- JE -JE

I would do it like this in bash shell because I find Applescript very verbose! 我会在bash shell中这样做,因为我发现Applescript非常冗长! The script writes all error messages in a file on your Desktop called "renamerLog.txt". 该脚本将所有错误消息写入桌面上名为“ renamerLog.txt”的文件中。

You must drop a single folder onto the icon. 您必须将一个文件夹拖放到图标上。

It copies the files from the specified directory, renaming them as you asked on the way, to a directory (folder) on your Desktop called "Output" which MUST not exist before you start - else it would mix up pages from different books. 它将文件从指定目录复制,并按您的要求将其重命名到桌面上名为“输出”的目录(文件夹),该目录在开始之前一定不存在-否则会混淆不同书籍的页面。

It looks like this in Automator : Automator看起来像这样:

在此处输入图片说明

You can copy and paste the actual script from below: 您可以从下面复制并粘贴实际脚本:

# Place where we will save messages
LOG="$HOME/Desktop/renamerLog.txt"

# Clear log from any previous run
> "$LOG"

# Set up name for output directory
OUT="$HOME/Desktop/Output"

# Check user dropped a directory, rather than files
if [ ! -d "$1" ]; then
   echo "ERROR: Please drop a DIRECTORY onto me!" >> "$LOG"
   exit 1
fi
echo "Processing files in: $1" >> "$LOG"

# Go to that directory
cd "$1"
if [ $? -ne 0 ]; then
   echo "ERROR: Unable to go to specified directory" >> "$LOG"
   exit 1
fi

# Create output directory
mkdir "$OUT"
if [ $? -ne 0 ]; then
   echo "ERROR: Unable to create output directory" >> "$LOG"
   exit 1
fi

# Don't barf if no files or upper or lower case
shopt -s nullglob
shopt -s nocaseglob

# Count files to determine number of leading zeroes required
for f in *.jpg; do
   ((nfiles++))
done

# Tell user how many files we found and set up output formattiing
echo "Files: $nfiles" >> "$LOG"
format="$OUT/%03d-%03d.jpg"
[ $nfiles -gt 999 ] && format="$OUT/%04d-%04d.jpg"      # Maybe this should be "-gt 499" rather than "-gt 999"

# Now copy the files renaming them as we go
i=0
j=1
for f in *.jpg; do
   newname=$(printf $format $i $j)
   ((i+=2))
   ((j+=2))
   echo Copy $f to $newname >> "$LOG"
   # cp "$f" "$newname"
done

You can try running it a few times and looking at the log to see if you like what it says, then, if you are happy with it, you can remove the # from the penultimate line so that it actually copies the files. 您可以尝试几次运行它,然后查看日志以查看是否喜欢它所说的内容,然后,如果对它满意,则可以从倒数第二行中删除# ,以便实际上复制文件。

I prefer to do it by copying rather than renaming because I don't want to risk losing your data. 我宁愿通过复制而不是重命名来完成此操作,因为我不想冒丢失数据的风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM