0

我正在尝试制作一个 AppleScript 小滴来重命名一堆格式令人讨厌的图像,但我发现我的 AppleScript 技能已经不存在了,而且我无处可去。所以如果可能的话,完整的代码,而不仅仅是片段。

文件设置始终相同,但有很多变化(例如:Yellowst.Nat.Park.D12P55.DMS.3248.jpg)

  • 它以地名开头,应该是一堆不同字符串的查找和替换,(“Yellowst.Nat.Park”->“Yellowstone National Park”)
  • 然后是两个应该在格式上更改的数字(D12P55 -> [12x55])。它们总是设置在“D”后跟两个数字、一个“P”和两个数字。
  • 它以随机字符串结尾,可以是数字、字母等,都必须去。它们的格式和长度不同,没有图案。

基本上我想从“Yellowst.Nat.Park.D12P55.DMS.3248.jpg”到“Yellowstone National Park [02x03] .jpg”我想在之后添加文本,所以想以空格结尾。

在我看来,最好的方法是重复查找和替换第一部分,为一堆术语列出一个列表,这些术语必须被一堆相应的术语替换。随后检测数字格式并以删除其后的随机字符串结束。

4

2 回答 2

1

这是另一种方法。

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 & "/'"
于 2012-02-15T19:05:48.470 回答
0

我喜欢像 Sam 这样的小挑战。他们对我来说很有趣……也许我病了;)。无论如何,我给你写了一个处理程序来按照你的要求清理文件名。如果您对文本项目分隔符等感到满意,那么在 applescript 中操作文本并不难。这些小挑战使我的文字技巧保持敏锐。

注意:在 nameList 属性中,名称必须以句点或您提到的数字序列 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
于 2012-02-15T17:22:23.177 回答