Замена недопустимых символов в имени файла тире с использованием AppleScript - PullRequest
0 голосов
/ 02 мая 2018

Моя цель - создать службу в Automator с использованием AppleScript или Javascript, которая заменяет все недопустимые символы выбранного имени файла ()[\\/:"*?<>|]+_ и пробелов тире (-) и делает имя файла строчными.

Ответы [ 3 ]

0 голосов
/ 16 мая 2018

Замена недопустимых символов в имени файла / папки может быть достигнута с помощью сценария Bash Shell в вашей службе Automator.

Следующие шаги описывают, как этого добиться:

Настройка Automator

  1. Запуск Automator
  2. Введите ⌘N или выберите File> New в строке меню.
  3. Выберите Service и нажмите Choose
  4. В верхней части области холста настройте его параметры следующим образом:

    enter image description here

  5. Выберите Library в верхней части панели / столбца слева:

    • В поле поиска введите: Get Select Finder items и перетащите действие 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 Service / Workflow должна теперь выглядеть примерно так:

    enter image description here

  9. Введите ⌘S или выберите File> Save в строке меню. Назовем файл Replace Impermissible Chars.


Запуск службы:

  1. В Finder :

    • Выберите отдельный файл или папку, а затем ctrl + нажмите , чтобы отобразить контекстное меню
    • Выберите сервис Replace Impermissible Chars в контекстном меню для его запуска.
  2. В качестве альтернативы в Finder :

    • Выберите несколько файлов и / или папок, а затем ctrl + нажмите , чтобы отобразить контекстное меню
    • Выберите сервис Replace Impermissible Chars в контекстном меню для его запуска.

Примечания:

  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) на несколько символов вместо одного дефиса -.

0 голосов
/ 14 июня 2018

Два отличных решения здесь. Но так как каждая проблема обычно имеет много решений, я предлагаю другое:

    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
0 голосов
/ 02 мая 2018

Это довольно легко с помощью Regular Expression и Foundation Framework, соединенных с 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
...