Control several Browsers with Applescript - browser

Is there any way to control 2 different safari browser windows with Applescript at the same time und switch between them? Something like a unique ID to be sure which commands are for which safari window.
something like...
tell application "Safari" id 1
activate
....
end tell
:-)
thanks for your help.-

Windows in AppleScript are code objects, so if you make a reference to one, e.g.:
tell application "Safari"
set aWindow to window 1 -- sets a reference to the current front window
end tell
AppleScript will remember that particular window even if you move it, hide it, put it in the background, minimize it... Until you reset the variable or close the window, you can be sure that aWindow will point to that particular window. In fact, this works the same for tabs. You can say:
tell application "Safari"
set aTab to tab 3 of window 1 -- sets a reference to the third tab of the current front window
end tell
and even if you move that tab to a different window, AppleScript should still track it. Likewise, if you have a window pointing to the Google Search engine, you can say:
tell application "Safari"
set aWindow to window "Google" -- sets a reference to the Google window
end tell
and then if you navigate to a different webpage, the variable aWindow will still point at the same window. Neat, huh?
So all you need to do is create a couple of variables for your two windows, and you can switch back and forth as you like:
tell application "Safari"
-- assuming you have a 'Google' window and a 'Yahoo' window open
set firstWindow to window "Google"
set secondWindow to window "Yahoo"
set URL of document of firstWindow to "https://stackoverflow.com"
set index of secondWindow to 1
set miniaturized of firstWindow to true
end tell

Windows are identified by their absolute Id. You can see ID's with :
tell application "Safari" to set myWindow to every window
It gives you list of all open windows of Safari like :{window id 4519, window id 4426, window id 4514}. There are 3 open windows !
However to switch between 2 Safari windows, you must use their number: 1 is the frontmost, 2 is behind, 3 is behind the 2,...
You can tell the System Events to make number 2 before number 1. Doing so, old 2 becomes 1 and the 1 becomes 2.
tell application "System Events" to perform action "AXRaise" of window 2 of process "Safari"
if you just have 2 windows, the line above will keep switching between your 2 Safari windows, each time making the second window the frontmost one.

Related

Applescript - Repeat until static text is not equal to value

I literally only started using Applescript today trying to write macros for an audio program I use so I really don't know a lot and am stumbling through this.
All I need to do is monitor when a certain window is open within the software and then most importantly, when it closes.
When I export a file from my software, a window comes up image of window The window has no identifier other than the static text "Bouncing..." In the screenshot there is a cancel button, but this doesn't apply to every instance of the window (depends on what options are selected) so it's the only thing I have to work with.
So far I have this:
tell application "System Events"
tell process "Pro Tools" to set myvalue to value of every static text of front window
repeat with i from 1 to count of myvalue
if (item i of myvalue = "Bouncing...") then repeat
end repeat
delay 1
end repeat
end tell
Testing this with a popup instead of the repeat stating it has found the static text works. So I'm halfway there.
All I need to do is to repeat this entire process over and over until the window disappears. Obviously when "myvalue" is "Bouncing..." I need to go back to the start, collect the static text values from the window again and then only exit the loop when the static text value of "Bouncing..." doesn't exist.
Currently it seems to exit the loop before the window has closed
Thanks for any help given here, I've spent hours on this and I'm just not knowledgeable enough to figure it out.
Found another way around it. It seems it does default to "window 1":
try
tell application "System Events"
repeat while (value of first static text of window 1 of application process "Pro Tools" = "Bouncing...")
delay 1
end repeat
end tell
end try

How can I detect whether something is selected with AppleScript?

I'm using some software called controllermate with allows you to customize the behavior of buttons on a keyboard or mouse. One of the 'building blocks' you can use in a particular function in an apple script. I would like to create a custom behavior for my mouse such that a certain button will execute CMD+C if something that can be copied is currently selected, but otherwise execute a different keyboard shortcut. It seems I will have to use an appleScript in order to determine whether text is selected, but after reading other some other people's solutions online I was unable to figure out how to implement it myself. Thanks for any help.
Here's a solution:
Put an empty string to the clipboard, do CMD+C, check the clipboard.
set the clipboard to ""
tell application "System Events" to keystroke "c" using command down
delay 0.2
set b to false
try
set b to (the clipboard as string) is not ""
on error -- error when the clipboard contains a custom type (like a copy in the Photos Application)
set b to true
end try
if b then -- the clipboard does not contains an empty string
-- *** script to execute a different keyboard shortcut ***
--tell application "System Events" to keystroke someChar using someModifier
end if

Focusing Issue in Clarion 6

We have an application in Clarion 6 which does not have any mdi windows in it anywhere. These have all been removed and made non-mdi. When this one window is open and other windows are opened off of it (say from a button click), the first window will 'pop out' in front of the other windows when the items are refreshed based on a timer.
How can this be stopped? We do not want this window to get the focus and cover other windows automatically this way. However, the display data does need to refresh every so often.

How to prohibit user to change OS window focus/type keys for several seconds on Linux

I write automated tests for a website. One of the tests needs to press Ctrl+S and type some letters to save a webpage including CSS/JS. The test should require Firefox to have window focus for those several seconds to be able to type the filename and click "Save" (currently XDoTool is used for pressing keys and clicking the mouse).
Sometimes I run the tests on my computer. I don't want to occasionally type something or change the window focus while the page is being saved, so I want to prohibit changing focus for those several seconds when those mouse clicks/key presses are running.
It's not possible to do it at the Webdriver level (a tool that I use for controlling the browser), so I think I should do it at the OS level. Can I prohibit the user from changing window focus or typing anything for a period of time? It may look like:
prohibit user from typing keys/change window focus
save page
allow user to type/change window focus
I guess that another way to achieve the desired effect would be to run Firefox and your automated tests on a nested X server (xf86-video-nested/Xephyr/Xnest).
$ Xnest :1
$ export DISPLAY=:1.0
$ firefox http://something/to/test &
$ your-test-script
Since the nested X server has it's own idea of the focused window, moving the focus on your desktop shouldn't affect it.
(If you don't actually need to see the test progressing, you could use a dummy X server (xf86-video-dummy/Xvfb/Xfake) instead)

How do I make a window move to the top of other windows in Gnome when that window already has the focus?

I have an application that sends the focus to other windows but those windows then don't automatically display themselves in the foreground, i.e. on top of all the other windows. Where can I configure the preferences of my window manager so that this is the default behaviour?
In particular I'm using the Ctrl-0 and Ctrl-Shft-0 shortcuts in the MATLAB IDE to move between the command window and the editor window and although the focus seems to be transferred the new window doesn't automatically redraw itself in the foreground.
Not sure of a key binding off hand that does it, but if you alt-click on a window (which allows you to drag a window) it should come to the front.
As codeDr suggests, MATLAB is also kind of bad about repainting its windows. If you draw to a figure while code is executing, the figure does not update unless you execute drawnow or have some similar pause in the execution to allow the GUI to repaint. Since we're talking about MATLAB, the figure command will also cause the indicated figure to come to the front (in fact, it's harder to get it to not come to the front). So you could do figure(gcf) to bring the current figure to the front, or save the figure number with h = figure; and then later do figure(h). Incidentally, if you want to switch current figures without switching focus, set(0, 'CurrentFigure', h) should set h to the current figure.
Your window manager (probably Metacity?) implements focus-stealing prevention so that rogue apps don't pop up windows that would disturb your typing. Matlab needs to raise its window, and give it the input focus with the correct timestamp. If this is being done from a KeyPress event handler, the timestamp for setting the input focus would be the timestamp from the KeyPress event (i.e. the timestamp of the user-generated event that caused a window to be raised/focused).
To politely give the input focus to a window, google for _NET_ACTIVE_WINDOW.
Usually when the window doesn't repaint, it means that the application's main application loop isn't running to refresh the window. Could it be that Matlab is doing some computation or disk activity when you are switching between windows?

Resources