-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/07/03 23:35
# dMod: 2016/07/04 01:43
# Appl: Microsoft Word 14.6.5
# Task: Extract Table Data from a Word Document
# Libs: None
# Tags: @Applescript, @Script, @Microsoft_Word, @Satimage.osax, @Extract, @Table, @Data
# Note: Tested only with Word 14.6.5 from Office 2011 on OSX 10.11.5.
-------------------------------------------------------------------------------------------
set dataCollator to {}
tell application "Microsoft Word"
tell active document
tell table 1
set rowCount to count of rows
set columnCount to count of columns
repeat with theRow from 1 to rowCount
set tempData to {}
tell row theRow
repeat with theCellNum from 1 to columnCount
set end of tempData to content of text object of cell theCellNum
end repeat
end tell
--------------------------------------------------------------------------------
# Remove characters introduced by Word:
--------------------------------------------------------------------------------
# Remove BELL character.
set tempData to cng(
"\\x{07}",
"",
tempData)
of me # Remove LINE TABULATION character.
set tempData to cng(
"\\x{0B}",
"\\n",
tempData)
of me # Remove Trailing whitespace
set tempData to cng(
"\\s+\\Z",
"",
tempData)
of me --------------------------------------------------------------------------------
set end of dataCollator to tempData
end repeat
end tell
end tell
end tell
set targetFile to ((path to desktop folder as text) & "subtitleDataList.lst")
writeFile(dataCollator, targetFile, 0, list)
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on cng(_find, _replace, _data)
change _find into _replace in _data with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on writeFile(srcData, targetFile, startPosition, dataType)
try
set fileRef to open for access targetFile with write permission
if startPosition = 0 then
set eof of fileRef to 0
end if
write srcData to fileRef as dataType starting at startPosition
close access fileRef
on error errMsg number errNum
try
close access fileRef
end try
error "Error in handler: writeFile of library: gen.lib" & return & return & errMsg
end try
end writeFile
-------------------------------------------------------------------------------------------