3

Background

I have a workflow that collects documents from various sources, converts them to PDF, OCRs them, compresses them, extracts their contents and annotations, uploads the files to a server and makes the corresponding mysql entries in order to provide a full-featured index for my web-based search engine.

To display PDFs within the search engine, I use PDF.js from mozilla, which can, in some cases, not load documents with certain characters in the filename. These critical characters include german umlauts (Ä,Ö,Ü,ä,ö,ü), brackets ((),[],{}), french accents (é,è,à) and spanish accents (Ñ,ñ,Ó,ó,Á,á,É,é,Í,í,Ú,ú).

Every file that gets processed in applescript 'checks in' to php/mysql using curl. It would be no big deal to determine the new filename with PHP, but I have troubles to rename files which have the mentioned characters in their filename with AppleScript to a standardized name.

Question

I would like to implement a function that standardises filenames containing the special characters mentioned above using applescript.

The following filenames should become their corresponding values on the right

  • Riñón.pdf --> Rinon.pdf
  • Ergänzung.pdf --> Ergaenzung.pdf
  • Übersicht.pdf --> Uebersicht.pdf
  • Système impérmeable.pdf --> Systeme impermeable.pdf

In short, german umlauts become expanded (like Ä --> Ae, ü --> ue) and all other accents become their 'unaccented' corresponding value (like ñ --> n, é --> e) and brackets become spaces ((Ergänzung).pdf --> Ergaenzung .pdf)

Thanks for any advice

2 Answers 2

1

Renaming a file in AppleScript can be performed through the Finder:

tell application "Finder"
   set the name of file "Monterey" to "Eden"
end tell

Reducing a filename to a-z is tricky. If you are comfortable with perl, there is an ideal module called Text::Unidecode. Other approaches, such as using regular expressions, is discussed by Perl Monks in removing accents.

You mention using php, so this question may provide easier answers to integrate into your workflow, How do I remove accents from characters in a PHP string? The highest voted answer suggests:

function stripAccents($stripAccents){
  return strtr($stripAccents,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ','aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
}
5
  • Thank you, I'm aware of that call. Unfortunately, Perl is not an option, it has to be done entirely in AppleScript. During check-in, the filenames are, as mentioned above, submitted to php for db storage. Moreover, reproducing the same problem in php results in just one function, be it strtr or str_replace. Still, this has to be done entirely in applescript to ensure, that files don't get saved multiple times.
    – nic
    Mar 12, 2014 at 9:34
  • 1
    Are you aware of the do shell script AppleScript command? This can be used in an AppleScript to access other scripts and tools. This approach can be used to keep the flow control within the AppleScript itself. Mar 12, 2014 at 10:33
  • 1
    Hi I've posted my answer above. Thank you for your help; and yes – I was aware of do shell script but didn't consider perl to be an option due to lacking experience with it. Cheers
    – nic
    Mar 12, 2014 at 15:16
  • @nic - I'm not sure why you can't answer. Could you check again that there is no "Answer Your question" button... cl.ly/UNtc I've up voted your question so hopefully you can ask a question on Ask Different Meta if you continue to have problems. I'd really like to help you get the answer in the answer section if at all possible.
    – bmike
    Mar 12, 2014 at 15:29
  • @bmike Thank you very much bmike, the button has appeared and I was able to answer the question. Cheers
    – nic
    Mar 14, 2014 at 15:46
0

I have found a solution myself, and would like to share the result with you: I built a kind of clumsy function that finds and replaces the undesired chars if they appear in the string (unfortunately AppleScript returns bad results if the find-and-replace doesn't find an occurence.

Edit: As I have found the bracket chars to be uncritical, their transformation is excluded from the script pasted below. Therefore, the needles given at the bottom of the UnAccentString() function must be adjusted, if brackets should be replaced, too.

The findAndReplace() function is courtesy of MacScripter, thank you.

Be aware that the order of replacements within the UnAccentString() function is crucial. When you place the 'Ü'-transformation above the 'ü', you will get bad results. Interestingly, OS X seems to consider an 'ü' as valid when looking for 'Ü's, but nor vice versa nor with other* umlauts. No idea why this is happening but it works with this order:

on UnAccentString(TheString)

if TheString contains "ü" then
    set TheString to findAndReplace("ü", "Ue", TheString)
end if

if TheString contains "ä" then
    set TheString to findAndReplace("ä", "ae", TheString)
end if

if TheString contains "ö" then
    set TheString to findAndReplace("ö", "oe", TheString)
end if

if TheString contains "Ü" then
    set TheString to findAndReplace("Ü", "Ue", TheString)
end if

if TheString contains "Ä" then
    set TheString to findAndReplace("Ä", "Ae", TheString)
end if

if TheString contains "Ö" then
    set TheString to findAndReplace("Ö", "Oe", TheString)
end if

if TheString contains "é" then
    set TheString to findAndReplace("é", "e", TheString)
end if

if TheString contains "è" then
    set TheString to findAndReplace("è", "e", TheString)
end if

if TheString contains "à" then
    set TheString to findAndReplace("à", "a", TheString)
end if

if TheString contains "ó" then
    set TheString to findAndReplace("ó", "o", TheString)
end if

if TheString contains "á" then
    set TheString to findAndReplace("á", "a", TheString)
end if

if TheString contains "ú" then
    set TheString to findAndReplace("ú", "u", TheString)
end if

if TheString contains "í" then
    set TheString to findAndReplace("í", "i", TheString)
end if

if TheString contains "Á" then
    set TheString to findAndReplace("Á", "A", TheString)
end if

if TheString contains "É" then
    set TheString to findAndReplace("É", "E", TheString)
end if

if TheString contains "Í" then
    set TheString to findAndReplace("Í", "I", TheString)
end if

if TheString contains "Ó" then
    set TheString to findAndReplace("Ó", "O", TheString)
end if

if TheString contains "Ú" then
    set TheString to findAndReplace("Ú", "U", TheString)
end if

if TheString contains "ñ" then
    set TheString to findAndReplace("ñ", "n", TheString)
end if

if TheString contains "Ñ" then
    set TheString to findAndReplace("Ñ", "N", TheString)
end if

if TheString contains "ê" then
    set TheString to findAndReplace("ê", "e", TheString)
end if

if TheString contains "Ê" then
    set TheString to findAndReplace("Ê", "E", TheString)
end if

if TheString contains "ë" then
    set TheString to findAndReplace("ë", "e", TheString)
end if

if TheString contains "Ë" then
    set TheString to findAndReplace("Ë", "E", TheString)
end if

if TheString contains "È" then
    set TheString to findAndReplace("È", "E", TheString)
end if

if TheString contains ":" then
    set TheString to findAndReplace(":", "", TheString)
end if

if TheString contains "," then
    set TheString to findAndReplace(",", "", TheString)
end if

if TheString contains " " then
    set TheString to findAndReplace(" ", "-", TheString)
end if

if TheString contains "%" then
    set TheString to findAndReplace("%", "", TheString)
end if

if TheString contains "'" then
    set TheString to findAndReplace("'", "", TheString)
end if

return TheString & ".pdf" as text
end UnAccentString


on findAndReplace(tofind, toreplace, TheString)
set ditd to text item delimiters
set res to missing value
set text item delimiters to tofind
repeat with tis in text items of TheString
    if res is missing value then
        set res to tis
    else
        set res to res & toreplace & tis
    end if
end repeat
set text item delimiters to ditd
return res
end findAndReplace

Therefore, calling the function with the following string:

return UnAccentString("Übersetzungen im alltäglichen Leben eines Riñóns und eines Négligée à Noël") as text

will return

Uebersetzungen-im-alltaeglichen-Leben-eines-Rinons-und-eines-Negligee-a-Noel

This is a list of all the characters that the function looks for and their respective counterparts they're getting replaced with:

  • Ü --> Ue
  • Ä --> Ae
  • Ö --> Oe
  • ü --> Ue
  • ä --> ae
  • ö --> oe
  • é --> e
  • è --> e
  • à --> a
  • ó --> o
  • á --> a
  • ú --> u
  • í --> i
  • Á --> A
  • É --> E
  • Í --> I
  • Ó --> O
  • Ú --> U
  • ñ --> n
  • Ñ --> N
  • ê --> e
  • Ê --> E
  • ë --> e
  • Ë --> E
  • È --> E
  • : --> (null)
  • , --> (null)
  • (space) --> -
  • % --> (null)
  • ' --> (null)

I hope that helps anyone. Cheers, nic.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .