Saturday, January 28, 2012

Making Microsoft Word 2011 Mac produce (Xe)(La)TeX Style Outlines for Academic Papers with Applscript

(*
Academic Paper Setup

Copyright © 2012 Jonathan Komar

Just kidding there is no copyright. Open Source rules!

You may use this code however you like. It would be nice 
if you made reference to the original code when sharing.

THINGS TO DO:
find a fix for changing font color of headings without hardcoding it.
fix headings 4-9
*)



(*<Properties to be set by the user*)
--number suffix corresponds to heading 1, heading 2, etc.
--NOTE: To change the coloring of headings, it must be hardcoded. must find a fix.
property fontName1 : "CMU Serif Roman"
property fontName2 : "CMU Serif Roman"
property fontName3 : "CMU Serif Roman"
property fontName4 : "CMU Serif Roman"
property fontName5 : "CMU Serif Roman"
property fontName6 : "CMU Serif Roman"
property fontName7 : "CMU Serif Roman"
property fontName8 : "CMU Serif Roman"
property fontName9 : "CMU Serif Roman"
property fontSize1 : 15
property fontSize2 : 14
property fontSize3 : 13
property fontSize4 : 12
property fontSize5 : 12
property fontSize6 : 12
property fontSize7 : 12
property fontSize8 : 12
property fontSize9 : 12
property boldTF : true
property italicTF : false
property highlightTF : false
(*Properties to be set by the user>*)

tell application "Microsoft Word"
    tell active document
        (*<normal code BLOCK*)
        set name of font object of Word style style normal to "CMU Serif Roman"
        set font size of font object of Word style style normal to 12
        set bold of font object of Word style style normal to false
        set italic of font object of Word style style normal to false
        set color index of font object of Word style style normal to black
        (*normal code BLOCK>*)
        
        
        (*<heading 1 code BLOCK*)
        set name of font object of Word style style heading1 to fontName1
        set font size of font object of Word style style heading1 to fontSize1
        set bold of font object of Word style style heading1 to boldTF
        set italic of font object of Word style style heading1 to italicTF
        set color index of font object of Word style style heading1 to black
        --set paragraph format left indent of paragraph format of Word style style heading1 to (centimeters to points centimeters 0) --indent sections relative to margin
        --set tab stop position of tab stop of Word style style heading1 to (centimeters to points centimeters 1)
        --set tab hanging indent of paragraph format of Word style style heading1 to count 1
        (*heading 1 code BLOCK>*)
        (*<heading 2 code BLOCK*)
        set name of font object of Word style style heading2 to fontName1
        set font size of font object of Word style style heading2 to fontSize1
        set bold of font object of Word style style heading2 to boldTF
        set italic of font object of Word style style heading2 to italicTF
        set color index of font object of Word style style heading2 to black
        (*heading 2 code BLOCK>*)
        (*<heading 3 code BLOCK*)
        set name of font object of Word style style heading3 to fontName1
        set font size of font object of Word style style heading3 to fontSize1
        set bold of font object of Word style style heading3 to boldTF
        set italic of font object of Word style style heading3 to italicTF
        set color index of font object of Word style style heading3 to black
        (*heading 3 code BLOCK>*)
        (*<heading 4 code BLOCK*)
        set name of font object of Word style style heading4 to fontName1
        set font size of font object of Word style style heading4 to fontSize1
        set bold of font object of Word style style heading4 to boldTF
        set italic of font object of Word style style heading4 to italicTF
        set color index of font object of Word style style heading4 to black
        (*heading 4 code BLOCK>*)
        (*<heading 5 code BLOCK*)
        set name of font object of Word style style heading5 to fontName1
        set font size of font object of Word style style heading5 to fontSize1
        set bold of font object of Word style style heading5 to boldTF
        set italic of font object of Word style style heading5 to italicTF
        set color index of font object of Word style style heading5 to black
        (*heading 5 code BLOCK>*)
        (*<heading 6 code BLOCK*)
        set name of font object of Word style style heading6 to fontName1
        set font size of font object of Word style style heading6 to fontSize1
        set bold of font object of Word style style heading6 to boldTF
        set italic of font object of Word style style heading6 to italicTF
        set color index of font object of Word style style heading6 to black
        (*heading 6 code BLOCK>*)
        (*<heading 7 code BLOCK*)
        set name of font object of Word style style heading7 to fontName1
        set font size of font object of Word style style heading7 to fontSize1
        set bold of font object of Word style style heading7 to boldTF
        set italic of font object of Word style style heading7 to italicTF
        set color index of font object of Word style style heading7 to black
        (*heading 7 code BLOCK>*)
        (*<heading 8 code BLOCK*)
        set name of font object of Word style style heading8 to fontName1
        set font size of font object of Word style style heading8 to fontSize1
        set bold of font object of Word style style heading8 to boldTF
        set italic of font object of Word style style heading8 to italicTF
        set color index of font object of Word style style heading8 to black
        (*heading 8 code BLOCK>*)
        (*<heading 9 code BLOCK*)
        set name of font object of Word style style heading9 to fontName1
        set font size of font object of Word style style heading9 to fontSize1
        set bold of font object of Word style style heading9 to boldTF
        set italic of font object of Word style style heading9 to italicTF
        set color index of font object of Word style style heading9 to black
        (*heading 9 code BLOCK>*)
        
    end tell
    
    (*<apply numbering to the sections code BLOCK*)
    set selFind to find object of selection --selects text of execute find command
    clear formatting selFind --reset selFind just to make sure
    set style of selFind to style heading1 --set style we are looking for
    execute find selFind find text "" wrap find find continue with find format and match forward
    if found of selFind is true then
        --display dialog (get content of text object of selection) --for debugging only
        (*<code BLOCK word__set paragraph style*)
        --I apply the formatting to the previously found text
        set myLT to list template 5 of list gallery 3 of active document
        apply list format template (list format of text object of selection) ¬
            list template myLT
    end if
    (*Apply numbering to the sections code BLOCK>*)
    
    
    
    
    
    
    
    
    
    
    
    
    (*******************************************
    (*<code BLOCK word_get style.scpt*)
    --i select the header 1 stuff
    set selFind to find object of selection --selects text of execute find command
    clear formatting selFind --reset selFind just to make sure
    set style of selFind to style heading1 --set style we are looking for
    execute find selFind find text "" wrap find find continue with find format and match forward
    if found of selFind is true then
        --display dialog (get content of text object of selection) --for debugging only
        (*<code BLOCK word__set paragraph style*)
        --I apply the formatting to the previously found text
        set theRange to text object of active document --i say where to apply the script
        set myStyle to Word style of object of selection of active document --debug only
        --set myStyle to make new Word style at active document with properties ¬
        {name local:"Section", style type:style type paragraph}
        tell font object of selFind
            set name to fontName1
            set font size to fontSize1
            set bold to true
        end tell
        set myLT to list template 5 of list gallery 3 of active document
        apply list format template (list format of text object of selection) ¬
            list template myLT
        (*code BLOCK word__set paragraph style>*)
        --set style of theRange to myStyle --i actually apply the settings
        update styles active document
    end if
    (*code BLOCK word_get style.scpt>*)
    *********************************)
    
end tell

No comments:

Post a Comment