-- renomme les fichiers et les dossiers en supprimant les caractères spéciaux
--ÀÁÂàÄÅàáâàäåÒÓÔÕÖòóôõöÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ()[]{}/%@#",;?!.\>< (majuscule par minuscule)
--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"
                try
                    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
                    if lextension is not "" then
                        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
                    else
                        set nomcourt to nom
                    end if
                on error
                    set lelog to open for access ((path to desktop folder as text) & "les erreurs.txt") as text with write permission
                    write "Le nom du fichier n'a pas pu être extrait : " & lefichier & return to lelog starting at eof
                    close access lelog
                end try
                set pourerreur to lefichier as string
                set nouveaunom to my remplace(nomcourt, pourerreur)
          
                if nomcourt is not nouveaunom then
                    try
                        set a to container of lefichier as string
                        set verifexist to a & nouveaunom & "." & lextension
                        if exists verifexist then
                            set nouveaunom to nouveaunom & "2"
                        end if
                        if lextension = "" then
                            set name of lefichier to nouveaunom
                        else
                            set name of lefichier to nouveaunom & "." & lextension -- renomme le fichier
                        end if
                        -- Ecrit un fichier erreur sur le bureau si erreur
                    on error
                        set lelog to open for access ((path to desktop folder as text) & "les erreurs.txt") as text with write permission
                        write "Le fichier n'a pas pu être renommer : " & pourerreur & return to lelog starting at eof
                        close access lelog
                    end try
                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
            try
                set verifexist to a & nouveaunom
                if exists verifexist then
                    set nouveaunom to nouveaunom & "2"
                end if
                set name of lechemin to nouveaunom --renomme dossier
                set lechemin to a & nouveaunom as alias --reconstruit le chemin pour dossier suivant
            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 dossier n'a pas pu être reenommer : " & pourerreur & return to lelog starting at eof
                close access lelog
            end try
        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, 92, 62, 60, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90}
    set par to {97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 101, 101, 101, 101, 101, 101, 101, 101, 99, 99, 105, 105, 105, 105, 105, 105, 105, 105, 117, 117, 117, 117, 117, 117, 117, 117, 121, 110, 110, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 95, 95, 45, 45, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}
    set nbar to count aremplacer
    set new to mavar
 
    set lesblancs to count character in new
    repeat --supprime espace fin
        if character lesblancs of new is not " " then
            exit repeat
        end if
        set new to text 1 thru (lesblancs - 1) of new
        set lesblancs to count characters in new
    end repeat
    repeat --supprime espace debut
        if character 1 of new is not " " then
            exit repeat
        end if
        set new to text 2 thru lesblancs of new
        set lesblancs to count characters in new
    end repeat
    -- remplace les espaces par des tirets
    set nb to count characters in new
    repeat with i from 1 to nb
        if character i of new is " " then
            set new to text 1 thru (i - 1) of new & "-" & text (i + 1) thru nb of new
        end if
    end repeat
    set nb to count characters in new
    set i to 1
    --supprime les tirets qui se suivent
    repeat
        if character i of new is "-" then
            if character (i + 1) of new is "-" then
                set new to text 1 thru (i - 1) of new & text (i + 1) thru nb of new
                set nb to nb - 1
                set i to i - 1
            end if
        end if
        set i to i + 1
        if i = nb then exit repeat
    end repeat
    -- remplace les caractères de la liste
    set nb to count characters in new
    set mavar to new
    try
        repeat with i from 1 to nb
            set a to character i in mavar
            set lavaleur to ASCII number a
            repeat with j from 1 to nbar
                if item j of aremplacer is lavaleur then
                    if lavaleur = 60 or lavaleur = 62 then --traite le ><
                        if i = 1 then
                            set new to (text 2 thru nb of mavar)
                            set avant to ""
                        else
                            set avant to text 1 thru (i - 1) of new
                        end if
                        if i = nb then
                            set new to avant
                        else
                            set new to avant & text nb thru -(nb - i) of mavar
                        end if
                  
                    else --traite les autres caracteres
                        set newcar to ASCII character (item j of par)
                        if i = 1 then
                            set new to newcar & (text 2 thru nb of new)
                            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
                    end if
                end if
            end repeat
        end repeat
    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 caractère n'a pas pu être remplacer : " & pourerreur & return to lelog starting at eof
        close access lelog
    end try
    set mavar to new
end remplace