Jack
Jack

Reputation: 21193

Access window by document filename

I don't do much programming in applescript but I have a personal app which is mostly python but generates simple applescripts and calls them via a system call. Applescript is so different from the languages I usually program in that I can't figure out how I...

get the window order of a document within an application?

For making calls like:

set bounds of **first** window to %s

in other words, how can I get the document's "window order" for an application?

Is it possible to interact with a window through accessing the document like this:

to get bounds of first window whose document is "%s"

(which doesn't work) or do I have to get the document's window order first and then interact with that window (via its order) in a second line?

Any insight would be great. Thanks.

Upvotes: 2

Views: 481

Answers (1)

Antal Spector-Zabusky
Antal Spector-Zabusky

Reputation: 36622

You can do both of these things just fine. The first line is just set bounds of window 1 to ..., or, if you prefer, set bounds of the first window to ... The second one depends on what, exactly, you want to do. If you want to access the first window whose name is something in particular, you can just do get the bounds of window "NAME"; if you really want the name of the document, though, you'll need to do something like

set d to the document "NAME"
repeat with w in windows
  if w's document is d then return bounds of w
end repeat

You should be able to do the first window whose document is d, but this fails; as far as I can tell, it's because document is also a type name. Also, if window "NAME"/document "NAME" fails—it's the sort of thing that I remember sometimes not working, even though it should—you can instead use the first window whose name is "NAME" (or the first document ...). But the simple form will almost certainly work.

Also, if you're just generating these AppleScripts, calling them, and deleting them—in other words, if you're pretending they're Python functions, rather than generating them for later use—I'd highly recommend using appscript instead,. I've never used in in Python, but I have in Ruby, and it's a really great way to deal with everything AppleScript does while still using your language of choice. For instance, I think your first example would become app('Whatever').windows[1].bounds.set((0,0,0,0)), (or ...windows.first.... if you prefer) and your second would become either app('Whatever').windows['NAME'].bounds.get() or app('Whatever').windows[its.document.name == 'NAME'].get(), depending on if you need the window's name or the window's document's name. This is all untested, but certainly captures the flavor of what appscript tends to look like (nice and concise).

Upvotes: 3

Related Questions