# Applescript et Keynote



## Mike4444 (7 Octobre 2021)

Bonjour,

Via Applescript, je souhaiterais pouvoir exporter un diaporama Keynote au format HTML.
J'ai trouvé le script ci-dessous qui le fait. Toutefois, je voudrais le modifier légèrement, de la manière suivante :


*Ce que fait le script ci-dessous :**Ce que je voudrais changer :*Il exporte le résultat dans le dossier "Documents"Que le script exporte le résultat dans le dossier "Exportation" dont le chemin est /Users/Mike/ExportationIl crée un nouveau dossier sans écraser le précédent du même nomQue le script écrase le dossier précédent du même nom

Je m'y échine depuis plusieurs heures, ce doit être simple pour quelqu'un qui s'y connaît en Applescript.
D'avance, je remercie pour l'aide apportée.

Source du script : https://iworkautomation.com/keynote/document-export.html


```
-- THE DESTINATION FOLDER
-- (see the "path" to command in the Standard Additions dictionary for other locations, such as pictures folder, movies folder, sites folder, desktop folder)
set the defaultDestinationFolder to (path to documents folder)

tell application "Keynote"
    activate
    try
        if playing is true then tell the front document to stop
        
        if not (exists document 1) then error number -128
        
        -- DERIVE NAME FOR NEW FOLDER FROM NAME OF THE FRONT DOCUMENT
        set documentName to the name of the front document
        if documentName ends with ".key" then ¬
            set documentName to text 1 thru -5 of documentName
        
        -- CREATE AN EXPORT DESTINATION FOLDER
        -- IMPORTANT: IT’S ADVISED TO ALWAYS CREATE A NEW DESTINATION FOLDER, AS THE CONTENTS OF ANY TARGETED FOLDER WILL BE OVERWRITTEN
        tell application "Finder"
            set newFolderName to documentName
            set incrementIndex to 1
            repeat until not (exists folder newFolderName of defaultDestinationFolder)
                set newFolderName to documentName & "-" & (incrementIndex as string)
                set incrementIndex to incrementIndex + 1
            end repeat
            set the targetFolder to ¬
                make new folder at defaultDestinationFolder with properties ¬
                    {name:newFolderName}
            set the targetFolderHFSPath to targetFolder as string
        end tell
        
        -- EXPORT THE DOCUMENT
        with timeout of 1200 seconds
            export front document as HTML to file targetFolderHFSPath
        end timeout
        
    on error errorMessage number errorNumber
        display alert "EXPORT PROBLEM" message errorMessage
        error number -128
    end try
end tell
```


----------



## zeltron54 (8 Octobre 2021)

Bonjour,

Essai avec ces modifications et dis moi si cela fonctionne.


```
-- THE DESTINATION FOLDER
-- (see the "path" to command in the Standard Additions dictionary for other locations, such as pictures folder, movies folder, sites folder, desktop folder)
set the defaultDestinationFolder to (path to users folder) as string
set defaultDestinationFolder to defaultDestinationFolder & "Mike:Exportation:"

tell application "Keynote"
    activate
    try
        if playing is true then tell the front document to stop
        
        if not (exists document 1) then error number -128
        
        -- DERIVE NAME FOR NEW FOLDER FROM NAME OF THE FRONT DOCUMENT
        set documentName to the name of the front document
        if documentName ends with ".key" then ¬
            set documentName to text 1 thru -5 of documentName
        
        -- CREATE AN EXPORT DESTINATION FOLDER
        -- IMPORTANT: IT’S ADVISED TO ALWAYS CREATE A NEW DESTINATION FOLDER, AS THE CONTENTS OF ANY TARGETED FOLDER WILL BE OVERWRITTEN
        tell application "Finder"
            set newFolderName to documentName
            
            set chemin to defaultDestinationFolder & newFolderName
            if exists chemin then
                set repertoire to quoted form of POSIX path of chemin
                set efface to "rm -R " & repertoire
                do shell script efface
            end if
            
            set the targetFolder to make new folder at defaultDestinationFolder with properties {name:newFolderName}
            set the targetFolderHFSPath to targetFolder as string
        end tell
        
        -- EXPORT THE DOCUMENT
        with timeout of 1200 seconds
            export front document as HTML to file targetFolderHFSPath
        end timeout
        
    on error errorMessage number errorNumber
        display alert "EXPORT PROBLEM" message errorMessage
        error number -128
    end try
end tell
```


----------



## Mike4444 (8 Octobre 2021)

Merci infiniment !
Cela fonctionne parfaitement.
En découvrant vos modifications, je me rends compte que j'étais (très très) loin de la solution avec mes bidouilles de débutant.
Encore merci.
Cordialement


----------



## zeltron54 (8 Octobre 2021)

content pour toi !


----------

