This is my very first AppleScript. It is meant to be called from the command line and it is returning the list of processes windows and their position. It already does mostly what I need (when Universal Access / Enable access for assistive device is turned on)
I d like to initialize both allWindowsName and allWindowsPosition simultaneously.
I d also like to have my own delimiter (e.g. ### ) appended before and after each entry.
tell application "System Events"
set allWindowsName to name of window of processes whose visible is true
set allWindowsPosition to position of window of processes whose visible is true
true
end tell
return {allWindowsPosition, allWindowsName}
I managed to add a beginning and end delimiter to each outputted entry (I used the delimiters BEG and END in this example).
However I have several questions…
tell application "System Events"
set allWindowsName to name of window of processes whose visible is true
set allWindowsPosition to position of window of processes whose visible is true
-- set {T, B} to {name, position} of window of processes whose visible is true and frontmost is true
end tell
set windowList to {}
repeat with i from 1 to (count allWindowsName)
set end of windowList to "BEG"
set end of windowList to contents of item i of allWindowsName
set end of windowList to contents of item i of allWindowsPosition
set end of windowList to "END"
-- endif
end repeat
return windowList
The above code only works when Universal Access / Enable access for assistive device is turned on. Supposedly in 10.5 there s an API allowing to get the windows position without needing this Universal Access to be turned on. Apparently since 10.5 you can read back information on other windows within the same GUI sessions as the calling process using the CGWindows API defined in /System/Library/Frameworks/ApplicationsServices.framework/Frameworks/CoreGraphics.framework/Headers/CGWindow.h.
Any script example using that API to get back windows position info on 10.5 without needing to turn on Universal access would be lovely.
I tried to get back only the non-iconified windows (that is, only the windows that are really “visible”, but my definition of visible obviously is not Apple’s definition of visible but I can’t get it to work.
I tried the following
set allWindowsName to name of window of processes whose visible is true and miniaturized is false
But got an AppleScript Error: System Events got an error: Can’t make name of window of every process whose visible=true and miniaturized=false into type reference.
If someone could help me rectify this line it would be great.
You can’t read information out of frameworks with plain AppleScript.
Either you use AppleScript Studio, which is able to call ObjC methods, or you could write a Foundation Tool CLI with C or Objective C
to access the framework methods directly
With this syntax
set allWindowsName to name of window of processes whose visible is true and miniaturized is false
the whose filter refers to the processes, not to the windows.
The error occurs, because processes have no miniaturized property.
Even with assistive devices enabled I doubt, that you can retrieve all window information via System Events with an one-liner.
Some Carbon application don’t provide detailed window information, so probably you will get other error messages.
Maybe a reliable way is to check the processes one by one in a repeat loop to catch the errors