1

I am looking for a way to batch-convert/bulk-convert .doc files into .docx files. I found this TidBITS Talk that references a Redit thread for an AppleScript (AS) and a dead DropBox link for an Automator integration of mentioned AS in a WorkFlow.

However, when I run the script in Automator or Script Editor I get a Syntax Error - Expected end of line, etc. but found identifier. Inside the script compatibility is highlighted (as being the issue, I guess).

What can be done to correct the error?

Find the new version as suggested by @CJK of the entire script here below:

property theList : {"doc"}

on run {input, parameters}
        set output to {}
        tell application "Microsoft Word" to set theOldDefaultPath to get default file path file path type documents path
        repeat with theDoc in input
        tell application "System Events"
            set theFolderPath to the path of theDoc's container
            set theExtension to name extension of theDoc
            set theName to name of theDoc
        end tell
        if theExtension is in theList then
            set l to the length of theName
            set exl to the length of theExtension
            set n to l - exl - 1

            set theFilename to (text 1 through n of theName) & ".docx"

            tell application "Microsoft Word"
                set default file path file path type documents path path theFolderPath
                open theDoc
                set theActiveDoc to the active document
                save as theActiveDoc file format format document file name theFilename without maintain compatibility
                close theActiveDoc
            end tell

            set end of output to the POSIX path of (theFolderPath & theFilename)
        end if
        end repeat

        tell application "Microsoft Word" to set default file path file path type documents path path theOldDefaultPath
        return output
end run

Find here below a screenshot of Script Editor after compilation without the word compatibility (trying to compile the code with this word gives me the same error message as the one at the beginning of this post): Screenshot of Script Editor after compiling the code.


System used:

  • macOS Catalina 10.15.5
  • MacBook Pro (13", 2019, 2.8 GHz Quad-Core Intel Core i7, 16 GB RAM, Intel Iris Plus Graphics 655 1536 MB)
  • Microsoft Word Version 16.39 (20071300)
  • Script Editor Version 2.11 (208)
  • Automator Version 2.10 (492)
19
  • For one, you've nested multiple tell application blocks inside each other. That's asking for trouble. Separate them out into distinct blocks. It may or may not solve your issue, but it'll definitely prevent other ones from arising that might otherwise. – CJK Aug 13 '20 at 23:24
  • @CJK: thanks for the suggestion. Unfortunately, I am not a programmer and I don't know how to do that. I found the code on the web and just tried to use it. Any suggestions? – pdeli Aug 14 '20 at 12:32
  • Here you go: https://paste.ee/p/5Zebi – CJK Aug 14 '20 at 23:07
  • A quick way to diagnose the problem will be seeing how Script Editor renders the script just after you compile it (by pressing ⌘⃣ + K⃣ or clicking the spanner button). If you screenshot your Script Editor window just after you compile the script, being sure to get the entire script in view, you can add the screenshot to your post by clicking the edit button. I have a suspicion what the problem might be, but seeing the compiled script will tell me for sure. – CJK Aug 14 '20 at 23:12
  • @CJK: thank you for the script. I pasted it into Script Editor, however when I run it (or try to compile it) the error remains the same as with the multiple-nested tell application: "Syntax Error, Expected end of line, etc. but found identifier." and the word compatibility is highlighted. – pdeli Aug 15 '20 at 23:00
0

You want folders… I got your folders right here…

To use, select a folder and run the script.

Notes: I used Word to open each document as that made it easier to match file with container, which should also reduce issues with how long it takes to open multiple files. There isn't any error correction so if your selection isn't a folder….

-- Save all 'doc' within folder structure as 'docx' in same folder
-- Create list of each 'doc' file and its containing folder
set fKind to "Microsoft Word 97 - 2004 document"
tell application "Finder"
    set topFolder to selection as alias
    set refList to a reference to entire contents of topFolder
    
    set bList to {}
    set docList to every item of refList whose kind is fKind
    repeat with ea in docList
        set c to container of ea as alias as text
        copy {name of ea, c} to end of bList
    end repeat
end tell

-- Open each 'doc', save in same folder, close doc
tell application "Microsoft Word"
    set oldDefaultPath to get default file path file path type documents path
    
    repeat with b in bList
        -- b in bList example {"filename.doc", "Mac:Users:username:Documents:docFolder1:deepFolder2:"}
        set sName to first item of b
        set sPath to second item of b
        set default file path file path type documents path path (sPath)
        
        open sPath & sName
        save as document sName file name (sName & "x") file format format
        close document 1 saving no
        
    end repeat
    set default file path file path type documents path path oldDefaultPath
end tell
4
  • Hello Mockman. Thanks for the script. However, when I try it (probably the wrong way), nothing happens. What I did: 1. paste your script in Script Editor; 2. Click on the spanner to check that it compiles correctly; 3. Go to the Finder and click on a folder; 4. Click Play in Script Editor. I also tried to save it as a script and run it, but nothing. Finally, I tried to save it as an Application, but here I get a Can’t get «class ects» of «class appf» "DocToDocx.app" of «class cfol» "Documents" of «class cfol» "pit" of «class cfol» "Users" of «class sdsk» of application "Finder". error. – pdeli Aug 22 '20 at 12:45
  • I also tried Automator. Here's what I did: 1. Create a new document in Automator; 2. Ask for Finder Items; 3. Set Type: Files; 4. Check Allow Multiple Selection; 5. Run AppleScript; 6. Paste your code; 7. Run. Whether I select only one file or multiple ones, I get a The action “Run AppleScript” encountered an error: “Finder got an error: Can’t get entire contents of application file "DocToDocx.app" of folder "Documents" of folder "XX" of folder "Users" of startup disk.” – pdeli Aug 22 '20 at 12:53
  • Offhand, the cause is likely that the selection at the time of execution is actually the 'app'. Note, in your first comment, that the 'class' of the first two items are not 'cfol' (or folder). In your second comment, the error specifies that the 'app' is the target. While running from Script Editor/Automator, you should be able to simply select a folder then switch and run the script. While testing in SE, you could try using 'choose folder' to set 'topFolder'. Automator should be set to 'ask for type 'folders''. – Mockman Aug 22 '20 at 19:51
  • Dear Mockman. Thanks for your comment, although I'm not sure I understood everything. I think that for the moment I'll go for Automator only as it seems easier to me than SE. Here's what I did: 1. Create new Workflow; 2. Ask for Finder Items; 3. Set Type: Folders; 4. Run AppleScript; 6. Paste your code; 7. Click Run. Then I choose the folder that contains 11 .doc files, the workflow completes, but no file has been converted (nothing happens in the folder). Although Word was not launched before running the workflow, it appears in the Dock once it has finished running. – pdeli Sep 2 '20 at 15:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .