I’m having a very hard time figuring out how to open a specified OpenDocument Text (.ODT) file in Word via AppleScript. I have no trouble opening the file from a Choose File dialog, but I can’t automate it. This always produces errors:
set myDoc to "/Users/myname/Desktop/Sample.odt"
tell application "Microsoft Word"
activate
open file name myDoc file converter open format auto
end tell
I’ve tried many variations on this; without the file converter parameter, Word tells me I need to grant privileges to open the document. With it, I get a message from Word saying that the file is corrupt. But the file opens in Word when double-clicked or when I use “Choose File” to select it.
Does anyone know the correct syntax for the “open” command that will solve this? Thanks for any help
This seems to work for opening the file in Word, but I wonder if there is a some “run script” line that would get the whole job done within Word, so that I can open the file, then save it in a different format more straightforwardly:
set myDoc to "Macintosh HD:Users:edward:Desktop:Sample.odt"
set theApp to POSIX file "/Applications/Microsoft Word.app" as alias
tell application "Finder"
open myDoc using theApp
end tell
And while I’m asking, is there a reliable way to get the address of Microsoft Word, or will it always (even in international systems) be in “/Applications/”?
To continue answering my own question, with the help of various posts on this forum, I figured out this extremely messy and inefficient method. There must be a better way!
set myDoc to "Macintosh HD:Users:edward:Desktop:Sample.odt"
tell (info for myDoc as alias) to set {Nm, Ex} to {name, name extension}
set baseName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
set wordApp to POSIX file "/Applications/Microsoft Word.app" as alias
tell application "Finder"
open myDoc using wordApp
end tell
delay 1
tell application "Microsoft Word"
set inTime to current date
repeat
try
set namesList to name of documents
if baseName is in namesList then
exit repeat
end if
end try
if (current date) - inTime is greater than 20 then
display dialog "Timed out"
exit repeat
error number -128
end if
delay 1
end repeat
set active document to baseName
save as active document file name "/Users/edward/Desktop/Output.docx" file format document
close active document
set namesList to name of documents
if (count of namesList) is 0 then quit
end tell
set myDoc to "Macintosh HD:Users:edward:Desktop:Sample.odt"
set wordApp to path to application "Microsoft Word"
tell application "Finder"
open file myDoc using wordApp
end tell
That’s the way. Thank you!