Applescript重复重命名

Applescript重复重命名,applescript,file-rename,batch-rename,Applescript,File Rename,Batch Rename,我正试图制作一个AppleScript droplet来重命名一堆格式烦人的图像,但我发现我的AppleScript技能已经不存在了,我一事无成。因此,如果可能的话,完整的代码,而不仅仅是片段 文件设置总是一样的,但是有很多变化(例如:Yellowst.Nat.Park.D12P55.DMS.3248.jpg) 它以地名开头,应该是一组不同字符串的查找和替换(“Yellowst.Nat.Park”->“黄石国家公园”) 然后,后面是两个应更改格式的数字(D12P55->[12x55])。它们总

我正试图制作一个AppleScript droplet来重命名一堆格式烦人的图像,但我发现我的AppleScript技能已经不存在了,我一事无成。因此,如果可能的话,完整的代码,而不仅仅是片段

文件设置总是一样的,但是有很多变化(例如:Yellowst.Nat.Park.D12P55.DMS.3248.jpg)

  • 它以地名开头,应该是一组不同字符串的查找和替换(“Yellowst.Nat.Park”->“黄石国家公园”)
  • 然后,后面是两个应更改格式的数字(D12P55->[12x55])。它们总是被设置在一个“D”,后面跟着两个数字,一个“P”,再后面是两个数字
  • 它以一个随机字符串结尾,可以是数字、字母等,所有这些都必须去。它们的格式和长度不同,没有图案
基本上,我想从“Yellowst.Nat.Park.D12P55.DMS.3248.jpg”转到“黄石国家公园[02x03].jpg”。我想在后面添加文本,所以希望以空格结尾


在我看来,最好的方法似乎是重复查找和替换。第一部分,列出一系列术语,这些术语必须由一系列相应的术语替换。然后检测数字格式,最后删除后面的随机字符串。

我喜欢像Sam这样的小挑战。它们对我来说很有趣。。。也许我病了。无论如何,我编写了一个处理程序,按照您的要求清理文件名。如果您熟悉文本项分隔符之类的东西,那么在applescript中操纵文本并不难。这些小挑战让我的文字技能保持敏锐

注意:在名称列表属性中,名称必须以句点或任何字符结尾,如您所述,在数字序列DxxPxx中字母D的前面

试试看。插入各种文件名,并确保其按您所需的方式工作。当然,您还需要在nameList和nameReplaceList属性中添加更多值

property nameList : {"Yellowst.Nat.Park."}
property nameReplaceList : {"Yellowstone National Park"}

set fileName to "Yellowst.Nat.Park.D12P55.DMS.3248.jpg"
cleanFilename(fileName)


(*================ SUBROUTINES ================*)
on cleanFilename(fileName)
    -- first find the base name and file extension of the file name
    set tids to AppleScript's text item delimiters
    set ext to ""
    if fileName contains "." then
        set AppleScript's text item delimiters to "."
        set textItems to text items of fileName
        set ext to "." & item -1 of textItems
        set baseName to (items 1 thru -2 of textItems) as text
        set text item delimiters to ""
    else
        set baseName to fileName
    end if

    -- next find the pattern D, 2 numbers, P, and 2 numbers in the baseName
    set chars to characters of baseName
    set theSequence to missing value
    repeat with i from 1 to (count of chars) - 6
        set thisChar to item i of chars
        if thisChar is "d" and item (i + 3) of baseName is "p" then
            try
                set firstNum to text (i + 1) thru (i + 2) of baseName
                firstNum as number
                set secondNum to text (i + 4) thru (i + 5) of baseName
                secondNum as number
                set theSequence to text i through (i + 5) of baseName
                exit repeat
            end try
        end if
    end repeat

    -- now make the changes
    if theSequence is not missing value then
        set AppleScript's text item delimiters to theSequence
        set theParts to text items of baseName
        set fixedFirstPart to item 1 of theParts
        repeat with i from 1 to count of nameList
            if item i of nameList is fixedFirstPart then
                set fixedFirstPart to item i of nameReplaceList
                exit repeat
            end if
        end repeat
        set fixedName to fixedFirstPart & " [" & firstNum & "x" & secondNum & "]" & ext
    else
        set fixedName to fileName
    end if

    set AppleScript's text item delimiters to tids
    return fixedName
end cleanFilename
现在,如果您想为一个充满文件的文件夹自动执行此操作,可以使用以下代码。用这个替换上面脚本的第3行和第4行。我没有检查这段代码,但它足够简单,应该按原样工作

注意:如果使用此代码选择的文件夹中有非图像文件,则无需担心,因为它们(我假设是这样)没有DxxPxx编号序列,因此此脚本不会以任何方式更改它们

set theFolder to choose folder
tell application "Finder"
    set theFiles to files of theFolder
    repeat with aFile in theFiles
        set thisName to name of aFile
        set newName to my cleanFilename(thisName)
        if newName is not thisName then
            set name of aFile to newName
        end if
    end repeat
end tell

这是另一种方法

property pictureFolder : (alias "Mac OS X:Users:Sam:Pictures:test:")
property findList : {"Yellowst.Nat.Park", "Jellyst.Nat.Park"}
property replaceList : {"Yellowstone National Park", "Jellystone \\& National Park"}

tell application "System Events"
set nameList to (name of every file of pictureFolder whose visible = true)
repeat with i from 1 to count of (list folder pictureFolder without invisibles)
    set fileName to item i of nameList
    set fileExtension to (name extension of (file fileName of pictureFolder))

    repeat with j from 1 to count of findList
        if fileName contains item j of findList then
            set tempName to do shell script "echo " & fileName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/[\\1x\\2] " & i & "." & fileExtension & "/'"
            set tempName to do shell script "echo " & tempName & " | sed 's/^" & item j of findList & "/" & item j of replaceList & " /'"
            set name of (file fileName of pictureFolder) to tempName
            exit repeat
        else if j = (count of findList) then
            set tempName to do shell script "echo " & fileName & " | sed 's/[.]/ /g'"
            set tempName to do shell script "echo " & tempName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/ [\\1x\\2] " & i & "." & fileExtension & "/'"
            set name of (file fileName of pictureFolder) to tempName
        end if
    end repeat
end repeat
end tell
为了避免名称重复,我在文件名的末尾添加了一个计数器。如果没有重复项,您可以改为使用:

set tempName to do shell script "echo " & fileName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/[\\1x\\2] " & "." & fileExtension & "/'"

你考虑过使用自动机吗?您应该能够使用内置的操作非常快速地将某些内容组合在一起。就个人而言,我可能会使用更通用的脚本语言,如Ruby或PHP,或者使用一些shell脚本。但如果做不到这一点,我自己可能会选择自动程序解决方案,而不是原始的AppleScript。坦白地说,我觉得做这样的工作太辛苦了。我已经做了。据我所知,Automator没有什么比简单的查找和替换更好的了,如果您使用Automator进行设置需要花费很长时间,并且在这种情况下毫无意义,那么我仍然必须手动更改数字序列。Automator适用于非常简单的东西,我还没有找到它的好用途,我经常尝试完成一些事情,但它缺乏Applescript所具有的功能。(我知道你可以将AppleScript放在Automator中,但这有点违背了重点。)谢谢,我实际上刚刚发现tompaman的脚本在第一部分中有一个以D开头的单词时会引起问题。这是因为并非所有图像都有需要完全替换的字符串,其中很大一部分只需要将句点替换为空格,不幸的是,此脚本跳过了句点。我可以将它们全部添加到列表中,但这在一定程度上挫败了试图用这个脚本赢得时间的目的。当脚本在已经更改的文件上运行时,它会在数字模式之前为那些被自己替换的文件(find和replace中的单词相同)增加一个额外的空间我更新了脚本,如果findList中没有匹配的术语,就用空格替换句点。我想继续清理我的图片文件夹,并意识到脚本做了一些我不知道为什么的事情。尽管字符串在findList中,它仍运行else部分。它运行了,谢谢。如果你不介意的话,有一个简单的问题,如果我想要一个符号,我应该在替换列表{}中填写什么?现在,使用常规的“与”符号会导致所需的“与”符号(XXXYYY->XXX&yy->XXX xxxyy-YYY)周围出现重复。Using\&通常与sed一起使用,但AppleScript不接受它(应为“”,但找到未知标记)