我喜欢像 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