Mr. C
Mr. C

Reputation: 1710

Applescript - Close Pages Document Automatically

I am trying to create a script that will automatically close the frontmost window of Apple Pages.

    on run {}
    tell application "System Events"
        if (window 1 of process "Pages" exists) then
            try
                tell application "Pages"
                    --display dialog "Hello World!" --TODO: remove, test code only.
                    --Keywords I have tried: file, document, window, 
                    close window 1 saving no
                end tell
                --close window 1 of process "Pages" saving no
            on error errMsg
                display dialog "ERROR: " & errMsg
            end try
        end if
    end tell
end run

Whenever I run this, it gives me the following error:

ERROR: Pages got an error: window 1 doesn’t understand the “close” message.

I have looked at this article, and have used the following command:

sudo defaults write /Applications/Pages.app/Contents/Info NSAppleScriptEnabled -bool YES

However, it still fails to work. Any advice?

Details:
System Version: OS X 10.9.1 (13B42)
Kernel Version: Darwin 13.0.0
Pages: 5.0.1

Upvotes: 0

Views: 1308

Answers (1)

CRGreen
CRGreen

Reputation: 3429

If Pages isn't scriptable, you're kind of out of luck. If it were scriptable, you wouldn't need System Events to close a window; that kind of functionality is usually included in a scriptable app's dictionary.

System Events can help with apps that aren't scriptable, but you have to rely on the actual UI. But if that's the solution, you can't use tell application "Pages" for the inner block (like you have it); you have to use:

tell process "Pages"

If you go that route, now you have to use either the close button on window 1 or use the close menu command. Something like:

activate application "Pages"--note that this will probably be NECESSARY (if it's not frontmost, it prob won't work)
tell application "System Events"
    tell process "Pages"
        click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1 of it
    end tell
end tell

BUT then you have to come up with the problem of what happens if the window hasn't been saved (has been modified) -- which in a scriptable app uses the construction you were originally trying. When using System Events, you can do:

activate application "Pages"--note that this will probably be NECESSARY (if it's not frontmost, it prob won't work)
tell application "System Events"
    tell process "Pages"
        click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1 of it
        delay .5
        keystroke "d" using command down
    end tell
end tell

But then again how do you make the script smart enough to know if the window has been modified or not? Or maybe you use System Events to see if the window has been killed after the close command, and if it hasn't, it does the keystroke thing. This kind of thing can be done by doing something like:

activate application "Pages"
tell application "System Events"
    tell process "Pages"
        set frontMostWinName to name of window 1
        click menu item "Close" of menu "File" of menu bar item "File" of menu bar 1 of it
        tell me to delay 0.5
        if exists window 1 then
            if name of window 1 = frontMostWinName then keystroke "d" using command down
        end if
    end tell
end tell

I don't have Pages, but this works with another non-scriptable app, Bean (although I should mention that Bean uses tabs, and I had to move a tab to a window to get this to work*, and I don't know how Pages works in this regard). [EDIT: *actually, this is not true; this works in Bean regardless of tabs/windows]

Upvotes: 2

Related Questions