2012-11-24 7 views
2

J'essaie de créer un script qui ouvre un fichier swf avec safari et ensuite l'imprimer en format pdf en utilisant la fonction intégrée "Save as PDF". Jusqu'à présent, mon code ressemble à ceci:GUI Applescript sur Mountain Lion

set appleworksFolder to choose folder 

tell application "Finder" 
set folderItems to (files of entire contents of appleworksFolder) 
repeat with I from 1 to number of items in folderItems 
    set the_doc to item I of folderItems 
    set doc_name to name of the_doc as text 
    tell application "Finder" 
     set the clipboard to doc_name & ".pdf" 
    end tell 
    if name of the_doc is not ".DS_Store" then 
     try 
      tell application "Safari" 
      activate 
       open the_doc 
       tell application "System Events" 
        tell application process "Safari" 
         delay 1 
         click menu item "Print…" of menu "File" of menu bar 1 
         delay 5 
         click menu button "PDF" of window "Print" of application process "Safari" of application "System Events" 
         delay 1 
         click menu item "Save as PDF…" of menu "PDF" of menu button "PDF" of window "Print" of application process "Safari" of application "System Events" 
         delay 1 
         keystroke "v" using command down 
         click button "Save" of window "Save" of application process "Safari" of application "System Events" 
         delay 8 
         keystroke "w" using command down 
         delay 0.5 
        end tell 
       end tell 
      end tell 
     end try 
    end if 
end repeat 
end tell 

Cela fonctionne à voir le dialogue d'impression, mais il ne semble pas cliquer sur le bouton de menu pdf ne soyez pas si loin que cela. Je me demandais si c'était un problème avec le lion de montagne qui ne me laissait pas utiliser l'applescript pour contrôler les boutons, et si oui, y a-t-il une solution? Toute aide serait grandement appréciée!

+0

Vous pouvez également utiliser [wkpdf] (http://plessl.github.com/wkpdf/) pour créer les fichiers PDF. – user495470

Répondre

1

Je viens de montrer à quelqu'un comment faire cela dans Mail sur un autre site Web. Vous pouvez le voir dans la publication # 7 here. Quoi qu'il en soit, voici le code ajusté pour vos coordonnées et Safari. J'espère que ça aide!

set appleworksFolder to choose folder 

tell application "Finder" 
    set folderItems to (files of entire contents of appleworksFolder) as alias list 
end tell 

repeat with i from 1 to count of folderItems 
    set thisItem to (item i of folderItems) as text 

    tell application "Finder" 
     set n to name of file thisItem 
     set c to (container of file thisItem) as text 
    end tell 

    tell application "Safari" 
     activate 
     open file thisItem 
    end tell 

    saveAsPDFInSafari(c, n, true) 
end repeat 

tell me 
    activate 
    display dialog "Finished!" buttons {"OK"} default button 1 with icon note 
end tell 

(*********** SUBROUTINES *************) 
on saveAsPDFInSafari(saveFolder, saveFileName, shouldCloseWhenFinished) 
    set myDelay to 0.2 

    -- setup the name 
    if saveFileName does not end with ".pdf" then 
     if character -4 of saveFileName is "." then 
      set saveFileName to text 1 thru -5 of saveFileName 
     end if 
     set saveFileName to saveFileName & ".pdf" 
    end if 

    -- setup the folder 
    set saveFolder to saveFolder as text 
    set posixSaveFolder to POSIX path of saveFolder 
    if posixSaveFolder ends with "/" then set posixSaveFolder to text 1 thru -2 of posixSaveFolder 

    -- save as pdf 
    tell application "Safari" to activate 
    tell application "System Events" 
     tell process "Safari" 
      keystroke "p" using command down 
      repeat until exists sheet 1 of window 1 
       delay myDelay 
      end repeat 

      tell sheet 1 of window 1 
       click menu button "PDF" 
       repeat until exists menu 1 of menu button "PDF" 
        delay myDelay 
       end repeat 

       click menu item "Save as PDF…" of menu 1 of menu button "PDF" 

      end tell 

      repeat until exists (sheet 1 of sheet 1 of window 1) 
       delay myDelay 
      end repeat 

      keystroke saveFileName 
      delay myDelay 

      keystroke "g" using {command down, shift down} 
      repeat until exists sheet 1 of sheet 1 of sheet 1 of window 1 
       delay myDelay 
      end repeat 

      tell sheet 1 of sheet 1 of sheet 1 of window 1 
       set value of text field 1 to posixSaveFolder 
       delay myDelay 
       click button "Go" 
      end tell 

      repeat while exists sheet 1 of sheet 1 of sheet 1 of window 1 
       delay myDelay 
      end repeat 

      tell sheet 1 of sheet 1 of window 1 
       click button "Save" 
      end tell 

      repeat while exists sheet 1 of sheet 1 of window 1 
       delay myDelay 
      end repeat 

      repeat while exists sheet 1 of window 1 
       delay myDelay 
      end repeat 

      if shouldCloseWhenFinished then 
       keystroke "w" using command down 
      end if 
     end tell 
    end tell 
end saveAsPDFInSafari 
+0

Merci! Merci pour le détail aussi! –