on run {input, parameters}
   
   
    set chemin to input as alias
    tell application "Finder"
        make new folder at ((path to home folder) as string) with properties {name:"compilation"}
        make new folder at ((path to home folder) & "compilation" as string) with properties {name:"la_selection"}
        set chemin_compilation to ((path to home folder) & "compilation:") as string
        set chemin_selection to ((path to home folder) & "compilation:la_selection:") as string
        set chemin_pageblanche to ((path to home folder) & "pageblanche.pdf") as string
       
        set les_fichiers to files of chemin
        repeat with chaque_fichier in les_fichiers
            set nom to name of chaque_fichier as string
           
            if nom contains "eleve" and nom contains ".pdf" then
               
                if nom contains "ACTIVITES - ELEVES - Chapitre Complet.pdf" then
                   
                else
                    duplicate chaque_fichier to chemin_selection
                end if
            end if
        end repeat
       
        set la_selection to chemin_selection as alias
        set les_fichiers to files of la_selection
        repeat with le_fichier in les_fichiers
            set nom to name of le_fichier
           
            if not (exists chemin_compilation & "la_compilation.pdf") then
                duplicate le_fichier to chemin_compilation
                set anciennom to chemin_compilation & nom as alias
                set name of anciennom to "la_compilation.pdf"
               
            else
               
                duplicate le_fichier to chemin_compilation
                set anciennom to chemin_compilation & nom as alias
                set name of anciennom to "suite.pdf"
               
                set fich to ((path to home folder) & "compilation:la_compilation.pdf") as string
                set fich to quoted form of POSIX path of fich
               
                set NP to do shell script "/usr/local/bin/exiftool -pagecount " & fich
                set NP to word 3 of NP
                if NP mod 2 is not 0 then
                   
                    set leblanc to chemin_pageblanche as string
                    set leblanc to quoted form of POSIX path of leblanc
                    set nouveau to ((path to home folder) & "compilation:nouveau.pdf") as string
                    set nouveau1 to quoted form of POSIX path of nouveau
                    set lenew to fich & " " & leblanc
                   
                    do shell script "'/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py' --output " & nouveau1 & space & lenew
                    do shell script "rm " & fich
                    set nouveau to nouveau as alias
                    set name of nouveau to "la_compilation.pdf"
                end if
               
                set le2 to ((path to home folder) & "compilation:suite.pdf") as string
                set le2 to quoted form of POSIX path of le2
                set nouveau to ((path to home folder) & "compilation:nouveau.pdf") as string
                set nouveau1 to quoted form of POSIX path of nouveau
                set lenew to fich & " " & le2
               
                do shell script "'/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py' --output " & nouveau1 & space & lenew
                do shell script "rm " & fich
                do shell script "rm " & le2
                set nouveau to nouveau as alias
                set name of nouveau to "la_compilation.pdf"
               
            end if
           
        end repeat
        set fich to ((path to home folder) & "compilation:la_compilation.pdf") as string
        set Chapitre to word 1 of nom
        set renom to (Chapitre & " - ACTIVITES - ELEVES - Chapitre Complet.pdf") as string
        set fich to fich as alias
        set name of fich to renom
        set fich to ((path to home folder) & "compilation:" & renom) as string
        set fich to fich as alias
        move fich to chemin with replacing
        set efface to ((path to home folder) & "compilation:") as string
        set efface to quoted form of POSIX path of efface
        do shell script "rm -Rf " & efface
       
    end tell
  
    tell application "Finder"
        (display dialog ("waouou ... Ca y est c'est fait !") buttons {"Salut !"})
    end tell
  
   
    return input
end run