Can Mac's Automator scan a folders contents & lists the files that are over a certain length?

844 Views Asked by At

I work in media production at a university, we work on Mac systems, but our servers are windows based. Illegal characters & long file names are causing us problems when transferring our production files to the server.

To prevent file transfers failing & being sent to a holding pen in our DAM system i'm looking to create a simple Automator App that can be used by the production team to do the following;

  1. Accept source folder as input for the app.
  2. Scan contents & replace the following characters ()\/[]"*?<>|+ with an underscore.
  3. Scan contents & for file names longer than 100 characters
  4. Log / report on the affected files for our producers to amend.

Within Automator I have had success with replacing the illegal characters using a find & replace rule for each, but I'm not sure of the apple script that would be required to check the file name lengths & reporting all changes.

I'll be eternally grateful if anyone would be able to suggest a route forwards!

1

There are 1 best solutions below

0
Mockman On

Obviously, I have no clue what you might be passing along, nor how you might be replacing the text in filenames, nor exactly what you would like to report, as you don't really provide any of those details. However, here is a straightforward way to test for filenames longer than a given length within automator.

To provide the initial file list to test, I begin with three actions:

Get Selected Finder Items
Get Folder Contents ('repeat for each subfolder found' is checked)
Filter Finder Items ('kind is document')

This will pass along a list of alias file objects to the fourth 'run applescript' action, as input.

on run {input, parameters}
    set fList to input
    set nList to {} -- becomes list of filenames
    set cList to {} -- becomes list of filename lengths
    tell application "Finder"
        repeat with ff in fList -- list of file aliases
            set nn to name of ff
            set end of nList to nn
            set end of cList to length of nn
        end repeat
    end tell
    set longList to {}
    repeat with cc from 1 to length of cList
        if item cc of cList is greater than 100 then
            set end of longList to item cc of nList -- names with more than 100 characters
            
        end if
    end repeat
    return longList
end run

This should be run when a folder is selected in the Finder.

Essentially, what this does is take the input (i.e. list of file aliases) and create corresponding lists of filenames and filename lengths. The script then loops through the list of lengths looking for items > 100 and and returns a list of matching filenames. If instead, you set end of longList… to items from fList then it will return a list of file aliases.

If this isn't what you're looking for, please advise. The above works under Sierra.