Automation 用于更改文件名的Applescript

Automation 用于更改文件名的Applescript,automation,applescript,filenames,rename,finder,Automation,Applescript,Filenames,Rename,Finder,我想用applescript更改数千个文件名 文件名都是这样生成的: 第一部分-第二部分XX.xxx XX是一个特定的数字,而.xxx是一个jpg或png扩展名 我想简单地改变周围的部分,使其成为: 第二部分-第一部分XX.xxx 我已经想出了这个办法,但我的编码技能让我失望 tell application "Finder" to set aList to every file in folder "ImageRename" set text item delimiters to {" - "

我想用applescript更改数千个文件名

文件名都是这样生成的:

第一部分-第二部分XX.xxx

XX是一个特定的数字,而.xxx是一个jpg或png扩展名

我想简单地改变周围的部分,使其成为:

第二部分-第一部分XX.xxx

我已经想出了这个办法,但我的编码技能让我失望

tell application "Finder" to set aList to every file in folder "ImageRename"
set text item delimiters to {" - ", "."}
repeat with i from 1 to number of items in aList
    set aFile to (item i of aList)
    try
        set fileName to name of aFile
        set firstPart to text item 1 of fileName
        set secondPart to text item 2 of fileName
        set thirdPart to text item 3 of fileName
        set newName to secondPart & " - " & firstPart & "." & thirdPart
        set name of aFile to newName
    end try
end repeat
这只适用于第二部分的数字。 因此,它变成:

第二部分XX-第一部分xxx

如何将两个整数作为文本项分隔符


请帮帮我,一路上教我:-

只需使用空格作为分隔符并构建部件。 已编辑:在文本部分中允许空格

tell application "Finder" to set aList to every file in folder "ImageRename"
set AppleScript's text item delimiters to " "
repeat with i from 1 to number of items in aList
    set aFile to (item i of aList)
    try
        set fileName to name of aFile
        set lastParts to text item -1 of fileName
        set wordParts to (text items 1 thru -2 of fileName) as string
        set AppleScript's text item delimiters to " - "
        set newName to {text item 2 of wordParts, "-", text item 1 of wordParts, lastParts}
        set AppleScript's text item delimiters to " "
        set name of aFile to (newName as string)
    end try
end repeat
set AppleScript's text item delimiters to ""

只需使用空格作为分隔符并构建部件。 已编辑:在文本部分中允许空格

tell application "Finder" to set aList to every file in folder "ImageRename"
set AppleScript's text item delimiters to " "
repeat with i from 1 to number of items in aList
    set aFile to (item i of aList)
    try
        set fileName to name of aFile
        set lastParts to text item -1 of fileName
        set wordParts to (text items 1 thru -2 of fileName) as string
        set AppleScript's text item delimiters to " - "
        set newName to {text item 2 of wordParts, "-", text item 1 of wordParts, lastParts}
        set AppleScript's text item delimiters to " "
        set name of aFile to (newName as string)
    end try
end repeat
set AppleScript's text item delimiters to ""

要回答您的问题,如何使用整数作为文本项分隔符,请执行以下操作:

set AppleScript's text item delimiters to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
您可以一次设置多个文本项分隔符,但问题是,当使用多个文本项分隔符时,您实际上不知道两个文本项之间是什么。此外,使用文本项分隔符时,分隔符在文本中的显示顺序并不重要。因此,我建议使用正则表达式,定义某种格式,而不是分隔字符串并猜测哪个字符实际上是分隔符

tell application "Finder" to set aList to every file in folder "ImageRename"
repeat with i from 1 to number of items in aList
   set aFile to item i of aList
   set fileName to name of aFile as string
   set newName to do shell script "perl -pe 's/^(.*) - (.*) ([0-9]{2}\\.(jpeg|png))$/\\2 - \\1 \\3/i' <<<" & quoted form of fileName
   if newName is not fileName then set name of aFile to newName
end repeat
第一组将匹配第一个零件,第二组将匹配第二个零件,第三组XX.xxx将匹配剩余零件。我们只需要在返回新字符串时对它们重新排序。新字符串中后跟数字的反斜杠将被匹配组替换。在替换命令中,这将被标记为/s/search/\2-\1\3/flags

替换的最后一部分是一些标志,我将I作为不区分大小写匹配的标志

把这些放在一起让我

s/^(.*) - (.*) ([0-9]{2}\.(jpeg|png))$/\2 - \1 \3/I

注意:由于\在applescript中是一个特殊字符,因此我们必须将\写为\\

来回答您的问题。如何使用整数作为文本项分隔符,如下所示:

set AppleScript's text item delimiters to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
您可以一次设置多个文本项分隔符,但问题是,当使用多个文本项分隔符时,您实际上不知道两个文本项之间是什么。此外,使用文本项分隔符时,分隔符在文本中的显示顺序并不重要。因此,我建议使用正则表达式,定义某种格式,而不是分隔字符串并猜测哪个字符实际上是分隔符

tell application "Finder" to set aList to every file in folder "ImageRename"
repeat with i from 1 to number of items in aList
   set aFile to item i of aList
   set fileName to name of aFile as string
   set newName to do shell script "perl -pe 's/^(.*) - (.*) ([0-9]{2}\\.(jpeg|png))$/\\2 - \\1 \\3/i' <<<" & quoted form of fileName
   if newName is not fileName then set name of aFile to newName
end repeat
第一组将匹配第一个零件,第二组将匹配第二个零件,第三组XX.xxx将匹配剩余零件。我们只需要在返回新字符串时对它们重新排序。新字符串中后跟数字的反斜杠将被匹配组替换。在替换命令中,这将被标记为/s/search/\2-\1\3/flags

替换的最后一部分是一些标志,我将I作为不区分大小写匹配的标志

把这些放在一起让我

s/^(.*) - (.*) ([0-9]{2}\.(jpeg|png))$/\2 - \1 \3/I


注意:因为\在applescript中是一个特殊字符,所以我们必须写一个\作为\\

Hehe,是的,我试过这个。。。结果名称的某些部分也是空格…所以只需使用空格作为分隔符,将XX.xxx与前面的分隔开来,然后使用-将这两个部分分开并切换。小菜一碟。我编辑代码来演示。如果你想要香草的话,不需要使用grep来做这件事。是的,这就是我想要尝试的,在循环本身中添加分隔符。谢谢它的工作原理:-松脆的GREP和香草都是令人愉快的口味-呵呵,是的,我试过这个。。。结果名称的某些部分也是空格…所以只需使用空格作为分隔符,将XX.xxx与前面的分隔开来,然后使用-将这两个部分分开并切换。小菜一碟。我编辑代码来演示。如果你想要香草的话,不需要使用grep来做这件事。是的,这就是我想要尝试的,在循环本身中添加分隔符。谢谢它的工作原理:-松脆的GREP和香草都是令人愉快的口味-我希望它能更好地解释为什么在这种情况下我选择正则表达式而不是使用文本项分隔符。事实上,默认情况下Applescript的文本比较和处理程序不区分大小写。否则,您必须打开区分大小写的功能,方法是使用“考虑大小写”包装代码。。。终止considering@ShooTerKo:该命令在Bash中执行,而不是在AppleScript中执行。这意味着AppleScript的案例考虑与在命令行实用程序中进行的字符串比较无关。请重试,昨天编辑时似乎出现了问题。几乎正确:。。。它应该是没有括号的[a-z]{2,4}。整个表达式应该是^.*-.[0-9]{2}\.[a-z]{2,4}$我希望它能更好地解释我为什么选择
在这种情况下,使用文本项定界符来替换正则表达式。事实上,默认情况下,Applescript的文本比较和处理程序不区分大小写。否则,您必须打开区分大小写的功能,方法是使用“考虑大小写”包装代码。。。终止considering@ShooTerKo:该命令在Bash中执行,而不是在AppleScript中执行。这意味着AppleScript的案例考虑与在命令行实用程序中进行的字符串比较无关。请重试,昨天编辑时似乎出现了问题。几乎正确:。。。它应该是没有括号的[a-z]{2,4}。整个表达式应该是^.*-.[0-9]{2}\[a-z]{2,4}$