Replace in Filenames (Regex)

Summary: Lets you make changes to the filenames of the selected records using find and replace and regular expressions.
Requires: EagleFiler
Install Location: ~/Library/Scripts/Applications/EagleFiler/
Last Modified: 2011-11-09

Description

EagleFiler will replace the source pattern with the replacement template in the filename of each selected record. This script deals with Unix filenames, so for a “/” character as shown in EagleFiler enter “:”, and for a “:” character enter “/”.

Regular expression patterns follow the Python syntax. The replacement template can use numeric backreferences (“\1”, “\2”) and named backreferences (“\g<1>”, “\g”).

See also Replace in Filenames.

Installation Instructions · Download in Compiled Format · Download in Text Format

Script

tell application "EagleFiler"
    
repeat
        
set _pattern to my promptForString("Enter pattern to find in the filenames:")
        
if _pattern is not "" then exit repeat
    
end repeat
    
    
set the _template to my promptForString("Enter replacement template:")
    
    
set _records to selected records of browser window 1
    
tell library document 1
        
repeat with _record in _records
            
set _name to _record's filename
            
set filename of _record to my regexReplace(_name, _pattern, _template)
        
end repeat
    
end tell
end tell

on promptForString(_prompt)
    
display dialog _prompt default answer "" buttons {"Cancel", "OK"} default button 2
    
return text returned of the result
end promptForString

on regexReplace(_string, _pattern, _template)
    
set _qString to _string's quoted form
    
set _qPattern to _pattern's quoted form
    
set _qTemplate to _template's quoted form
    
set _script to "/usr/bin/python -c \"import re; print re.sub(" & _qPattern & ", " & _qTemplate & ", " & _qString & "),\""
    
return do shell script _script
end regexReplace