Hi there,
I have a bunch of macros to rename files in Path Finder. All these macros work perfectly. I'm currently trying to write a macro to rename files to standard web naming conventions (no spaces, all lowercase, no special characters, no accented characters,…)
So, I use "Search and Replace Variable Using Regular Expression" and I created this action to replace the accented "e":
Basically, the expression must match any character in the set. But it does not work.
I really appreciate any help you can provide.
Hey Carey,
That works for me, so it would seem to be another step where the problem is occurring.
-Chris
Hi all,
This is really strange.
The following quick test is working fine:
However, the following macro executed on a file with the name "élèmënt.pdf" returns "élèmënt.pdf" instead of the expected "elements.pdf".
This is driving me nuts
Okay,
I found that - apparently - it's an encoding problem.
If I convert the "élèmënt.pdf" from my first example in HTML entities, this returns the following characters:
But, if I convert the text from the second example (returned from the AppleScript), this returns the following characters:
So, the regex finds nothing.
Any help?
The problem is that HFS defines the unicode as decomposed, so the accents are actually separate characters.
So you probably could just replace the accent characters.
But really the problem is that you are trying to do this yourself, when it is a task that has almost infinite complexity. Instead let some real system deal with it. For example, here is a Perl script solution that should work. The script is:
#!/usr/bin/perl
use warnings;
use strict;
use diagnostics;
use utf8;
binmode(STDOUT, ":utf8");
use Encode ();
use Unicode::Normalize ();
my $s = Encode::decode('UTF-8', $ENV{KMVAR_Process}, 1);
$s = Unicode::Normalize::NFD($s);
$s =~ s/\pM//g;
print $s;
Keyboard Maestro Actions.kmactions (1.3 KB)
1 Like
Hi Peter,
Thanks for the clarification. Now, I understand the HFS/Unicode thing.
I’m not a Perl specialist and I do not understand a single word of the script. It frustrates me terribly but it works. Thanks again for your help!
Hey Cary,
A simpler method is to use the Satimage.osax, although you have to install it.
http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html
------------------------------------------------------------
# Replace diacriticals with plain text.
------------------------------------------------------------
set nameList to {}
tell application "Path Finder"
set pfSelectionList to selection
repeat with i in pfSelectionList
set end of nameList to name of i
end repeat
end tell
# Replace diacriticals:
set nameList to change "\\p{M}" into "" in nameList with regexp without case sensitive
return nameList
------------------------------------------------------------
We take all that Perl and replace it with a 1-liner regular expression.
Basically all that Perl boils down to managing the utf8, so it can do a search/replace:
$stringVar =~ s/\pM/<nothing>/g;
You can learn more about the regex by reading this:
http://www.regular-expressions.info/unicode.html
If you want to visualize the problem you can run this from TextWrangler (or BBEdit) by pasting in a document and selecting ‘Run’ from the ‘#!’ menu.
* Select a file in Path Finder that has diacriticals FIRST.
#! /usr/bin/env bash
read -r -d '' myScript <<'EOF'
considering diacriticals
set nameList to {}
tell application "Path Finder"
set pfSelectionList to selection
repeat with i in pfSelectionList
set end of nameList to name of i
end repeat
end tell
set AppleScript's text item delimiters to linefeed
return nameList as unicode text
end considering
EOF
S=$(osascript -e "$myScript");
echo "$S";
-Chris
1 Like
Hey Chris,
Thanks for your help! I’ll take a look at these explanations calmly. Have a nice day…
—
Cary