For example if I want to know what is window behind the front/active window, how do I grab it from the window properties?
Inside an application:
tell application (path to frontmost application as text)
item 2 of (windows whose visible is true)
end tell
But AFAIK there's no way to do it if the second window is in another application. (Besides resorting to hacks like emulating the shortcut used for Move focus to active or next window.)
Related
The scenario I have is that the new windows opens, and the script executes inside it. I am looking for a way how to come back to the previous active window and did try these 2 ways:
1. Executing keyboard shortcut
tell application "System Events" to keystroke "ยง" using command down
Note: Unpleasant experience since the slight flashback as a result of window swap.
2. Giving lastWindow personal ID and later bringing it to the front
set lastWindow to id of window 1
...
set index of window id lastWindow to 1
Note: When the lastWindow is changed it becomes visible but inactive state, and requires an additional click on page to make it truly active
I did also try to change visible false of the newly created window, but it results in making it minimized and slowing the speed of background script execution.
Question. So is there a way to create new window and swap back to the last one while in a most "silent" way?
In reference to:
Note: When the lastWindow is changed it becomes visible but inactive state, and requires an additional click on page to make it truly active
The following example AppleScript code will raise the previous front most window to the top having full focus and be active. No additional click necessary.
tell application "Safari"
activate
set thePreviousFrontWindowID to id of front window
make new document with properties {URL:"https://www.google.com"}
delay 4 -- # The delay command is here as a placeholder for whatever you're doing in the new window.
set index of window id thePreviousFrontWindowID to 1
end tell
tell application "System Events" to perform action "AXRaise" of front window of application process "Safari"
It's the last line in this example AppleScript code that you need to get the window all the way to the top fully focused and active.
In reference to:
Question. So is there a way to create new window and swap back to the last one while in a most "silent" way?
Since windows don't make any sound when, for example, running the example AppleScript code above it couldn't be any more "silent" however, as far as minimizing visual distractions, that's so subjective that it almost should not have been asked. The fact is, there is always going to be some level of visual distraction as one moves though the windows of an app, manually or programmatically.
The only suggestion I have is, maybe set the bounds of the new window to that of the previous front window. Then when whatever is happening in the new and current front window is done and you bring the previous front window forward, you will see the entire window and none of the new window that's now behind it.
tell application "Safari"
activate
set thePreviousFrontWindowID to id of front window
set thePreviousFrontWindowBounds to bounds of front window
make new document with properties {URL:"https://www.google.com"}
set bounds of front window to thePreviousFrontWindowBounds
delay 4 -- # The delay command is here as a placeholder for whatever you're doing in the new window.
set index of window id thePreviousFrontWindowID to 1
end tell
tell application "System Events" to perform action "AXRaise" of front window of application process "Safari"
These are just examples to show how to get farther along then you were.
I have a script that, among other things, records the browser window it was activated on when it was launched. Things happen in the middle, and then the script needs to go back to the original window and tab it was called on.
Problem is, a user may change the active window or tab during the script's run. I want to return to the window and tab that was used when the script was called.
Here's how I'm trying (and failing) to do this:
tell application "Safari"
if (id of front window) is not (windowID of browserInfo)
display dialog "Made it in the block!"
display dialog (get index of window 1)
display dialog (get index of window (windowID of browserInfo))
-- ...
The dialogs are all for debugging, of course.
Now, browserInfo is an object whose windowID property corresponds to the Safari window where the script was called. This is usually something like '889' or '1195' or some other number.
Now, what's interesting is the first four lines fire properly when simulating a user that started in one window, then activated another. The fourth line returns '1', as expected. But the fifth line gives an error: Safari got an error: Can't get window 809. Invalid index.
How can I get the index of a Safari window when all I can use is an ID?
(And yes, URL and window title are fine things, but they are out of bounds for my application. Users may have multiple windows open with the same URL and window title. So I need the specific window ID.)
I believe this is what you're after...
on run
tell application "Safari"
set myID to id of window 1
set myTab to current tab of window 1
end tell
do shell script "sleep 8" -- simulates time where user may change tabs
tell application "Safari"
set index of window id myID to 1
set current tab of window 1 to myTab
end tell
end run
I've successfully managed to get AppleScript to manipulate the background colors in my Numbers document by refering to the background color property. However, I would like to set the borders of a selection or range of cells as well.
I've looked in the Dictionary in AppleScript Editor for a command or property that could help me out here, but I haven't found anything. Is it the case that it's not possible to create borders in Numbers with AppleScript?
There is nothing in the AppleScript dictionary to allow this functionality (which is awful, IMO, but perhaps Apple will add this in the future if enough people complain).
You can use SystemEvents to interact with the interface of Numbers to add the borders:
-- Set the selection range
tell application "Numbers"
make new document
tell front document to tell active sheet to tell table 1
set selection range to range "A3:C6"
end tell
end tell
-- Asks System Events to interact with the interface
tell application "System Events" to tell application process "Numbers"
-- Asks the main window to click on the button called 'Cell' (the name depends of your system language)
tell window 1
click radio button "Cell" of radio group 1
-- Scroll area 4 is the inspector area
tell scroll area 4
-- The first incrementor of the inspector is the borders' one.
increment incrementor 1
end tell
end tell
end tell
If you want to manipulate other elements of the interface, I recommend using the Accessibility Inspector app that comes with Xcode.
How can I set focus to a specific window of a given application using applescript?
I have several iTerm2 windows running on different displays. I want to set focus to a specified window using applescript.
I need two things, one script that collects the window ID's and prints them to stdout. I've got this:
tell application "iTerm"
set wins to id of every window
end tell
which prints 6 integers: 3034, 2528, -1, -1, -1, -1
Bonus Question: What are the four -1's ?
Then I try:
tell application "System Events"
activate window 3034
end tell
Upon which the only thing happening is that I lose focus of my current terminal (in which I am typing these commands), not matter whether I specify 3034 or 2528 as the ID.
You almost have it. You can filter out the "-1" window IDs as by only looking at visible windows:
tell application "iTerm 2"
set wins to id of every window whose visible is true
end tell
I figured this out by looking at the results of:
tell application "iTerm 2" to properties of every window
I noticed that the "-1" windows have the property visible:false
Then you can tell the window ID directly to the iTerm application instead of system events:
tell application "iTerm 2"
activate window 13195
end tell
I need some help for my appleScript.
For all open windows, I want to know which one is hidden (in the dock), which one is visible and which one is focused?
To list windows I use :
tell application "System Events"
set procs to processes
set windowName to {}
repeat with proc in procs
try
if exists (window 1 of proc) then
repeat with w in windows of proc
copy w's name to the end of windowName
end repeat
end if
end try -- ignore errors
end repeat
end tell
return windowName
I tried focused property:
copy w's focused to the end of windowName
and selected property:
copy w's selected to the end of windowName
But this doesn't work!
Thanks for help!
On Mac OS X 10.6 (AppleScript 2.1.2) the description property of a window of an application process (in System Events' terms) is "dialog" if the window is miniaturized (in the Dock), and some other value (such as "standard window", but can be something different depending on the application) if it's not miniaturized.
When an application is hidden altogether (using cmd+H or the "Hide" command from the application's menu), all of its windows will be hidden, regardless of whether they were miniaturized or not, so to find out whether it's hidden, use
visible of application process "<ProcessName>"
It's false when the application is hidden. To un-hide it, set that property to true.
To find out which window of an application is currently focused (frontmost/active), use
window 1 of application process "<ProcessName>"
The list of windows of an application (returned by windows of application process...) is ordered by the vertical stack: the frontmost window is first, the one behind it is second, and so on.
Since in OS X only one application is frontmost at a time, and only one window is in the foreground, you'll get at the currently focused window like this:
window 1 of (first application process whose frontmost is true)