Re: Traversing windows with Cocoa
Re: Traversing windows with Cocoa
- Subject: Re: Traversing windows with Cocoa
- From: Bill Cheeseman <email@hidden>
- Date: Tue, 04 Mar 2008 06:33:00 -0500
- Thread-topic: Traversing windows with Cocoa
on 2008-03-03 5:45 PM, aldo kurnia at email@hidden wrote:
> UI scripting is exactly what I've tried to do and failed. For example, you'd
> expect the following script to work....
No. Your script doesn't work because it doesn't use GUI Scripting correctly.
At the end of this message I've given you two scripts that accomplish your
goal.
To use GUI Scripting, you must address the entire script to the System
Events application and then, perhaps in a nested 'tell' block, to each
targeted application 'process' in turn. In GUI Scripting, a 'process' is an
object in the System Events application's terminology dictionary. You can't
tell another application to use the 'process' object returned by System
Events, and as you discovered you can't use standard AppleScript commands
relating to a 'window' object with every application because not all
applications implement an AppleScript 'window' object.
Also -- this is a completely separate issue -- it generally isn't valid in
AppleScript to use a variable containing the target application's name in a
'tell' statement. That is, 'tell application appName' generally won't work
(although it's more likely to work now than it was in the old days), whereas
'tell application "TextEdit"' will work. As you can imagine, this limitation
of AppleScript makes it difficult to cycle through multiple applications if
you don't know their names at the time you're writing the script. With the
System Events 'process' object you can avoid this issue.
Here's a script that returns the name of every window of every running
application. I limited it to every running 'application process', rather
than every running 'process', to avoid returning information about
background processes, even though some of them sometimes have windows.
tell application "System Events"
get name of every window of every application process
end tell
You'll notice if you run this script that there are many unnamed windows
open, not all of which are visible on screen.
Here's a somewhat more complex script that does exactly what you're trying
to do, written so as to spell out the proper technique:
set winName to "Untitled"
tell application "System Events"
activate
repeat with thisProcess in processes
repeat with thisWindow in windows of thisProcess
if name of thisWindow as string is winName then
display dialog winName & " - " & name of thisProcess
return
end if
end repeat
end repeat
end tell
Here's a much simpler and faster version of the same script:
set winName to "Untitled"
tell application "System Events"
activate
get first process where name of windows of it contains winName
if result is not {} then display dialog winName & " - " & name of item 1
of result
end tell
As written, these scripts only return the first instance of window
"Untitled" that they find.
GUI Scripting is very powerful. However, like everything having to do with
AppleScript, it has a learning curve.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
www.quecheesoftware.com
PreFab Software - www.prefabsoftware.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden