Script to Batch Convert TXT to DOCX

Discussion in 'Mac Programming' started by wh7262, Jul 9, 2014.

  1. wh7262 macrumors newbie

    Joined:
    Jul 9, 2014
    #1
    First, I have been trying to create a batch convert text files into a docx file with formatting changes in Word document.
    Listed below is what I am wishing to create a script for, but have not had any success as I am a newbie to AppleScript and Automator:

    Open a Text file within Word

    Set Word document file with following configurations:
    Set all margins to 0.65 inches
    Set Font to Times New Roman
    Set Font Size to 11
    Go to first line and have it set as a Style of Heading 1
    Save document as .docx extension.​

    Then possibly create an EPUB file from this.

    Hope someone can enlighten me on this issue.
    Thanks in Advance
    The Older than Dirt Man
     
  2. joemod macrumors regular

    Joined:
    Jun 8, 2010
    Location:
    Athens, Greece
    #2
    Greetings,
    I am not in front of my mac at the moment, so pardon for the not straightforward instructions.
    If I recall correctly you can't do formatting from applescript inside word (apologies if I am mistaken). What I would do, is create a macro for the
    part, then call the macro from applescript along with .docx and epub steps.
     
  3. wh7262 thread starter macrumors newbie

    Joined:
    Jul 9, 2014
    #3
    Thanks, I will try and work on the macro and see what happens.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Here's an example to get you started.

    Applescript :

    Code:
    set theFolder to choose folder with prompt "Select a folder with txt files in."
    
    
    tell application "Finder"
    	set theTxtFiles to (files of folder theFolder whose name extension is "txt") as alias list
    	if not (exists folder "WordDocs" of folder theFolder) then
    		set wordDocsDest to (make new folder at theFolder with properties {name:"WordDocs"}) as string
    	else
    		set wordDocsDest to (theFolder as text) & "WordDocs:"
    	end if
    	if not (exists folder "EPUBFiles" of folder theFolder) then
    		set EPUBFilesDest to (make new folder at theFolder with properties {name:"EPUBFiles"}) as string
    	else
    		set EPUBFilesDest to (theFolder as text) & "EPUBFiles:"
    	end if
    end tell
    
    repeat with i from 1 to number of items in theTxtFiles
    	set this_item to item i of theTxtFiles
    	set baseName to do shell script "basename " & quoted form of POSIX path of this_item & " .txt"
    	tell application "Microsoft Word"
    		activate
    		open this_item --without confirm conversions
    		set theDocument to active document
    		run VB macro macro name "Macro1"
    		save as theDocument file name (wordDocsDest & baseName & ".docx") file format format document
    		close theDocument
    	end tell
    end repeat
    
    tell application "Finder" to set theDocxFiles to (files of folder wordDocsDest whose name extension is "docx") as alias list
    
    tell application "Microsoft Word" to quit
    
    tell application "Pages" to activate
    
    repeat with i from 1 to number of items in theDocxFiles
    	set theItem to item i of theDocxFiles
    	tell application "Pages"
    		---activate
    		set the_document to open theItem
    		activate
    		set the_document_name to name of the_document
    		ignoring application responses
    			export the_document to file (EPUBFilesDest & the_document_name & ".epub") as epub
    		end ignoring
    		tell application "System Events"
    			tell process "Pages"
    				repeat until exists sheet 1 of window 1
    					delay 0.5
    				end repeat
    				click button "OK" of sheet 1 of window 1
    			end tell
    		end tell
    		close the_document saving no
    	end tell
    end repeat
    Microsoft Word macro :

    Code:
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    Dim oDoc As Document
    Set oDoc = ActiveDocument
        With oDoc.PageSetup
         .LeftMargin = InchesToPoints(0.65)
         .RightMargin = InchesToPoints(0.65)
        .TopMargin = InchesToPoints(0.65)
        .BottomMargin = InchesToPoints(0.65)
        End With
        Selection.WholeStory
        Selection.Font.Name = "Times New Roman"
        Selection.Font.Size = 11
        Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=1, Name:=""
        Selection.Style = ActiveDocument.Styles("Heading 1")
    End Sub
    
    Note : Tested on Mavericks 10.9.4 with Microsoft Word 14.0.0 and Pages 5.0.1. YMMV. You can also convert your txt files to docx files with the textutil command and then do your formatting in Word.
     
  5. wh7262 thread starter macrumors newbie

    Joined:
    Jul 9, 2014
    #5
    Thanks for your hard work on this. Wished I knew more about Applescripts and Macros so I could help out others like you and the others on this forum.

    Thanks again, will try these out, but have been up for almost 20 hours, got to get some sleep and be alert today for taking wife for a MRI. Terrible migraines causing her to be nauseous, week, not hungry and dizzy, plus no sleep for her as well....

    Again thanks.
     
  6. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #6
    If the goal is an EPUB file you can do that without Word. Basically you stuff a boiler plate EPUB with the content of the text file.

    In general it is not easy to build an EPUB with a shell script but in your case you are making very simple EPUBs. Look inside one you made by hand.
     

Share This Page