• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: System Events: window id's instead of window names?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: System Events: window id's instead of window names?


  • Subject: Re: System Events: window id's instead of window names?
  • From: Axel Luttgens <email@hidden>
  • Date: Tue, 22 Jun 2010 13:46:59 +0200

Le 22 juin 2010 à 01:40:57, Daniel Brown a écrit :

> Using System Events, is there any way to get id's of windows instead
> of just their names?
>
> I've written a little script to horizontally tile the windows of the
> current application, but if multiple windows have the same name then
> it only finds one of them and fails to tile properly. Here's the
> script:
>
>  tell application "Finder"
>      set {screenWidth, screenHeight} to {item 3, item 4} of (get
> bounds of window of desktop)
>  end tell
>
>  tell application "System Events"
>      set ws to windows of process (name of first process whose
> frontmost = true and visible = true)
>      set {width, height} to {screenWidth / (count ws), screenHeight}
>      set i to 0
>      repeat with w in ws
>          set {position of w, size of w} to {{i * width, 0}, {width, height}}
>          set i to i + 1
>      end repeat
>  end tell
>
> The problem appears to be that the windows given to me by System
> Events are identified only by name, not id—I get an error if I ask
> `get id of w` in the loop. And the reason that I'm going through
> System Events is that I want tiling to work even for non-scriptable
> applications.
>
> Is there a way to identify the windows of a non-scriptable application
> by id? Or maybe I'm just making a dumb mistake somewhere?

Hello Daniel,

Let's have two files named "a.rtf" (at differing locations on the disk, of course) and open them in TextEdit:

	tell application "System Events"
		tell application process "TextEdit"
			name of windows
				--> {"a.rtf", "a.rtf"}
			position of windows
				--> {{775, 276}, {673, 22}}
		end tell
	end tell

This one should illustrate the problem you are encountering:

	tell application "System Events"
		tell application process "TextEdit"
			set ws to windows
			repeat with w in ws
				position of w
					--> {775, 276}
					--> {775, 276}
			end repeat
		end tell
	end tell

The first question is to know whether it is really needed to build a list of window references.

This appears to be redundant, since internally System Events already has such a list.

Moreover, System Events' internal model may or may not be correctly translated into an explicit AppleScript list.

So, let's try without the intermediate list:

	tell application "System Events"
		tell application process "TextEdit"
			repeat with w in windows
				position of w
					--> {775, 276}
					--> {673, 22}
			end repeat
		end tell
	end tell

Ensuring to make use of a "direct" reference thus seems to be more reliable.

Another way would be to address the windows by index:

	tell application "System Events"
		tell application process "TextEdit"
			repeat with i from 1 to count of windows
				position of window i
					--> {775, 276}
					--> {673, 22}
			end repeat
		end tell
	end tell

Extending the above to something closer to your concern:

	tell application "TextEdit" to activate
	tell application "System Events"
		tell (first process whose frontmost is true and visible is true)
			repeat with w in windows
				position of w
					--> {775, 276}
					--> {673, 22}
			end repeat
		end tell
	end tell

Now, wrt to your id-related question:

	tell application "System Events"
		properties of window 1 of application process "TextEdit"
			--> {minimum value:missing value, orientation:missing value, position:{775, 276}, class:window, role description:"fenêtre standard", accessibility description:missing value, focused:false, title:"a.rtf", size:{475, 442}, value:missing value, help:missing value, enabled:missing value, maximum value:missing value, role:"AXWindow", entire contents:{}, subrole:"AXStandardWindow", selected:missing value, name:"a.rtf", description:"fenêtre standard"}
	end tell

Thus no property named "id" amongst a window's properties. It could well be that System Events' dictionary is somewhat cheating here. Anyway, no wonder that trying to access a window by id is just failing...

HTH,
Axel



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: System Events: window id's instead of window names?
      • From: Daniel Brown <email@hidden>
References: 
 >System Events: window id's instead of window names? (From: Daniel Brown <email@hidden>)

  • Prev by Date: Re: Folder actions and Leopard
  • Next by Date: Re: LaunchD and applescript
  • Previous by thread: System Events: window id's instead of window names?
  • Next by thread: Re: System Events: window id's instead of window names?
  • Index(es):
    • Date
    • Thread