Getting the *front* window in Excel for OS X

I’m trying to get the name/path of the true front window in Microsoft Excel for OS X. I try:


return document 1 of application testapp

Which doesn’t seem to work with multiple documents. I think it’s grabbing the 1st created document. Any ideas out there?

Thanks.

A partial answer to my own question…


return selection of application "Microsoft Excel"

This yields: Range “R31C6” of Worksheet “Sheet1” of Workbook “new.xls” of application “Microsoft Excel”

But now how do I access the “new.xls” entry?

Also, I might run into trouble if there are two documents with the same name.

Does this work?

return front document of application testapp

– Rob

Have you tried…

return name of window frontmost of application "appname"

or you can get the properties of a window…

get properties of window frontmost of application "Finder"

I tried the suggestions of the two previous replies and they do not work.

I think this “selection” may be the way to go…but I can’t figure out how to extract info from it. Anyone know how?

This is not a great solution since it requires GUI scripting but it does work:

tell application "System Events" to set the_name to name of window 1 of process "Microsoft Excel"
tell application "Microsoft Excel"
	repeat with i from 1 to count of documents
		if Name of document i = the_name then return document i
	end repeat
end tell

Jon

Microsoft Excel has a substantial dictionary, but doesn’t follow the standard AppleScript Object Model - it seems more Visual Basic based, which isn’t a surprise.

As a result, the information isn’t where you’d expect it to be.

The following code will return the active/frontmost window:

tell application "Microsoft Excel"
	get ActiveWorkbook
end tell

As for where you’d find this command in the dictionary, check Custom Suite → Application and you’ll see ‘ActiveWorkbook’ as a property. Technically, of course, this is an application property but it’s not in the standard suite for some reason.