1

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
  • If you can stand to wait during every loop, you could put a 3-5 second delay in there to wait for the document to open. It would simply be delay 5 – Chris Norman Dec 4 '19 at 21:43
  • @ChrisNorman Yes I am aware of "delay". I could stand to just put a delay, but the purist in me wants to avoid it and hunt for a better way. – j33205 Dec 5 '19 at 0:07
  • @wch1zpink thanks good note. But there is actually only 1 repeat until command, the rest are commented out (forgot to remove them). I'll take them out here for clarity. – j33205 Dec 5 '19 at 1:44
0

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
  • This seems promising. Like I said I'm new to AppleScript (like as of yesterday) so the idea of "documents" vs "document" is new to me and is also very helpful. What is the purpose of "reference" buffer just to count the docs? – j33205 Dec 5 '19 at 0:31
  • When you set a variable to reference to some object… every object has properties for example some objects may have properties like these…(name, date modified, id, etc) and you can retrieve those values and Store the information in additional variables. for example set objectRef to a reference to someObject. then you could do something like set dateModified to date modified of objectRef I'm not explaining this very well because for me, the reference to operator trips me up sometimes, but for your needs, it works – wch1zpink Dec 5 '19 at 0:49
  • I usually start playing around with the reference to operator when I'm testing my code and I get an error number -1728. Also you mentioned "documents" vs "document". Sometimes AppleScript is pretty flexible with wording. I would have gotten the same results if I had used "every document" instead of "documents" – wch1zpink Dec 5 '19 at 0:59
  • thanks for the clarification, still a bit weird in my head. Anyway -> I still am running into the problem of "documents" or "count of documents" being 0 when I have documents open. Very weird. But I'm moving toward use of the name of the document directly to check if it is open and waiting until it is. This seems a little more robust, for now... – j33205 Dec 5 '19 at 1:00

You must log in to answer this question.

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