Insert header and Footer into a Word Document

I have written a script (with a lot of help from the net) to list only folders and subfolders from a selected location. I then put the list into a word document. what I want to do is add a header and footer .In the header I would like to put the source disk and center it, in the footer the date and page numbers.

Just cannot find any advice on the Web as to how one would do that any suggestions greatly appreciated. This is the script so far.


--Object to Return list of the 1st Folder selected and all its Sub Folders

global sSL
global FolderList

tell application "Finder"
	set FolderList to {}
	set theFolder to choose folder --Get Starting Folder
	set strip to (container of theFolder) --Folder Path excluding name Folder
	set dk to (disk of theFolder)
	set dk to dk as string
	set FolderList to FolderList & "Listing of Parent and all Subfolders on Disk: " & dk & return & return
	set sS to strip as string --Convert to String
	set sSL to (length of sS) + 1 --Length Path excluding 1st Folder Name
end tell

my processFolders(theFolder) --Call Handler
activate
tell application "Microsoft Word"
	activate
	make new document
	set myRange to create range active document start 0 end 0
	--set test to FolderList as string
	--set content of myRange to test
	
	
end tell
tell application "TextEdit"
	activate
	make new document
	--set textheader to "This is a Test"
	set text of document 1 to FolderList as string
end tell

--display dialog FolderList as string

on processFolders(afolder)
	set aFolderS to afolder as string --Converts Foder being processed to string
	set aFl to length of aFolderS --Length of Folder inc Name
	set folderN to (text sSL thru aFl) of aFolderS --Removes the path before 1st Folder
	set FolderList to FolderList & return & folderN --Creates List of all Folders
	tell application "Finder"
		set subFolders to every folder of afolder
		repeat with eachFolder in subFolders
			my processFolders(eachFolder) --Recursive Call to Handler
		end repeat
	end tell
end processFolders

Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)

Hi Mitch,

Here’s an example I’ve been working on and still needs a lot of fixing up:

set t to quoted form of "Title
Para 1
Para 2
Para 3
The End"
set cmd to "echo " & t & " | textutil -stdin -stdout -format txt -convert rtf | sed -e 's/Title/{\\\\i Title}/; s/The End/{\\\\b The End}/' >~/Desktop/Testing.rtf"
do shell script cmd

It seems like part of what you want to do. It converts the text (t) to rtf and changes the first and last paragraphs to italics and bold respectively. I think there is one for centering also.

I really need to fix it up and probably make it shorter.

Edited: taking a break.

Edited: I can’t find a centering special character that works. Here’s what I got so far.

set t to quoted form of "<Title>
Para 1
Para 2
Para 3
<The End>"
set cmd to "echo " & t & " | textutil -stdin -stdout -format txt -convert rtf | sed -e '
s/<Title>/{\\\\b <Title>}/
s/<The End>/{\\\\i <The End>}/
' >~/Desktop/Testing.rtf"
do shell script cmd

I’ll try to look into it tomorrow unless someone else solves it. One thing you might think about doing is using bold or something instead of centering.

Edited: here’s the macro way using System Events.

set t to "<Title>
Para 1
Para 2
Para 3
<The End>"
tell application "TextEdit"
	launch
	activate
	delay 1
	set text of front document to t
	delay 1
end tell
tell application "System Events"
	key code 126 using command down -- up arrow
	delay 1
	keystroke (character id 124) using {command down}
	delay 1
	key code 125 using command down -- down arrow
	delay 1
	keystroke (character id 124) using {command down}
end tell

Edited: found a way to center the rtf with \qc. Turning it off is \ql I think. Need to work on it tomorrow. Still need to turn off centering:

set t to quoted form of "<Title>
Para 1
Para 2
Para 3
<The End>"
set cmd to "echo " & t & " | textutil -stdin -stdout -format txt -convert rtf | sed -e '
s/<Title>/\\\\qc <Title>/
s/<The End>/\\\\qc <The End>/
' >~/Desktop/Testing.rtf"
do shell script cmd

Edited: good news! Got it working:

set t to quoted form of "<Title>
Para 1
Para 2
Para 3
<The End>"
set cmd to "echo " & t & " | textutil -stdin -stdout -format txt -convert rtf | sed -e '
s/<Title>/\\\\qc <Title>/
s/Para 1/\\\\ql Para 1/
s/<The End>/\\\\qc <The End>/
' >~/Desktop/Testing.rtf"
do shell script cmd

Still need to fix it up a lot. Now I can rest.

gl,
kel

Hi Kel

thanks for all your effort it is really appreciated however I think I may not have explained what I was tryng to do. Also my script may be misleading. My scrip shows two finally actions opening a Word Document and also a text document.

what I wanted was to set headers and footers in the word document not the text one . I have no idea how one sets a header in a text document just using the application.

I run the system event script which certainly did the centering job. could not get your last scrip to work until I put in the tell block calling textedit but that did not center the first and last lines of the variable t.

Anyway obviously a lot of effort thanks again.

Peter

Hi Mitch,

I’ve found that you don’t need TextEdit to make a word document. Here’s an example if you’d rather not open TextEdit:

set t to quoted form of "<Title>
Para 1
Para 2
Para 3
<The End>"
set cmd to "echo " & t & " | textutil -stdin -stdout -format txt -convert rtf | sed -e '
/<Title>/ {
s/<Title>/\\\\qc &/
n
s/.*/\\\\ql &/
}
/<The End>/ s/<The End>/\\\\qc &/' | textutil -stdin -output ~/Desktop/Word.doc -format rtf -convert doc"
do shell script cmd

One problem you might have with this is that the text might be too long.

Still need to do something about that Sed part if there are any expert critique.

If you want to use the System Events macro, that’s ok.

Edited: corrected a typo.

gl,
kel

Can someone please put me out of my misery and write a better part for the Sed part of the script. It’s redundant.

Maybe tomorrow I might come up with the answer.

Thanks,
kel