having trouble trapping MSWord error when file won't open

My script shuts down when opening a corrupt MsWord file
I want to trap unopenable files and continue with next file
How do I error trap this error before the MsWord error shuts downs the script???

MsWord error says: The open XLM file ooo30075.docx cannot be open because there are problems with the content or the file name might contain invalid characters (for example, /)
No error detail available

It then forces me to hit Ok on two dialog boxes to continue.

Help!
Thanks!

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
	try
		set filelist to files of folder POSIX file "/Users/xxxx/Documents/DataRecoverydocs/Document/docx/" as alias list
		set num_Of_files to get count of filelist
	on error error_message number error_number
		display dialog " Could NOT get files from folder into a file list  " & return & error_message & "  error num.  " & error_number giving up after 2
		endscript()
	end try
end tell

------------------- This section begins opening each file in the filelist --------------
repeat with f in filelist --repeats till eof
	
	delay 1
	
	tell application "Microsoft Word"
		activate
		try
			set currentfilename to ""
			
			open f
			
			delay 1
			
		on error error_message number error_number
			display dialog " Could NOT open the file in  Word!  " & return & error_message & "  error num.  " & error_number giving up after 2
			tell application "Microsoft Word" to close documents saving no
			endscript()
		end try
		
		
		-- ??????? how to know if file opened, and did not have corrupt name... before MSWord shuts down the script with its error message???
		-- ??? the error trap on open f did not work ????
		
		set theActiveDoc to the active document
		
		set theContent to content of text object of active document as string
		try
			set thefirstline to paragraph 1 of theContent --This is the heading or (name) of the recovered document
			
			------- The MSWord error stops the progress of script because it was a corrupted file and couldnt open file --how can i trap this error???	
			
		on error error_message number error_number
			display dialog " Filename is corrupted--Word can not open file!  " & return & error_message & "  error num.  " & error_number giving up after 2
			tell application "Microsoft Word" to close documents saving no
			endscript()
			--display dialog "first line " & thefirstline giving up after 2
			
		end try
	end tell
	
end repeat

tell me
	activate
	set ScreenUpdating to true
	display dialog "Script is Finished....Closing Script" giving up after 1
	tell application "Microsoft Word" to close documents saving no
	delay 2
	tell application "Microsoft Word" to quit
	endscript()
end tell

on endscript()
	say "The script is stoping"
	error number -128
end endscript

Run this script which will return the list of the filenames.
Maybe it will help to find what is wrong with them.

set sourceFolder to (path to documents folder as string) & "DataRecoverydocs:Document:docx:"

tell application "Finder"
	try
		set filelist to files of folder sourceFolder as alias list
		set num_Of_files to get count of filelist
	on error error_message number error_number
		display dialog " Could NOT get files from folder into a file list " & return & error_message & " error num. " & error_number giving up after 2
		--endscript()
	end try
end tell

set fileNames to {}
tell application "Finder"
	repeat with f in filelist --repeats till eof
		set end of fileNames to (get name of f)
	end repeat
end tell
fileNames

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 12 juin 2020 11:08:02

Thanks for the reply!

The file names are ok!

It must be that the file is unreadable.

Any other suggestion on how to trap this error before MSWord stops the script execution?

Thanks!

I don’t own Merdosoft products so I tried to find an usable protocol with other applications able to open docx files.

TextEdit, Pages or LibreOffice are able to do that but, when the document is corrupted, they issue an error dialog and I found no way to drive it.

I tried with BBEdit using the free version).
When a document displays nothing in the left pane, it’s a corrupted one so we may drop it.
Alas, as well as valid documents, some corrupted ones display a hierarchy like:

V [__] papa-maman.docx
> [] _rels
_____ [] [Content_Types].me
> [] docProps
> [] word
when the document is valid.

So, the script below allow us to apply a first filter but not a complete one.

(*
use the free cliclick

CAUTION, my system doesn't use dark mode so, under Mojave or Catalina
you may need to edit the test identifying the pane without text.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 13 juin 2020 19:16:22
*)

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set aDoc to choose file of type {"org.openxmlformats.wordprocessingml.document"} --> an alias

tell application "BBEdit"
	open aDoc
	set {x, y, xr, yb} to bounds of window 1
	set {x, y} to {x + 5, y + 20}
	
	set maybe to ""
	repeat with i from 1 to 6 -- was from 0 to 10
		tell me to set theColor to do shell script "/usr/local/bin/cliclick cp:" & x + (i * 17) & "," & y + (i * 11)
		set maybe to maybe & theColor -- the string theColor ends with a return character
	end repeat
	-- log maybe
	(*
possibly valid document without dark mode
"113 187 217
105 199 241
220 220 220
255 255 255
255 255 255
255 255 255
"
corrupted document without dark mode
"247 247 247
247 247 247
244 243 244
229 229 229
194 194 194
255 255 255
"
*)
	set theNumbers to my decoupe(maybe, {space, return})
end tell

set maybe to item 1 of rest of (my sortAList:theNumbers)
if maybe as integer < 150 then -- may be edited for dark mode
	display dialog "“" & aDoc & "” may be a valid docx"
else
	display dialog "“" & aDoc & "” is corrupted docx"
end if

#=====

on sortAList:theList
	set theArray to current application's NSArray's arrayWithArray:theList
	set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
	return theArray as list
end sortAList:

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

Download Cliclick from www.bluem.net
and install it as : “/usr/local/bin/cliclick”
By default, the folder “Macintosh HD:usr:” is hidden.
but you may open it with :

set p2usr to POSIX file "/usr/"
tell application "Finder" to open p2usr

If I remember well, a subfolder named “local” exists
but you will have to create the subfolder “bin” to get the hierarchy:
“Macintosh HR:usr:local:bin:” where you will store “cliclick”.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 13 juin 2020 16:49:43

Here is a more complet script which move the corrupted docx files in a subfolder named “Corrupted”
and move the possibly valid ones in a subfolder named “Possibly Valid”.

(*
Move the corrupted docx files in a subfolder named "Corrupted"
Move the possibly valid docx files in a subfolder named "Possibly Valid"

use the free cliclick

CAUTION, my system doesn't use dark mode so, under Mojave or Catalina
you may need to edit the test identifying the pane without text.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 14 juin 2020 11:16:22
*)

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

-- set sourceFolder to (path to desktop folder as string) & "docx:" -- used by YK for tests
set sourceFolder to (path to documents folder as string) & "DataRecoverydocs:Document:docx:" -- Edit to fit your needs
set corrupted to "Corrupted"
set possiblyValid to "Possibly Valid"
set corruptedFolder to sourceFolder & corrupted
set possiblyValidFolder to sourceFolder & possiblyValid
tell application "System Events"
	if not (exists folder corruptedFolder) then
		make new folder at end of folder sourceFolder with properties {name:corrupted}
	end if
	if not (exists folder possiblyValidFolder) then
		make new folder at end of folder sourceFolder with properties {name:possiblyValid}
	end if
	
	set theDocs to path of files of folder sourceFolder whose type identifier is "org.openxmlformats.wordprocessingml.document"
end tell

repeat with aDoc in theDocs
	tell application "BBEdit"
		open aDoc as «class furl»
		set docWindow to window 1
		set {x, y, xr, yb} to bounds of docWindow
		set {x, y} to {x + 5, y + 20}
		
		set maybe to ""
		repeat with i from 1 to 6 -- was from 0 to 10
			tell me to set theColor to do shell script "/usr/local/bin/cliclick cp:" & x + (i * 17) & "," & y + (i * 11)
			set maybe to maybe & theColor -- the string theColor ends with a return character
		end repeat
		-- log maybe
		(*
possibly valid document without dark mode
"113 187 217
105 199 241
220 220 220
255 255 255
255 255 255
255 255 255
"
corrupted document without dark mode
"247 247 247
247 247 247
244 243 244
229 229 229
194 194 194
255 255 255
"
*)
		close docWindow
	end tell -- BBEdit
	
	set theNumbers to my decoupe(maybe, {space, return})
	set maybe to item 1 of rest of (my sortAList:theNumbers)
	if maybe as integer < 150 then -- may be edited for dark mode
		set target to possiblyValidFolder
	else
		set target to corruptedFolder
	end if
	(my moveFileAt:aDoc toFolder:target)
end repeat

#=====

on sortAList:theList
	set theArray to current application's NSArray's arrayWithArray:theList
	set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
	return theArray as list
end sortAList:

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on buildStamp()
	tell (current date) to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end buildStamp

#=====

on moveFileAt:sourcePath toFolder:destFolder -- here, sourcePath and destFolder are Hfs paths
	set POSIXsource to current application's NSString's stringWithString:(POSIX path of sourcePath)
	set POSIXdest to current application's NSString's stringWithString:(POSIX path of destFolder)
	set theNewPath to POSIXdest's stringByAppendingPathComponent:(POSIXsource's lastPathComponent())
	set fileManager to current application's NSFileManager's defaultManager()
	if fileManager's fileExistsAtPath:theNewPath then
		set thePathNoExt to theNewPath's stringByDeletingPathExtension()
		set stamp to my buildStamp()
		set theExtension to POSIXsource's pathExtension()
		-- insert the stamp in the file name so it will not duplicate
		set theNewPath to (thePathNoExt's stringByAppendingString:stamp)'s stringByAppendingPathExtension:theExtension
	end if
	set {theResult, theError} to fileManager's moveItemAtPath:POSIXsource toPath:theNewPath |error|:(reference)
	if (theResult as boolean) is false then
		error (theError's localizedDescription() as string)
	end if
end moveFileAt:toFolder:

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 14 juin 2020 12:04:05

Bingo, I found a neater way to filter the corrupted docx files.

I try to open them with TextEdit.
When a document is corrupted, a dialog warn us that the document is corrupted.
We have no way to close this window but the system is fair enough which issue a timeout error when 2 minutes are passed so when the error is issued, we may click the “OK” button closing the dialog.

Happily, there is a command allowing us to rule the timeout duration.
I choose to set it to 10 seconds and now the script does its duty flawlessly.

I commented two instructions which may be edited to try to use your preferred application.

(*
Move the corrupted docx files in a subfolder named "Corrupted"
Move the possibly valid docx files in a subfolder named "Possibly Valid"

Try to open the docx files with TextEdit

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 14 juin 2020 15:16:22
*)

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

-- set sourceFolder to (path to desktop folder as string) & "docx:" -- used by YK for tests
set sourceFolder to (path to documents folder as string) & "DataRecoverydocs:Document:docx:" -- Edit to fit your needs
set corrupted to "Corrupted"
set possiblyValid to "Possibly Valid"
set corruptedFolder to sourceFolder & corrupted
set possiblyValidFolder to sourceFolder & possiblyValid
tell application "System Events"
	if not (exists folder corruptedFolder) then
		make new folder at end of folder sourceFolder with properties {name:corrupted}
	end if
	if not (exists folder possiblyValidFolder) then
		make new folder at end of folder sourceFolder with properties {name:possiblyValid}
	end if
	
	set theDocs to path of files of folder sourceFolder whose type identifier is "org.openxmlformats.wordprocessingml.document"
end tell

repeat with aDoc in theDocs
	-- log "dateTime before : " & (current date)
	with timeout of 10 seconds
		tell application "TextEdit" -- Edit the name of the application
			try
				open aDoc as «class furl»
				-- if we are here, the doc is valid
				close window 1
				set target to possiblyValidFolder
			on error errMsg number errNbr
				-- the dialog was displayed for 10 seconds so an error is issued
				log errMsg (*Erreur dans TextEdit : Délai dépassé pour un AppleEvent.*)
				set target to corruptedFolder
				tell application "System Events" to tell process "TextEdit" -- Edit the name of the process
					set frontmost to true
					set subRoles to subrole of every window --> {"AXDialog"}
					if subRoles contains "AXDialog" then
						tell (first window whose subrole is "AXDialog")
							-- class of UI elements --> {image, static text, static text, button, static text}
							-- name of buttons --> {"OK"}
							click button 1 --> "OK"
						end tell
					end if
				end tell
			end try
		end tell -- Text Edit
	end timeout
	(my moveFileAt:aDoc toFolder:target)
	-- log "dateTime after : " & (current date)
end repeat

#=====

on buildStamp()
	tell (current date) to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end buildStamp

#=====

on moveFileAt:sourcePath toFolder:destFolder -- here, sourcePath and destFolder are Hfs paths
	set POSIXsource to current application's NSString's stringWithString:(POSIX path of sourcePath)
	set POSIXdest to current application's NSString's stringWithString:(POSIX path of destFolder)
	set theNewPath to POSIXdest's stringByAppendingPathComponent:(POSIXsource's lastPathComponent())
	set fileManager to current application's NSFileManager's defaultManager()
	if fileManager's fileExistsAtPath:theNewPath then
		set thePathNoExt to theNewPath's stringByDeletingPathExtension()
		set stamp to my buildStamp()
		set theExtension to POSIXsource's pathExtension()
		-- insert the stamp in the file name so it will not duplicate
		set theNewPath to (thePathNoExt's stringByAppendingString:stamp)'s stringByAppendingPathExtension:theExtension
	end if
	set {theResult, theError} to fileManager's moveItemAtPath:POSIXsource toPath:theNewPath |error|:(reference)
	if (theResult as boolean) is false then
		error (theError's localizedDescription() as string)
	end if
end moveFileAt:toFolder:

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 14 juin 2020 15:37:54

Which is the name of the application process of Microsoft Word ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 15 juin 2020 12:22:56

Application process for mS Word?? I don’t understand ?

Hey the last script you sent me worked GREAT! :slight_smile:

It was 10 times faster than the one I was using!

This was so much help to me! I really appreciate you taking the time to assist me!

I deal with lots of Word documents so I am learning applescript little at a time.

This script will be a cherished tool in my toolbox!

Thanks again!

Model: Mac Mojave 10.14.6 in (Manzanillo, Mexico)
AppleScript: 2.7
Browser: Safari 604.3.5
Operating System: macOS 10.14

If you look in the script you will see these two instructions:

tell application "TextEdit" -- Edit the name of the application
--…
tell application "System Events" to tell process "TextEdit" -- Edit the name of the process

When I asked, I knew that to use Word to do the job, we must edit the first one as :

tell application "Microsoft Word"

but I was not sure of the way I must edit the second one because some applications don’t use the same spelling for the application’s name and their process’s name.
For instance the process of the application “LibreOffice” is named “soffice”.
Happily, in a personal message, I received the answer.
The instruction must be edited as:

tell application "System Events" to tell process "Microsoft Word"

If you edit the script from message #6 this way, (edit also a line at the top which contains “TextEdit”), I assume that you would be able to sort valid and invalid files with Word itself.
In my original script I wonder if TextEdit may open some documents which the true Word application fails to open. It’s why I played safety and named a subfolder “Possibly Valid”.
If the edited version works, you may rename the subfolder from “Possibly Valid” to “Valid”.

I made more tests and, with the version driving TextEdit, I was able to reduce the timeout from 10 seconds to 5 seconds. You may try to do that with the edited version.
If the original folder contain 10 files, it’s not really useful but if it contain hundreds of them it would be real time saver.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 15 juin 2020 21:28:19

I did some reading about the process name and PID (all new to me). Got this results.

tell application "System Events"
	set procesList to the name of every process whose name contains "Word"
end tell
--> "Microsoft Word"


tell application "System Events"
	set {procesList, pidList} to the {name, unix id} of (every process whose name contains "Word")
end tell
--> 18688

You were right the process name was “Microsoft Word”

I´ll try making the modifications and see what happens! (I have over 2000 recovered files to check and rename after a HD crash and recovery)

Thanks