Combine Word Documents through Script

Discussion in 'Mac Programming' started by saberahul, Aug 12, 2015.

  1. saberahul macrumors 68040

    Joined:
    Nov 6, 2008
    Location:
    USA
    #1
    Hello,

    Maybe someone can help me accomplish this. Here's what I am trying to do:
    1. For parent folder "Untitled Folder" on Desktop, there are many sub-folders inside. Each sub-folder has 1-2 .docx files
    2. Ultimate goal is to combine all the documents into a single PDF (ordering does not matter)
    3. For each document, insert a page break before inserting the next document otherwise it is not readable easily
    4. For each document, at the top of its insert, insert the filename of the document so I know what the next few pages are on until the next filename shows up

    Here's what I have so far:
    Automator:
    Get Specified Finder Items
    Get Folder Contents (Repeat for each subfolder found is checked)
    Combine Word Documents
    Save Word Documents as PDF

    This works; however, I am unable to get filename of each document before its insert and unable to get a page break.

    Apple Script:
    I found this code online and tried manipulating it, but had absolutely no luck. Only change I made was add .docx to it. Same problems as above when this runs.

    Can someone assist or point me in the right direction?

    Here's the code:
    Code:
    on open theFiles
       
        tell application "Microsoft Word" to set comboDoc to make new document
       
        repeat with i from 1 to (count theFiles)
            set theFile to item i of theFiles
            set filePath to theFile as Unicode text
            tell application "Finder"
                set {creaType, fileType} to {creator type, file type} of theFile
                set ext to name extension of theFile
                set fileName to name of theFile
            end tell
            if (creaType is "MSWD" and {fileType} is in {"BINA", "W8BN", "s8BN", "W6BN", "s6BN", "RTF "}) or ext = "docx" then
                tell application "Microsoft Word"
                    set comboRange to text object of comboDoc
                    set collapsedRange to collapse range comboRange direction collapse end
                    insert file file name filePath at collapsedRange
                end tell
            end if
        end repeat
        tell application "Microsoft Word" to activate
    end open
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    There is an insert break command in the Microsoft Word Dictionary.

     

Share This Page