Tuesday, February 28, 2012

Customizing Microsoft Word with AppleScript

Original HOWTO Document

Customizing Microsoft Word with AppleScript
The first step in doing anything in Microsoft Word or Microsoft Excel with AppleScript is to download the tutorials for MS Office 2004 from Microsoft.

I started using Xelatex for my linguistics papers and I decided that I didn’t really need to go that hardcore. I decided to write scripts to boss around MS Word and make it imitate the quality of my Xelatex papers (which are great, but you can’t just copy and paste them due to the nature of code).
Outline of my idea

Use Microsoft Excel to set variables
Use AppleScript to generate a plist from excel file stored in ~/Library/Preferences
Use AppleScript to retrieve those properties from the plist in ~/Library/Preferences and apply them to a Word Document.


The following is an example Excel Document with can be used to set various settings in a Microsoft Word Document.
Setting Value Type of Value Accepted Comments
boldHeading1 TRUE boolean
boldHeading2 TRUE boolean
boldHeading3 TRUE boolean
boldHeading4 TRUE boolean
boldHeading5 TRUE boolean
boldHeading6 TRUE boolean
boldHeading7 TRUE boolean
boldHeading8 TRUE boolean
boldHeading9 TRUE boolean
boldNormal FALSE boolean
footerLine TRUE boolean
headerLine TRUE boolean
italicsHeading1 FALSE boolean
italicsHeading1 FALSE boolean
italicsHeading2 FALSE boolean
italicsHeading3 FALSE boolean
italicsHeading4 FALSE boolean
italicsHeading5 FALSE boolean
italicsHeading6 FALSE boolean
italicsHeading7 FALSE boolean
italicsHeading8 FALSE boolean
italicsHeading9 FALSE boolean
italicsNormal FALSE boolean
sectionNumbering TRUE boolean number each heading
smallcapscaptionFigures TRUE boolean
smallcapscaptionTables TRUE boolean
coverdateAuto TRUE boolean if false, applescript tries to use the dateCustom field
coverPage TRUE boolean makes cover appear on new page
footerlineThickness 25 integer points
headerlineThickness 25 integer
sizeHeading1 15 integer font size
sizeNormal 12 integer
marginpageL 2 integer sets page margin
marginpageT 2 integer sets page margin
marginpageR 2 integer sets page margin
marginpageB 2 integer sets page margin
sizeHeading2 14 integer
sizeHeading3 13 integer
sizeHeading4 12 integer
sizeHeading5 12 integer
sizeHeading6 12 integer
sizeHeading7 12 integer
sizeHeading8 12 integer
sizeHeading9 12 integer
colorHeading1 black string
colorHeading2 black string color word
colorHeading3 black string color word
colorHeading4 black string color word
colorHeading5 black string color word
colorHeading6 black string color word
colorHeading7 black string color word
colorHeading8 black string color word
colorHeading9 black string color word
colorNormal black string color word
fontcaptionFigures CMU Serif Roman string system font name
fontHeading1 CMU Serif Roman string system font name
fontHeading2 CMU Serif Roman string system font name
fontHeading3 CMU Serif Roman string system font name
fontHeading4 CMU Serif Roman string system font name
fontHeading5 CMU Serif Roman string system font name
fontHeading6 CMU Serif Roman string system font name
fontHeading7 CMU Serif Roman string system font name
fontHeading8 CMU Serif Roman string system font name
fontHeading9 CMU Serif Roman string system font name
fontNormal CMU Serif Roman string "normal" style font
sectionnumberingType list template 5 of list gallery 3 string this refers to the list numbering menu in word
textfooterL 1 string sets text of footer left
textfooterM 1 string sets text of footer middle
textfooterR 1 string sets text of footer right
textheaderL 1 string sets text of header left
textheaderM 1 string sets text of header middle
textheaderR 1 string sets text of header right
fontcaptionTables CMU Serif Roman string
coverTitle 1 string
coverAuthor 1 string
coverdateCustom 1 string
coverDegree Master of Linguisics string
coverUniversity Universit├Ąt Innsbruck string
coverPresentedto presented to the faculty of blah blah string if blank, not included

AppleScript to Read Excel to Plist file
The following script only needs to be run once. It will generate the property list file for the “Apply Settings to Word Document” script. If you make any changes to the settings, it will need to be run again to replace the old plist file.

set theFile to (choose file with prompt "Please select a Microsoft Excel file") as string

set theColumn to text returned of (display dialog "Please select a range" default answer "A2:C72")

set theRange to (theColumn) as string

tell application "Microsoft Excel"

open workbook workbook file name theFile

set aList to get value of range "A2:C72" of active sheet

end tell

--set up new plist file using the empty dictionary list item as contents

set userDir to (path to current user folder) as string

set plistfilePath to userDir & "Library:Preferences:msword.Academicsetup.plist"

tell application "System Events"

set the parent_dictionary to make new property list item with properties {kind:record}

set outFile to make new property list file with properties {contents:parent_dictionary, name:plistfilePath}

--add values to plist

tell outFile

repeat with i from 1 to (count of aList)

set keyName to (item 1 of (item i of aList) as string)

set keyValue to (item 2 of (item i of aList) as string)

set keyType to (item 3 of (item i of aList) as string)

if keyType is "boolean" then

make new property list item at end with properties ¬

{kind:boolean, name:keyName, value:keyValue}

end if

if keyType is "string" then

make new property list item at end with properties ¬

{kind:string, name:keyName, value:keyValue}

end if

if keyType is "integer" then

--important step to avoid real numbers (with decimals), coerse keyValue to be integer

-- create an integer entry

make new property list item at end with properties ¬

{kind:number, name:keyName, value:(keyValue as integer)}

end if

end repeat

end tell

end tell
say "Dieses Skript ist fertig!"
AppleScript to Apply Settings to Word Document

(*USE EXISTING PLIST START*)

set userDir to (path to current user folder) as string

set plistfilePath to userDir & "Library:Preferences:msword.Academicsetup.plist"

global plistfilePath

(*USE EXISTING PLIST*)

tell application "Microsoft Word"

tell active document

(**)

(**)

(**)

(**)

(**)

(**)

(**)

(**)

(**)

(**)

end tell

(**)

end tell

on pL(theItem)

tell application "System Events"

set p_list to property list file (my plistfilePath)

value of property list item (theItem as string) of p_list

end tell

end pL

1 comment:

  1. Attractive blog for Technical information.Thanks for sharing your views! For Help and support of laptop,computer ,software etc visit this link:-
    We24support Services

    ReplyDelete