【问题标题】:Use Automator/Applescript to crop filenames after certain character?使用 Automator/Applescript 在某些字符后裁剪文件名?
【发布时间】:2012-04-12 01:06:02
【问题描述】:

我有一个包含大约 5000 个文件的文件夹,其名称如下:

    Invoice 10.1 (2012) (Digital) (4-Attachments).pdf 
    Carbon Copy - Invoice No 02 (2010) (2 Copies) (Filed).pdf
    01.Reciept #04 (Scanned-Copy).doc

我想通过删除从第一个括号开始的所有内容来重命名这些文件,所以它们看起来像这样:

    Invoice 10.1.pdf
    Carbon Copy - Invoice No 02.pdf
    01.Reciept #04.doc

我发现很多脚本会删除最后一个 x 字母,但没有任何内容会从特定字符中裁剪出来。

理想情况下,我想使用 Automator,但我猜这可能太复杂了。有什么想法吗?

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    试试:

    set xxx to (choose folder)
    tell application "Finder"
    set yyy to every paragraph of (do shell script "ls " & POSIX path of xxx)
    repeat with i from 1 to count of yyy
        set theName to item i of yyy
        set name of (file theName of xxx) to (do shell script "echo " & quoted form of theName & " | sed s'/ (.*)//'")
    end repeat
    end tell
    

    【讨论】:

      【解决方案2】:

      code posted by @adayzone 可以工作,但不需要为此使用 sed - 使用 offset 可以使用普通的 AppleScript:

      set fullString to "Invoice 10.1 (2012) (Digital) (4-Attachments).pdf"
      set trimmedString to text 1 thru ((offset of "(" in fullString) - 1) of fullString
      -- trim trailing spaces
      repeat while trimmedString ends with " "
          set trimmedString to text 1 thru -2 of trimmedString
      end repeat
      

      这将返回“Invoice 10.1”。要将文件名拆分为名称和扩展名,并重新添加扩展名,您可以使用 System Events' Disk-File-Folder 套件,这将提供方便的name extension 属性,您可以在修剪名称后存储和重新添加。

      假设您使用一些 Automator 操作来获取要处理的文件,完整的处理工作流程将是在文件选择部分之后添加一个 AppleScript 操作,代码如下:

      repeat with theFile in (input as list)
          tell application "System Events"
              set theFileAsDiskItem to disk item ((theFile as alias) as text)
              set theFileExtension to name extension of theFileAsDiskItem
              set fullString to name of theFileAsDiskItem
              -- <insert code shown above here>
              set name of theFileAsDiskItem to trimmedString & "." & theFileExtension
          end tell
      end repeat
      

      如果您希望 Automator 工作流程进一步处理文件,您还必须创建重命名文件的别名列表并从 AppleScript 操作中返回该别名(而不是 input,当然,不再有效)。

      【讨论】:

        猜你喜欢
        • 2020-07-28
        • 2013-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-20
        • 1970-01-01
        • 2012-01-28
        相关资源
        最近更新 更多