删除部分文件名

时间:2013-10-13 19:26:15

标签: applescript rename

我想将丑陋的文件名(作为我的错误编程的副产品)更改为“Booklet xxx.pdf kopie.pdf”到“xxx.pdf”。 Automator将其更改为> “xxx.pdf”(文件名前面有一个非常烦人的空间......

这就是我能想到的......

    tell application "Finder"
    set allFiles to selection
    search of (name of every item of allFiles) for "Booklet "
try
    if file exists then delete "Booklet " of name
end try
    search of (name of every item of allFiles) for ".pdf kopie"
try
    if file exists then delete ".pdf kopie" of name
end try
    end tell

非常感谢!

1 个答案:

答案 0 :(得分:2)

您可以通过设置name属性重命名文件:

activate application "SystemUIServer" -- http://www.openradar.me/9406282
tell application "Finder"
    activate
    repeat with f in (get selection)
        if name of f ends with "kopie.pdf" then
            set name of f to text 9 thru -11 of (get name of f)
        end if
    end repeat
end tell

或者在shell中运行类似的东西:

cd directory; for f in *kopie.pdf; do f2=${f% *}; mv "$f" "${f2##* }"; done

${f% *}​ *的末尾删除最短的f模式,${f2##* }* ​的开头删除最长的f2模式