使用AppleScript用短划线替换文件名中的无效字符

时间:2018-05-02 12:21:09

标签: javascript regex applescript automator

我的目标是使用AppleScript或Javascript在Automator中创建服务,它将所选文件名()[\\/:"*?<>|]+_的所有无效字符和短划线(-)替换为空格,并将文件名设为小写。

3 个答案:

答案 0 :(得分:0)

借助正则表达式和与AppleScriptObjC桥接的基础框架,这很容易。

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use framework "Foundation"

set fileName to "New(Foo)*aBcd<B|r.ext"

set nsFileName to current application's NSString's stringWithString:fileName
set nsLowerCaseFileName to nsFileName's lowercaseString()
set trimmedFileName to (nsLowerCaseFileName's stringByReplacingOccurrencesOfString:"[()[\\/:\"*?<>|]+_]" withString:"-" options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:nsLowerCaseFileName's |length|()}) as text
display dialog trimmedFileName

答案 1 :(得分:0)

使用Automator服务中的Bash Shell脚本可以替换文件/文件夹名称中不允许的字符。

以下步骤描述了如何实现这一目标:

配置Automator

  1. 启动Automator
  2. 输入⌘N,或选择File&gt;菜单栏中显示New
  3. 选择Service,然后点击Choose
  4. 在画布区域的顶部配置其设置如下:

    enter image description here

  5. 选择左侧面板/列顶部的Library

    • 在搜索字段中输入:获取选择查找项目并将Get Select Finder items操作拖到画布区域。

    • 在搜索字段中键入:运行Shell 并将Run Shell Script操作拖到画布区域。

  6. 按如下方式配置Run Shell Script操作的顶部:

    enter image description here

  7. 将以下Bash脚本添加到Run shell Script操作的主要区域:

    #!/usr/bin/env bash
    
    # The following characters are considered impermissible in a basename:
    #
    #  - Left Square Bracket:   [
    #  - Right Square Bracket:  ]
    #  - Left Parenthesis:      (
    #  - Reverse Solidus:       \
    #  - Colon:                 :
    #  - Quotation Mark         "
    #  - Single Quotation Mark  '
    #  - Asterisk               *
    #  - Question Mark          ?
    #  - Less-than Sign         <
    #  - Greater-than Sign      >
    #  - Vertical Line          |
    #  - Plus Sign              +
    #  - Space Character
    #  - UnderScore             _
    #
    #  1. Sed is utilized for character replacement therefore characters listed
    #     in the bracket expression [...] must be escaped as necessary.
    #  2. Any forward slashes `/` in the basename are substituted by default with
    #     a Colon `:` at the shell level - so it's unnecessary to search for them.
    #
    declare -r IMPERMISSIBLE_CHARS="[][()\\:\"'*?<>|+_ ]"
    declare -r REPLACEMENT_STRING="-"
    
    # Obtain the POSIX path of each selected item in the `Finder`. Input must
    # passed to this script via a preceding `Get Selected Finder Items` action
    # in an Automator Services workflow.
    declare selected_items=("$@")
    
    declare -a sorted_paths
    declare -a numbered_paths
    
    # Prefix the POSIX path depth level to itself to aid sorting.
    for ((i = 0; i < "${#selected_items[@]}"; i++)); do
      numbered_paths+=("$(echo "${selected_items[$i]}" | \
          awk -F "/" '{ print NF-1, $0 }')")
    done
    
    # Sort each POSIX path in an array by descending order of its depth.
    # This ensures deeper paths are renamed before shallower paths.
    IFS=$'\n' read -rd '' -a sorted_paths <<<  \
        "$(printf "%s\\n" "${numbered_paths[@]}" | sort -rn )"
    
    
    # Logic to perform replacement of impermissible characters in a path basename.
    # @param: {Array} - POSIX paths sorted by depth in descending order.
    renameBaseName() {
      local paths=("$@") new_basename new_path
    
      for path in "${paths[@]}"; do
    
        # Remove numerical prefix from each $path.
        path="$(sed -E "s/^[0-9]+ //" <<< "$path")"
    
        # Replaces impermissible characters in path basename
        # and subsitutes uppercase characters with lowercase.
        new_basename="$(sed "s/$IMPERMISSIBLE_CHARS/$REPLACEMENT_STRING/g" <<< \
            "$(basename "${path}")" | tr "[:upper:]" "[:lower:]")"
    
        # Concatenate original dirname and new basename to form new path.
        new_path="$(dirname "${path}")"/"$new_basename"
    
        # Only rename the item when:
        # - New path does not already exist to prevent data loss.
        # - New basename length is less than or equal to 255 characters.
        if ! [ -e "$new_path" ] && [[ ${#new_basename} -le 255 ]]; then
          mv -n "$path" "$new_path"
        fi
      done
    }
    
    renameBaseName "${sorted_paths[@]}"
    
  8. Automator服务/工作流程的完整画布区域现在应该如下所示:

    enter image description here

  9. 输入⌘S,或选择File&gt;菜单栏中显示Save。我们将文件命名为Replace Impermissible Chars

  10. 运行服务:

    1. Finder

      • 选择单个文件或文件夹,然后 ctrl + 点击以显示上下文菜单
      • 在上下文菜单中选择Replace Impermissible Chars服务以运行它。
    2. 或者,在 Finder

      • 选择多个文件和/或文件夹,然后 ctrl + 点击以显示上下文菜单
      • 在上下文菜单中选择Replace Impermissible Chars服务以运行它。
    3. 注意:

      1. Finder 将允许服务一次最多选择1000个文件/文件夹。

      2. 如果这是您创建的第一个Automator服务,可能(如果我记得正确!)需要重新启动计算机才能通过上下文弹出窗口显示它菜单。

      3. 如果满足以下两个条件之一,Bash Shell脚本将不会替换文件/文件夹名称中不允许的字符:

        • 如果已存在与生成的新文件/文件夹名称相匹配的文件/文件夹名称。例如:假设我们有一个名为hello-world.txt的文件,并且在同一文件夹中有一个名为hello?world.txt的文件。如果我们在hello?world.txt上运行服务,则不会更正/更改其名称,因为这可能会覆盖已存在的hello-world.txt文件。

        • 如果生成的文件名为>=到255个字符。当然,只有当您将REPLACEMENT_STRING值(在Bash / Shell脚本中)更改为多个字符而不是仅仅一个连字符-时,才会出现这种情况。

答案 2 :(得分:0)

这里有两个很好的解决方案。但由于每个问题通常有很多解决方案,我提供另一个:

    property alphabet : "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    --------------------------------------------------------------------------------
    rename from "HELLO World+Foo(\"Bar\")new.ext"
    --------------------------------------------------------------------------------
    ### HANDLERS
    #
    # rename from:
    #   Receives a text string and processes it for invalid characters, which
    #   get replaced with the specified replacement string (default: "-"),
    #   returning the result
    to rename from filename given disallowed:¬
        invalidCharSet as text : "[()[\\/:\"*?<>|]+_] ", replaceWith:¬
        replacementStr as text : "-"
        local filename
        local invalidCharSet, replacementStr

        set my text item delimiters to {replacementStr} & ¬
            the characters of the invalidCharSet

        text items of the filename as text

        makeLowercase(the result)
    end rename


    # makeLowercase():
    #   Receives a text string as input and returns the string formatted as
    #   lowercase text
    to makeLowercase(str as text)
        local str

        set my text item delimiters to ""

        if str = "" then return ""

        set [firstLetter, otherLetters] to [¬
            the first character, ¬
            the rest of the characters] of str


        tell the firstLetter to if ¬
            it is "-" or ¬
            it is not in the alphabet then ¬
            return it & my makeLowercase(the otherLetters)

        considering case
            set x to (offset of the firstLetter in the alphabet) mod 27
        end considering

        return character x of the alphabet & my makeLowercase(the otherLetters)
    end makeLowercase

此代码可用于运行AppleScript Automator 操作,将rename from...放在on run {input, parameters}处理程序中,其余代码在它之外。它可以跟随为其提供 Finder 中的文件列表的操作,或者如果它作为 服务

    property alphabet : "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    on run {input, parameters}
        tell application "Finder" to repeat with f in input
            set the name of f to (rename from f)
        end repeat
    end run

    to rename from ...
        .
        .
    end rename

    to makeLowercase(str as text)
        .
        .
    end makeLowercase