Monday, July 16, 2018

#1 2017-10-20 04:45:05 pm

Registered: 2017-06-12
Posts: 2

Name & Path in "Export as PDF..."

Noticed today that there is an "Export as PDF…" option in Mail.

So I was playing around with activating that via AppleScript. Wondering if anyone knows if there is a way to specify the path in this dialog and "get" the filename so I can use later in the script after the PDF is saved?

Ideally I could start the script, it would select the newest message in the inbox, and export the PDF to a specific path. Then as a POC, alert the name of the file to demonstrate availability after the save.




tell application "Mail"
   set x to selected messages of first message viewer    
end tell

tell application "System Events"
   tell process "Mail"
       click menu item "Export as PDF…" of menu "File" of menu bar 1
   end tell
end tell



#2 2017-10-21 12:49:09 pm

Yvan Koenig
Registered: 2006-09-14
Posts: 3249

Re: Name & Path in "Export as PDF..."

You may try to run this script.



# Builds the path to the storage folder to fit your needs
set thePDFPath to (path to documents folder as text) & "the PDFs:"
# The interface requires a POSIX path
set posixFolderPath to POSIX path of thePDFPath

tell application "Mail"
   set theMessages to selected messages of first message viewer
   set PDFsNames to {}
   repeat with aMessage in theMessages
       set end of PDFsNames to (subject of aMessage) & ".pdf"
   end repeat
end tell

tell application id "" to tell process "Mail"
   tell menu bar 1
       -- get name of menu bar items
01 - "Apple",
02 - "Mail",
03 - "Fichier",
04 - "Édition",
05 - "Présentation",
06 - "Boîte aux lettres",
07 - "Message",
08 - "Format",
09 - "Fenêtre",
10 - "Aide"}

       set mt to 3
       -- get name of menu bar item mt
       -- {"Fichier"}
       tell menu bar item mt to tell menu 1
           --    get name of menu items
           (* {
01 - "Nouveau message",
02 - "Nouvelle fenêtre de visualisation",
03 - "Ouvrir le Message",
04 - missing value,
05 - "Fermer la fenêtre",
06 - "Tout fermer",
07 - "Fermer l’onglet"
08 - "Enregistrer",
09 - "Enregistrer sous…",
10 - "Enregistrer comme modèle…",
11 - missing value,
12 - "Joindre des fichiers…",
13 - "Enregistrer les pièces jointes…",
14 - "Coup d’œil sur les pièces jointes…",
15 - missing value,
16 - "Importer des boîtes aux lettres…",
17 - missing value,
18 - "Exporter au format PDF…",
19 - missing value,
20 - "Imprimer…"}

           set mi to 18
           --    get name of menu item mi
           --{"Exporter au format PDF…"}
           click menu item mi
       end tell -- menu bar item mt to tell menu 1
   end tell -- menu bar 1
Wait for the availability of the sheet *)

       if exists sheet 1 of window 1 then exit repeat
   end repeat
   tell sheet 1 of window 1
       # Open the sheet allowing us to define the destination folder. I assumes that it exists.
       keystroke "g" using {command down, shift down}
       repeat until exists sheet 1
           delay 0.02
       end repeat
       tell sheet 1
           # Fills the field dedicated to the target folder path
           # CAUTION. before 10.11 the target field was a text field. Now it's a combo box
           if class of UI elements contains combo box then
               --> {static text, combo box, button, button}
               set value of combo box 1 to posixFolderPath
               --> {static text, text field, button, button}
               set value of text field 1 to posixFolderPath
           end if
# An alternate way would be
set value of UI element 2 to posixFolderPath
# But there is no guarantee that the index remains equal to 2

           --get name of buttons (*Aller, Annuler*)
           keystroke return # ditto click button 1 (ou Aller)
       end tell -- sheet 1
       --class of UI elements
       --> {button, button, button, button, group}
       -- name of buttons
       --> {"Sélectionner", "Nouveau dossier", "Annuler", "Options"}
       click button 1
   end tell # sheet 1 of window 1
end tell # application id ""

delay 5 # Building the PDFs take some time.
# Now the folder thePDFPath contain the PDFs listed in PDFsNames

tell application id ""
   set thePaths to path of every file of folder thePDFPath whose name extension is "pdf"
end tell

As you may see, I disabled several instructions which are useful during the coding process to define some parameters.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) samedi 21 octobre 2017 20:47:52



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)