AppleScript/Automator combiner plusieurs PDF dont le nom...

Bonjour,

Je me permets d'envoyer un message car quand je lance le programme jai une boîte qui s'ouvre ou il est écrit "erreur dans Pages : document\nom du document.pages\ ne comprend pas le message export number -1708 from document "nom du document"".

N'y connaissant rien en script j'aurais besoin d'aide afin de pouvoir rentrer le script.

Merci d'avance
 
Bonjour, Thisse

Pourrais-tu préciser exactement comment tu procèdes, et à quel moment tu as ce message.
Tu choisis bien les dossiers ?

Précise ta version d'os et surtout ta version de Pages
 
Dernière édition:
Bonjour zeltron54

Tout d'abord j'ai copier coller le lien dans l'éditeur de script, je sélectionne le dossier ou se situe les document à convertir, puis je choisis le dossier ou il doivent aller mais après il m'affiche cette erreur...

Pour la version de mac c'est 10.11.6 (15G22010)
Pour la version de page c'est 5.5.3(2152)
 
Je ne connais pas la version pages 5.5.3 , Peux tu vérifier si Dans un document ouvert , dans le menu Fichier tu as accès au menu Exporter vers ... PDF et si ce menu fonctionne sur ta version ?
 
Je viens de vérifier sur un Imac sous 10.11.6, la version de pages est la 5.6.2 (Que je viens de télécharger sur AppStore) tout fonctionne sans problème....
 
@zeltron54
Bonjour
J'espère que tu passes toujours sur le forum :)

J'ai très récemment acheté un MacBook Air M2, sur lequel j'ai remis plein de chose du vieux MBA 2013 bloqué en macos Big Sur 11.7.1.
Ce nouveau MBA M2 peut lui avoir MacOS Ventura 13.0.1.
Mais du coup, les scripts de fusion de PDF fait avec toi ne fonctionnent plus...
Première chose, le chemin pour exiftools a changé, c'est la première erreur, corrigée facilement ça :
1670165318013.png
J'ai donc modifié en conséquence le script avec le nouveau chemin d'accès :
AppleScript:
set NP to do shell script "/opt/homebrew/bin/exiftool -pagecount " & fich

Mais ensuite, j'ai une nouvelle erreur :
AppleScript:
L’action « Exécuter un script AppleScript » a rencontré une erreur : « Erreur dans Finder : sh: /System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py: No such file or directory »

Ce que je sais c'est que le script, join.py n'est plus un script python, mais un exécutable.
1670167718989.png
Et là je ne sais pas trop comment corriger cette erreur...
Bon et bien en fait il suffit juste de remplacer le chemin d'accès et d'enlever le .py, et ça fonctionne :
AppleScript:
                    do shell script "'/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join' --output " & nouveau1 & space & lenew


Du coup, et bien voilà, il faut une version spécifique à MacOS Ventura.
Je ne sais pas si c'était aussi le cas avec MacOS Monterey vu que je n'ai jamais pu l'utiliser sur le vieux MBA...


Je reposte le script complet ci-dessous au besoin ;)
AppleScript:
on run {input, parameters}
    
    
    set chemin to input as alias
    
    display notification "Préparation des dossiers temporaires..." with title "Fusion PDF Cours Élèves"
    
    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
        
        display notification "Duplication des fichiers à fusionner..." with title "Fusion PDF Cours Élèves"
        
        repeat with chaque_fichier in les_fichiers
            set nom to name of chaque_fichier as string
            
            if nom contains "- ELEVES" 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 "/opt/homebrew/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/MacOS/join' --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/MacOS/join' --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
    
    display notification "Fin de la fusion des PDF élèves du chapitre sélectionné." with title "Fusion PDF Cours Élèves"
    
    return input
end run
 
@zeltron54 Je suis en train de me dire qu'il serait top dans ce script d'inclure quelque chose de ce genre (algorithme car je ne connais pas le langage AppleScript) :
Bloc de code:
definir variable OS=version_de_l_OS
Si OS = 11.7.x alors :
    definir variable exiftool_PATH="/usr/local/bin/exiftool"
    définir variable pdf_join_PATH="/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"
sinon, si OS >= 12.0 alors :
    definir variable exiftool_PATH="/opt/homebrew/bin/exiftool"
    définir variable pdf_join_PATH="
/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join"
Et ensuite, il faut utiliser les variables de chemins pour les deux executables plutôt que d'écrire en dur le chemin.
Ce sera plus facile à maintenir en cas de nouveau changement de chemin d'accès.
Mais cette partie, avec les variables, je ne sais pas faire en AppleScript... avec PowerShell, ou Bash, pas de soucis :)
 
@MilesTEG
Bonsoir,

Je ne peux pas tester car mes ordinateurs sont tous limités à Mojave "10.14.6"

Par contre ta demande de tester le système et de définir les variables est réalisable.
ce qui donne:
Bloc de code:
set mon_os to get system version of (system info) -- récupère la version du système de l'utilisateur

if mon_os < "12" then -- si système inférieur à 12
    set exiftool_PATH to "/usr/local/bin/exiftool"
    set pdf_join_PATH to "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"
else --sinon
    set exiftool_PATH to "/opt/homebrew/bin/exiftool"
    set pdf_join_PATH to "/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join"
end if
et incorporer ses variables dans le script... le script devient:
Bien sur il t'appartient de tester ...;)
Bloc de code:
on run {input, parameters}
   
   
    set chemin to input as alias
   
    set mon_os to get system version of (system info) -- récupère la version du système de l'utilisateur
   
    if mon_os < "12" then -- si système inférieur à 12
        set exiftool_PATH to "/usr/local/bin/exiftool"
        set pdf_join_PATH to "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"
    else --sinon
        set exiftool_PATH to "/opt/homebrew/bin/exiftool"
        set pdf_join_PATH to "/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join"
    end if
   
   
    display notification "Préparation des dossiers temporaires..." with title "Fusion PDF Cours Élèves"
   
    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
       
        display notification "Duplication des fichiers à fusionner..." with title "Fusion PDF Cours Élèves"
       
        repeat with chaque_fichier in les_fichiers
            set nom to name of chaque_fichier as string
           
            if nom contains "- ELEVES" 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 exiftool_PATH & " -pagecount " & fich --avec chemin exiftool dans la variable
                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 "'" & pdf_join_PATH & "' --output " & nouveau1 & space & lenew --avec chemin du fichier join dans la variable
                    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 "'" & pdf_join_PATH & "' --output " & nouveau1 & space & lenew --avec chemin du fichier join dans la variable
                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
   
    display notification "Fin de la fusion des PDF élèves du chapitre sélectionné." with title "Fusion PDF Cours Élèves"
   
    return input
end run
 
  • J’adore
Réactions: MilesTEG
@zeltron54
Top, merci beaucoup.
Ça fonctionne très bien avec les variables :)
 
@MilesTEG

Il restera à vérifier à partir de quel OS (11 ou 12) le chemin d'installation d' "exiftool" et le chemin du logiciel système "join" ont changé, et d'ajuster la ligne à la bonne valeur.
Bloc de code:
if mon_os < "12" then -- si système inférieur à 12
 
  • J’aime
Réactions: MilesTEG
@MilesTEG

Il restera à vérifier à partir de quel OS (11 ou 12) le chemin d'installation d' "exiftool" et le chemin du logiciel système "join" ont changé, et d'ajuster la ligne à la bonne valeur.
Bloc de code:
if mon_os < "12" then -- si système inférieur à 12
J'avais hier ajouté un peu de commentaires autour du bloc des conditions pour alerter sur les versions et les chemins ;)
AppleScript:
    -- Changer les valeurs pour les tests suivants si jamais le script ne se lance plus.
    -- Vérifier aussi les chemins d'accès.
    if mon_os < "12" then -- si système inférieur à 12
        set exiftool_PATH to "/usr/local/bin/exiftool"
        set pdf_join_PATH to "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"
    else --sinon
        set exiftool_PATH to "/opt/homebrew/bin/exiftool"
        set pdf_join_PATH to "/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join"
    end if
       -- -----------------

En tout cas, merci de ton aide.
Il n'a pas l'air si complexe ce langage AppleScript.
 
Bonjour @zeltron54
Je voudrais modifier un petit peu le script pour qu'il supprime au lancement l'éventuel fichier issu d'une fusion précédente.
N'étant pas bien calé en AppleScript, je quémande encore ton aide ^^
Il faudrait que le script cherche le nom de fichier contenant : "ACTIVITES - ELEVES - Chapitre Complet.pdf" et le supprime.
Pour rappel, voilà le script actuel :
AppleScript:
on run {input, parameters}
 
    set chemin to input as alias
 
    set mon_os to get system version of (system info) -- récupère la version du système de l'utilisateur
    
    -- Changer les valeurs pour les tests suivants si jamais le script ne se lance plus.
    -- Vérifier aussi les chemins d'accès.
    if mon_os < "12" then -- si système inférieur à 12
        set exiftool_PATH to "/usr/local/bin/exiftool"
        set pdf_join_PATH to "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"
    else --sinon
        set exiftool_PATH to "/opt/homebrew/bin/exiftool"
        set pdf_join_PATH to "/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join"
    end if
       -- -----------------
 
    display notification "Préparation des dossiers temporaires..." with title "Fusion PDF Cours Élèves"
 
    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
    
        display notification "Duplication des fichiers à fusionner..." with title "Fusion PDF Cours Élèves"
    
        repeat with chaque_fichier in les_fichiers
            set nom to name of chaque_fichier as string
        
            if nom contains "- ELEVES" 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 exiftool_PATH & " -pagecount " & fich --avec chemin exiftool dans la variable
                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 "'" & pdf_join_PATH & "' --output " & nouveau1 & space & lenew --avec chemin du fichier join dans la variable
                    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 "'" & pdf_join_PATH & "' --output " & nouveau1 & space & lenew --avec chemin du fichier join dans la variable
                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
 
    display notification "Fin de la fusion des PDF élèves du chapitre sélectionné." with title "Fusion PDF Cours Élèves"
 
    return input
end run


En étudiant le code que tu m'avais fait, j'en viens à proposer la modification suivante :
AppleScript:
        repeat with chaque_fichier in les_fichiers
            set nom to name of chaque_fichier as string
            
            if nom contains "- ELEVES" and nom contains ".pdf" then
                
                if nom contains "ACTIVITES - ELEVES - Chapitre Complet.pdf" then
                    tell application "Finder"
                        delete file nom
                    end tell
                else
                    duplicate chaque_fichier to chemin_selection
                end if
            end if
        end repeat
Est-ce que cette modification est correcte ?
Y-a-t'il plus simple et plus efficace ?
 
Dernière édition:
Bonjour,

Dur, dur de se replonger dans cet ancien script ! ;)

Actuellement le script cherche ce fichier pour l'exclure de la compilation...

Donc ce que tu proposes est bon dans la logique juste à modifier :
On est déjà dans un bloc "finder" donc pas besoin d'ajouter les 2 lignes:
  • tell application "finder"
  • end tell
et la commande pour mettre le fichier dans la corbeille (cela ne l'efface pas) demande le chemin du fichier, pas son nom, donc :
- delete chaque_fichier
Le bloc "repeat devient donc:
Bloc de code:
repeat with chaque_fichier in les_fichiers
            set nom to name of chaque_fichier as string
         
            if nom contains "- ELEVES" and nom contains ".pdf" then
             
                if nom contains "ACTIVITES - ELEVES - Chapitre Complet.pdf" then
                        delete chaque_fichier
                else
                    duplicate chaque_fichier to chemin_selection
                end if
            end if
        end repeat

Dis moi si c'est bon !
 
  • J’aime
Réactions: MilesTEG
Je te précise, si tu veux la suppression définitive du fichier sans passer par la corbeille, remplace la ligne
Bloc de code:
delete chaque_fichier
par la ligne
Bloc de code:
do shell script "rm " & quoted form of POSIX path of (chaque_fichier as string)
 
  • J’aime
Réactions: MilesTEG
@zeltron54
Merci pour ce correctif :)

Le fichier "Chapitre Complet" initialement présent est bien remplacé par le nouveau. Mais l'ancien n'apparait pas dans la corbeille.
Est-ce normal ?
J'aurais préféré le trouver dans la corbeille en cas de clic maladroit ^^
 
Ha ! j'ai rien dit, il fallait installer le script !
Ça fonctionne donc bien comme il faut, en plaçant le pdf dans la corbeille.

Mais du coup je ne sais plus où est mon fichier .workflow o_O qui était initialement dans le dossier Automator sur iCloud Drive.
 
Trouvés :
Bash:
/Users/pierrick/Library/Services

Merci encore pour ton aide @zeltron54 :)
 
OK !
merci pour le retour.

Bonne fin de journée et au plaisir ! ;)
 
  • J’aime
Réactions: MilesTEG