Why does AppleScript “exists” sometimes always return “false”? (part of question regarding checking documents in Pages)
-
25-05-2021 - |
Question
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
Solution
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
OTHER TIPS
A slightly different approach. It opens the selected Pages file in the Finder, which will launch Pages. It then activates Pages, waits a moment and tests for busyness. It then checks that the open doc matches the selected file and if they are the same, exports a pdf version to the same folder, and closes the pages doc.
tell application "Finder"
set oFil to selection as alias -- get alias of selected file
set oFilT to oFil as text -- get file of selected file
set oNam to name of oFil -- get name.extension of file
if kind of oFil is "Pages Document" then
open oFil
end if
end tell
tell application "Pages"
activate
delay 1
end tell
set pagApp to path to application "Pages" -- App to test
tell application "System Events"
if busy status of pagApp is true then -- Test busy status of Pages
say "Busy"
else -- if not busy then…
tell application "Pages"
set dFilT to file of document of window 1 as text -- get file of document 1
if oFilT = dFilT then -- is Finder selected file same as Pages open document
set dNam1 to name of document 1 -- get name without extension
tell application "Finder" to set xFil to (container of oFil as alias as text) & dNam1 & ".pdf"
export document dNam1 to file xFil as PDF with properties {image quality:Best}
end if
close document dNam1
end tell
end if
end tell