I am working on an applescript in Pages which requires scripting by using “System Events” with scripting control through use of the GUI.
I have a working script but would like a better way to do it. The code fragment I am tying to clean up is:
tell sheet 1 of window "untitled"
tell radio group 1 to click radio button "RTF"
click button "Next..."
end tell
The part I would like to change is “window ‘untitled’” to something like “front window” or even “window 1” so that if another document is open and named untitled the script will still work.
How do I reference the window without using it’s title? Also, has something changed recently with GUI scripting, because some reference code I have been using refers to the window as
“window 1” and doesn’t use the window’s title?
Thanks
-Jonathan
Model: iMac G4 17"
AppleScript: 1.10.3
Browser: Safari
Operating System: Mac OS X (10.4)
It looks like something may have broken in the retail version of Pages, Jonathan - since a reference to ‘window 1’ works with many other apps. I don’t use Pages myself, so I’m not sure I can really help here - but here are a few thoughts, FWIW. (Apologies if you’ve already tried them.)
One thing I’d try is to extract the window name(s) first. From your description, these suggestions could well error - although they might still be worth a go:
Through System Events:
tell application "System Events" to name of window 1 of process "Pages"
tell application "System Events" to item 1 of (get name of windows of process "Pages")
Directly from Pages:
tell application "Pages" to name of window 1
tell application "Pages" to name of document 1
If none of those work, I’d try looking in Pages’ AS dictionary to see if another application property has been added, á la Microsoft - such as ‘active window’ or ‘active document’.
And if all else fails, you may want to consider filing a bug report…
Thank you kai for the idea of just grabbing the front most window’s name. The code took some work, but this is what I came up with:
tell application "Pages"
activate
open thisfile
end tell
--Grab the name of the last window opened, second item is link.
tell application "System Events"
set front_window_name to second item of (get name of windows of process "Pages")
tell process "Pages"
set frontmost to true
click menu item "Export." of menu "File" of menu bar 1
tell sheet 1 of window front_window_name
tell radio group 1 to click radio button "RTF"
click button "Next..."
end tell
end tell
end tell
For some reason if I grab the window name after I activate and open a Pages file, the first item is link and the second is the window name.
The set frontmost to true statement is also needed because I guess the focus shifts when I grab the window name, so need to set the Page window back to the front window.
Thanks all for the help.