Search code examples
pdfapplescriptacrobat

closing documents with AppleScript and Adobe Acrobat


I'm trying to use AppleScript and Adobe Acrobat Pro to convert thousands of PDFs to TXTs. My code loops through every PDF, opens it, and saves it as TXT. That part works fine: the PDFs get converted to TXTs, no problem there. What I can't figure out how to do is how to close each PDF after it's been saved as TXT. The PDFs remain open and eventually I run out of memory (it's a lot of PDFs, some of them large).

This is my code:

set sourceFolder to "Macintosh HD:Users:thiagomarzagao:pdffolder"

tell application "Finder"
    set fileSet to get every file of folder sourceFolder
end tell

tell application "Finder"
    repeat with i from 1 to (count of fileSet)
        set currentFile to (item i of fileSet) as string
        set outFile to "Macintosh HD:Users:thiagomarzagao:txtfolder:" & "newfile" & i & ".txt"
        with timeout of 6000 seconds
            tell application "Adobe Acrobat Pro"
                activate
                open currentFile
                save document i to file outFile using conversion "com.adobe.acrobat.plain-text"
                close currentFile saving no
            end tell
        end timeout
    end repeat
end tell

I get:

error "Adobe Acrobat Pro got an error: \"Macintosh HD:Users:thiagomarzagao:mypdffile.pdf\" doesn’t understand the “close” message." number -1708 from "Macintosh HD:Users:thiagomarzagao:Desktop:mypdffile.pdf"

If I replace the problematic line with close document i saving no or close every document saving no I get this:

error "Adobe Acrobat Pro got an error: document 2 doesn’t understand the “save” message." number -1708 from document 2

If I try close window 1 I get this:

error "Adobe Acrobat Pro got an error: window 1 doesn’t understand the “close” message." number -1708 from window 1

What am I doing wrong here?

Adobe Acrobat XI Pro 11.0.23, macOS High Sierra 10.13.4, AppleScript 2.7, Script Editor 2.10 (194)


Solution

  • You are mixing up the HFS string paths and Adobe document specifier which are not interchangeable.

    Acrobat has a property active doc which can be used as a reference to the front document.

    Some notes:

    • The Finder is not needed in the second part
    • Rather than newFile12 I changed the naming to use the original name
    • The script uses relative paths
    • Some PDF files are not convertible to text, I added a try block to ignore errors

    set homeFolder to path to home folder as text
    set sourceFolder to homeFolder & "pdffolder:"
    set txtFolder to homeFolder & "txtfolder:"
    
    tell application "Finder"
        set fileSet to get every file of folder sourceFolder
    end tell
    
    activate application "Adobe Acrobat Pro"
    repeat with aFile in fileSet
        set currentFile to aFile as text
        set currentFileName to name of aFile
        set outFile to txtFolder & text 1 thru -5 of currentFileName & ".txt"
        with timeout of 6000 seconds
            tell application "Adobe Acrobat Pro"
                open currentFile
                try
                    save active doc to file outFile using conversion "com.adobe.acrobat.plain-text"
                end try
                close active doc saving no
            end tell
        end timeout
    end repeat