I have the following script. It seems as though it is only completing the ocr. Not the compression. Can someone help?
[code]tell application "PDFpenPro"
    -- Open the incoming file
    open theFile as alias
    -- Check if the document needs OCR
    set needsOCR to needs ocr of document 1
    if needsOCR then
        tell document 1
            -- Perform OCR
            ocr
            repeat while performing ocr
                delay 1
            end repeat
            delay 1
            -- Compress the PDF after OCR
            create optimized PDF
            delay 1
            -- Save and close
            close with saving
        end tell
        -- Quit PDFpenPro if no documents remain
        if name of window 1 is "Preferences" then
            tell application "PDFpenPro" to quit
        end if
    else
        -- Document already has text layer or OCR not needed
        tell document 1 to close without saving
        -- Quit PDFpenPro if no documents remain
        if name of window 1 is "Preferences" then
            tell application "PDFpenPro" to quit
        end if
    end if
end tell
/code]