set resultat to choose folder with prompt "Sélectionner le dossier pour les resultats"
set ledossier to choose folder with prompt "Sélectionner le dossier pour la recherche des PDF"
set pdf1 to ledossier as string
set pdf1 to quoted form of POSIX path of pdf1 as string
set bureau to POSIX path of (path to desktop folder as text)
set commande to "find " & pdf1 & " -maxdepth 1 -name '*.pdf' > " & bureau & "liste.txt"
do shell script commande
set laligne to 2
set drapeau to 0
repeat
    set liste1 to ""
    set nb1 to 0
    tell application "Microsoft Excel"
        activate
        set lefichier to value of range ("L" & laligne)
        set ccc to lefichier
        try
            set test to lefichier + 0
            set ccc to my entexte(lefichier, "L", laligne)
        end try
        set lefichier to ccc
        
        set colj to value of range ("j" & laligne)
        set ccc to colj
        try
            set test to colj + 0
            set ccc to my entexte(colj, "J", laligne)
        end try
        set colj to ccc
        
        set coli to value of range ("i" & laligne)
        set ccc to coli
        try
            set test to coli + 0
            set coli to my entexte(coli, "i", laligne)
        end try
        set coli to ccc
        
        set colm to value of range ("m" & laligne)
        set ccc to colm
        try
            set test to colm + 0
            set colm to my entexte(colm, "m", laligne)
        end try
        set colm to ccc
        
        set coln to value of range ("n" & laligne)
        set ccc to coln
        try
            set test to coln + 0
            set ccc to my entexte(coln, "N", laligne)
        end try
        set coln to ccc
        
        set colk to value of range ("k" & laligne)
        set ccc to colk
        try
            set test to colk + 0
            set ccc to my entexte(colk, "k", laligne)
        end try
        set colk to ccc
    end tell
    
    if lefichier is "" or lefichier is "0" then
        exit repeat
    end if
    
    set newnom to lefichier & "_" & colj & "_" & coli & "_" & colm & "_" & coln & "_" & colk & "ex"
    
    --Cherche le fichier
    set commande to "grep " & lefichier & "  " & bureau & "liste.txt"
    try
        set liste1 to do shell script commande
    on error
        set liste1 to ""
    end try
    
    set AppleScript's text item delimiters to {return}
    set nb1 to count text item of liste1
    
    if nb1 = 0 then
        set lelog to open for access ((path to desktop folder as text) & "les erreurs.txt") as text with write permission
        write "A la ligne : " & laligne & " dans liste = " & nb1 & " fichier trouvé pour " & lefichier & return to lelog starting at eof
        close access lelog
        set drapeau to 1
    end if
    
    if nb1 > 1 then
        set lelog to open for access ((path to desktop folder as text) & "les erreurs.txt") as text with write permission
        write "A la ligne : " & laligne & " dans liste = " & nb1 & " fichier trouvé pour " & lefichier & return to lelog starting at eof
        close access lelog
        repeat with i from 1 to nb1
            set fich to paragraph i of liste1
            set lelog to open for access ((path to desktop folder as text) & "les erreurs.txt") as text with write permission
            write fich & return to lelog starting at eof
            close access lelog
        end repeat
        
        set drapeau to 1
    end if
    
    
    set liste to liste1
    
    if drapeau = 0 then
        repeat with i from 1 to nb1
            set lechemin to text item i of liste
            set pournom to POSIX file lechemin as alias
            
            tell application "Finder"
                set lenom to name of pournom
            end tell
            
            set resultat to resultat as string
            set newnom to newnom & ".pdf"
            set a to POSIX file liste
            
            tell application "Finder"
                duplicate a to resultat
                set newfile to (resultat & lenom) as alias
                set name of newfile to newnom
            end tell
        end repeat
    end if
    set drapeau to 0
    set laligne to laligne + 1
end repeat
tell application "Finder"
    do shell script "rm " & bureau & "liste.txt"
    activate
    display dialog "termine"
end tell
on entexte(a, lacol, laligne)
    --display dialog " entre colonne " & lacol & " = " & a
    try
        set a to round of a rounding toward zero
    end try
    
    tell application "Microsoft Excel"
        activate
        set tmp to value of range "L1"
        set aa to "=TEXTE(" & lacol & laligne & ";0)"
        set value of range "L1" to aa
        set b to value of range ("L1") as string
        set value of range "L1" to tmp
    end tell
    
    set lefichier to b
end entexte