Applescript to convert Word to PDF

Discussion in 'Mac Programming' started by xpytljkqnwmxds, Feb 18, 2010.

  1. xpytljkqnwmxds macrumors newbie

    Joined:
    Feb 18, 2010
    #1
    I have no idea even where to start learning applescript. I want to make a script that when you select a .doc in Finder, it will make a pdf.

    I found a script that kind of does what I want, but have no idea how I would invoke it from the finder:

    http://www.jaimerios.com/?p=171
     
  2. fredthefool macrumors newbie

    Joined:
    Jun 4, 2008
    #2
    As for the automated or “batched“ creation of PDFs with AppleScript and the built-in Tools is not so easy as i would wish, the best approaches i actually could find out, are GUI-Scripting of the Printer dialog "Save as ... PDF" or -- in this case -- scripting an external program. Since you considered the Script you referred to as useful, i assume that MS Word is installed on your system.

    The AppleScript Overview will provide valuable information and links for the first start:

    http://developer.apple.com/mac/libr...ipt/Conceptual/AppleScriptX/AppleScriptX.html


    Detailed Description of the Language itself, you'll find in the
    AppleScript Language Guide

    http://developer.apple.com/mac/libr...eScriptLangGuide/introduction/ASLR_intro.html

    If you prefer a printed book, i can recommend the one of Stephen G. Kochan (which was a very good choice for me).

    In the script you referred to, the path to the doc (to be converted) is “hard-coded" as is the path to the output folder (where the PDFs are stored). For further, "general" use, you'll probably prefer to set up a dialog, which will let you choose one or more .doc-files to be processed. If you like this dialog to appear when starting the script, a "on run" routine will do the trick. And if you like to create a "droplet" (which will process all .doc-files dragged onto it), then use the "on open" routine.

    Save your script as “program“ or “program-bundle“, then you'll get a clickable icon, which you'll be able to place on your desktop or into your dock. Start your script via click on it, or drag one ore more .doc-files onto.

    If you take Jaime's code you referred, and throw away the first line ("set myDoc ...") and replace his output folder in the second line with yours, e.g. your Desktop ("set pdfSavePath to path to desktop folder"), then the following code should be a proper framework and able to help you.
    (The repeat-loop assures that each of the chosen files will be processed and the if-clause assures, that no other, accidentially mis-chosen file(-format) will be processed.) I hope, i haven't missed anything ...

    In the referred code, there is no command that closes "Microsoft Word", so it will stay open (in the background) after completing the process. Keep that in mind or, if you like, try to fix that as your own first steps in AppleScript ;)



    Code:
    on open fileList
    	doPrintToPDF(fileList)
    end open
    
    on run
    	set fileList to choose file with prompt "Select your .doc-files ... :" with multiple selections allowed
    	PrintToPDF(fileList)
    end run
    
    -- main
    on PrintToPDF(fileList)
    	repeat with myDoc in fileList
    		if (myDoc as text) ends with ".doc" then
    
    
    --[ place referred code here ]
    
    
    		end if
    	end repeat
    end PrintToPDF
    
     

Share This Page