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: 2022-03-01
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 ICU syntax. The replacement template can use numeric backreferences (“$1”, “$2”).
For example, to change a YYYYMMDD date at the beginning of a filename to YYYY-MM-DD format, you could tell the script to replace ^(\d\d\d\d)(\d\d)(\d\d)
with $1-$2-$3
.
See also Replace in Filenames.
Installation Instructions · Download in Compiled Format · Download in Text Format
Script
use
AppleScript
version
"2.4"
use
framework
"Foundation"
use
scripting additions
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
_regex
to
my
(
NSRegularExpression's
regularExpressionWithPattern:
_pattern
options:0
|error|:(
missing value
))
set
_nsString
to
my
(
NSString's
stringWithString:
_string)
set
_result
to
_regex's
stringByReplacingMatchesInString:
_nsString
options:0
range:{0,
_nsString's
|length|()}
withTemplate:
_template
return
_result
as
Unicode text
end
regexReplace