Why does AppleScript “exists” sometimes always return “false”? (part of question regarding checking documents in Pages)

applescriptfinderpagespdf

I'm new to AppleScript and I've reached this problem after several other problems and work arounds (see further down for more details). My current problem is that I'm telling Pages to check if any document is open with

tell application "Pages"
    exists front document
end tell

but sometimes even when Pages has a document open the "exists" is always "false". And in general, it seems that sometimes, any reference to "front document" returns errors that it can't get it when it is clearly there. When I quit Pages and try again, usually it works…until it doesn't.

Any ideas?

My original problem stems from wanting to make a QuickAction to open a file in Pages -> export to PDF -> close Pages -> repeat as necessary. But the script gets ahead of itself and tries to complete the export before the file is even open.

tell application "Finder"
    set theSelection to selection
    repeat with oneItem in theSelection
        set finderLocation to oneItem as text
        open oneItem
        tell application "Pages"
            set idx to 0
            repeat until (exists front document)
                set idx to idx + 1
                if idx > 2000 then quit
            end repeat
            set pagesLocation to file of front document as text
            repeat until finderLocation = pagesLocation
                set pagesLocation to file of front document as text
            end repeat
            set exportFile to file of front document as text
            set exportFile to text 1 thru -6 of exportFile
            set exportFile to exportFile & "pdf"
            export front document to file exportFile as PDF with properties {image quality:Best}
            close front document without saving
        end tell
    end repeat
end tell

So I've been trying to find a way to check if the document is open first before continuing. And I came up with this, to check if the front document file location in Pages matches the file locations opened by Finder. This worked until I tried from a fresh instance of Pages (quit Pages first then try). The problem there was that Pages would flag an error for not being able to find "front document" at all (since it needed to read its file path but nothing was open yet).

So now I'm here trying to check if ANYTHING is open before checking if the target file is open. But now, as described above, it seems that references to "document" just breaks and causes either "exists" or "file of front document" to hang.

Am I even headed in the right direction here or is there a better way to do this?

Thanks in advance.

EDIT:
now my script looks like this, and seems to be more consistent, at least so far

tell application "Finder"
set theSelection to selection
repeat with oneItem in theSelection
    set nameOfDocument to name of oneItem
    open oneItem
    tell application "Pages"
        repeat until (exists document nameOfDocument)
            delay 0.1
        end repeat
        set exportFile to oneItem as text
        set exportFile to text 1 thru -6 of exportFile
        set exportFile to exportFile & "pdf"
        export document nameOfDocument to file exportFile as PDF with properties {image quality:Best}
        close document nameOfDocument without saving
    end tell
end repeat
end tell

Best Answer

This following AppleScript code should work for you. Ultimately, this code sets a variable for the count of open documents in Pages (openDocuments) Then if the count of openDocuments is not 0, the names of the open documents are stored in this variable openDocumentNames

if application "Pages" is not running then
    tell application "Pages" to activate
    tell application "System Events" to tell application process "Pages"
        repeat until frontmost
            delay 0.1
        end repeat
    end tell
end if

tell application "Pages"
    set openDocumentsRef to a reference to documents
    set openDocuments to count of openDocumentsRef
    if openDocuments is not 0 then
        -- Gets The Name Of The Open Files
        set openDocumentNames to name of openDocumentsRef
        -- Gets The File Paths To The Open Files
        set openDocumentsPaths to file of openDocumentsRef
    end if
end tell

-- Shows & Selects The Files In A New Finder Window
tell application "Finder" to reveal openDocumentsPaths