-- renomme les fichiers et les dossiers en supprimant les caractères spéciaux
--ÀÁÂàÄÅàáâàäåÒÓÔÕÖòóôõöÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ()[]{}/%@#",;?!.
--AAAaAAaaaaaaOOOOOoooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn---------------_
tell application "Finder"
    set lechemin to choose folder with prompt "Sélectionnez le dossier contenant les fichiers et dossiers à traiter"
    my inspecter(lechemin)
end tell
tell application "Finder"
    my inspecter(lechemin)
end tell
on inspecter(lechemin)
    tell application "Finder"
        -- traitement des fichiers :
        set les_fichiers to files of lechemin
        repeat with chaque_fichier in les_fichiers
            -- traitement d'un fichier
            tell application "Finder"
                set lefichier to chaque_fichier
                set AppleScript's text item delimiters to {""}
                set nom to name of chaque_fichier --récupère le nom du fichier
                set extens to file nom in lechemin --recupere l extension du fichier
                set lextension to name extension of extens
                set sauv to AppleScript's text item delimiters --sauvegarder la variable delimiteur
                set AppleScript's text item delimiters to {"."} --récupère la partie avant le dernier "."
                set elements to text items of nom
                set nomcourt to (items 1 thru -2 of elements) as string
                set AppleScript's text item delimiters to sauv --restaurer la variable delimiteur
                set pourerreur to lefichier as string
                set nouveaunom to my remplace(nomcourt, pourerreur)
                if nomcourt is not nouveaunom then
                    set name of lefichier to (nouveaunom & "." & lextension) -- renomme le fichier
                end if
            end tell
        end repeat
       
        set a to container of lechemin as string
        set aa to lechemin as string
        set sauv to AppleScript's text item delimiters --sauvegarder la variable delimiteur
        set AppleScript's text item delimiters to {":"} --récupère la partie avant le dernier "."
        set nomcourt to (text item -2 of aa) as string
        set AppleScript's text item delimiters to sauv --restaurer la variable delimiteur
        set pourerreur to aa
        set nouveaunom to my remplace(nomcourt, pourerreur)
        if nomcourt is not nouveaunom then
            set name of lechemin to nouveaunom --renomme dossier
            set lechemin to a & nouveaunom as alias --reconstruit le chemin pour dossier suivant
        end if
        -- traitement des dossiers :
        set les_dossiers to folders of lechemin
        repeat with chaque_dossier in les_dossiers
            -- traitement d'un dossier
            my inspecter(chaque_dossier)
        end repeat
    end tell
end inspecter
tell application "Finder"
    activate
    (display dialog ("waouou ... Ca y est c'est fait !") buttons {"Salut !"})
end tell
on remplace(mavar, pourerreur)
    set aremplacer to {203, 231, 229, 136, 128, 129, 136, 135, 137, 136, 138, 140, 241, 238, 239, 205, 133, 152, 151, 153, 155, 154, 233, 131, 230, 232, 143, 142, 144, 145, 130, 141, 237, 234, 235, 236, 147, 146, 148, 149, 244, 242, 243, 134, 157, 156, 158, 159, 216, 132, 150, 40, 41, 91, 93, 123, 125, 47, 37, 64, 35, 34, 44, 59, 63, 33, 46}
    set par to {65, 65, 65, 97, 65, 65, 97, 97, 97, 97, 97, 97, 79, 79, 79, 79, 79, 111, 111, 111, 111, 111, 69, 69, 69, 69, 101, 101, 101, 101, 67, 99, 73, 73, 73, 73, 105, 105, 105, 105, 85, 85, 85, 85, 117, 117, 117, 117, 121, 78, 110, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 95}
    set nbar to count aremplacer
    set nb to count characters in mavar
    set new to mavar
    repeat with i from 1 to nb
        set a to character i in new
        set lavaleur to ASCII number a
        repeat with j from 1 to nbar
            if item j of aremplacer is lavaleur then
                try
                    set newcar to ASCII character (item j of par)
                    if i = 1 then
                        set avant to ""
                    else
                        set avant to text 1 thru (i - 1) of new
                    end if
                    if i = nb then
                        set new to avant & newcar
                    else
                        set new to avant & newcar & text nb thru -(nb - i) of mavar
                    end if
                on error
                    -- Ecrit un fichier erreur sur le bureau si erreur
                    set lelog to open for access ((path to desktop folder as text) & "les erreurs.txt") as text with write permission
                    write "Le nom n'a pas pu être remplacer : " & pourerreur & return to lelog starting at eof
                    close access lelog
                   
                end try
            end if
        end repeat
    end repeat
    set mavar to new
end remplace