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.
Related
Actually I have 3 questions about the same problem: controlling a window with applescript.
What should I do if I would press on button "Close Window" of application "Google Chrome"?
Is it possible to check if the window changes? For example, to see if appear a pop-up or something like that...
What about clicking on a specific place into a window? I mean, I know I can use
tell application "System Events"
click at {x,y}
end tell
but this command use the entire screen as reference system, and I want it works only on a specific window. For example, if at "{x,y}" i put "{1,1}", applescript will click on the first item on the menu bar. Is there a way I can say to "System Events" to click at "{1,1}", but on the window "Google Chrome"?
Here are three examples of how to close the front window of Google Chrome using AppleScript:
Note: The following assumes Google Chrome is running with at least one window open when you test each example AppleScript code in Script Editor.
Example one is the most straight forward way:
tell application "Google Chrome" to close front window
Example two directly clicks the close button:
tell application "System Events" to tell ¬
application process "Google Chrome" to ¬
click button 1 of front window
Example three calculates the center of the close button and clicks there:
activate application "Google Chrome"
delay 0.5
tell application "System Events" to tell ¬
application process "Google Chrome" to tell ¬
front window
set posB1 to (position of button 1)
set szB1 to (size of button 1)
set x to (item 1 of posB1) + (item 1 of szB1) / 2 as integer
set y to (item 2 of posB1) + (item 2 of szB1) / 2 as integer
end tell
tell application "System Events" to click at {x, y}
Note that in the first two examples, the front window of Google Chrome doesn't even need to be the frontmost window on the Desktop; however, with the third example it does, otherwise the click at {x, y} will not go to the intended target.
That said, example three really shouldn't be used when there it a straight forward way, as in example one, to get the job done. Example three was just a proof of concept to get the coordinates to click at. This method may be useful in some fringe cases, especially in an app that doesn't directly support AppleScript.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
In applescript GUI scripting you can simply refer to an element by name or index and tell it to click or to perform an action. For instance to click the close button on the first open window in Chrome you could use:
tell application "System Events"
tell process "Google Chrome"
tell window 1
tell button 1
click
end tell
end tell
end tell
end tell
You don't actually need to know its physical position to click one it; you just need to know that the first button in the window is the close button.
System Events always returns the position of any element in screen pixels, so if you want the position of an element in terms of its window, get the position of the element, get the position of the window, and do some addition or subtraction (e.g., if you want to click at {5,5} in a window whose position is {100, 125}, click at {105, 130})
AppleScript isn't really designed to monitor GUI changes, though if you want to be tricky and you know what change you're looking for you can do something like this:
tell application "System Events"
tell process "..."
tell window 1's pop up button 3
repeat until (exists menu 1)
delay 0.2
end repeat
-- menu 1 now exists, so the pop up button is open
end tell
end tell
end tell
...but note that this will hang the script until the menu is opened. A more elegant way to handle that is to write a script application with an idle handler, like so:
on run
-- whatever initialization is needed
end run
on idle
tell application "System Events"
try
tell process "..."
tell window 1's pop up button 3
if exists menu 1 then
-- menu 1 now exists
-- the pop up button is open
-- do what must be done
end if
end tell
end tell
on error errstr
display alert "Something went wrong" message "The script sent this error: " & errstr
end try
end tell
return 0.2
end idle
You can leave that running in the background watching for specific changes in the GUI (the 'try' statement is in case the app you're watching quits, the window closes, or something unexpected happens to the GUI).
If you haven't already, open the System Events scripting definition in Script Editor and look at the Processes Suite. That will show you all the things you can do with GUI scripting.
The following script will open a track in iTunes
use application "iTunes"
property trackURL : "itmss://itunes.apple.com/us/album/brahms-violin-concerto-in-d-major-op-77-iii-allegro/145533236?i=145533044&uo=4"
open location trackURL
Now, asking "iTunes" to play it does not work because the track is highlighted but not properly selected, i.e., it requires a manual mouse click to select it and play it.
How can I select the highlighted track? Or how could I ask "iTunes" to play the song?! Alternatively, is there a way to add a music to my library from an URL directly?
Disclaimer: I don't have the Apple Music subscription, so the UI on my end may not be exactly the same as yours. However, if I click the "Play" button, I get the little advertisement asking me to sign up for the service, which I assume would just play the music if you had the service. So, these are the steps I've been able to follow to get that box to pop up:
The first, and most convenient from AppleScript, thing to try is just to hit the space bar to start the music playing. This actually works great if I've selected the item manually by clicking on it. However, after open location, it doesn't work, and this appears to be because even though the row is highlighted in the viewer, the actual keyboard focus seems to be on the page itself (the iTunes Store and Apple Music appear to have their entire UI presented as web pages rendered by WebKit). You can verify this by tapping the up and down arrow keys on the keyboard; the page scrolls up and down instead of you switching to adjacent tracks.
My opinion is that this is actually a bug in iTunes; I'd consider the true solution to the problem to be to report this to Apple via the bug reporter. Using open location really should set the keyboard focus to the track you navigated to.
With that said, we can work around it in the short term by simulating a click on the "Play" button. Note that you'll probably need to add your app in System Preferences > Security and Privacy > Accessibility. Note also that this is incredibly fragile, and if Apple ever changes anything in the layout of the web pages they're serving, this whole thing will break. Finally, please note that this code is extremely ugly; the whole thing gives me hives just by looking at it, but it's the only thing I was able to get to work. Side effects of reading this code may include nausea, headaches, and suicidal thoughts. Do not read this code immediately after eating. Consult your doctor before reading this code if you have a history of depression or obsessive-compulsive disorder.
property trackURL : "itmss://itunes.apple.com/us/album/brahms-violin-concerto-in-d-major-op-77-iii-allegro/145533236?i=145533044&uo=4"
property trackTitle : "III. Allegro giocoso, ma non troppo vivace"
tell application "iTunes"
activate
open location trackURL
delay 1 -- give the page a second to load
end tell
tell application "System Events"
tell process "iTunes"
set theRows to the rows of table 1 of UI element 1 of scroll area 1 of group 1 of group 1 of front window
-- "repeat with eachRow in theRows" isn't working. I don't know why. Freaking AppleScript
repeat with i from 1 to the number of theRows
set eachRow to item i of theRows
if exists group 2 of UI element 2 of eachRow then
if value of static text 1 of group 1 of group 2 of UI element 2 of eachRow is trackTitle then
tell group 1 of UI element 2 of eachRow to click
end if
end if
end repeat
end tell
end tell
If Apple ever fixes the bug, of course, we should be able to just:
tell application "iTunes"
activate
open location trackURL
delay 1 -- give the page a second to load
end tell
tell application "System Events" to keystroke space
I am looking for a script that will place the cursor in the text field in the Messages App. I have looked for a keyboard shortcut to do this but cannot find one. Can anyone provide a script, or a similar one I can modify.
NB I am not a programmer or very familiar with AppleScript, but have been able to modify scripts that are close to my needs.
I need this as I am trying to make the messages app controllable using the built in dictation feature in Mac OS. I need a script I can assign to a voice command to place the cursor in the text field so that I can then dictate a message.
Many thanks.
If you are using dictation commands, in any application all you need to do is say the command “Show Numbers” and you will see this:
Then you would just say the command “Twenty” which will place your cursor right where you want it… in this case it would be the text field
Also speaking the command “Show Comands” Will open up this window listing tons of dictation commands.
The following was tested and works under OS X 10.8.5 and Messages 7.0.1 and may need to be adjusted for other versions of OS X/macOS/Messages:
tell application "Messages"
activate
tell application "System Events"
set focused of text area 1 of scroll area 4 of splitter group 1 of window 1 of application process "Messages" to true
end tell
end tell
Note: This is coded with the assumption that Messages is already open with an open window. Additional coding will be necessary, in the form of try and or delay and or on error statements as needed and appropriate otherwise.
Here's an example of how I'd code it otherwise, which handles whether or not Messages is open, has its window showing, etc.
on setFocusToTextArea()
tell application "System Events"
if (count of windows of application process "Messages") is equal to 0 then
click UI element "Messages" of list 1 of application process "Dock"
delay 0.25
end if
try
set focused of text area 1 of scroll area 4 of splitter group 1 of window 1 of application process "Messages" to true
end try
end tell
end setFocusToTextArea
tell application "Messages"
if running then
my setFocusToTextArea()
else
activate
delay 2
my setFocusToTextArea()
end if
activate
end tell
Note: If Messages is closed when this script is run, the delay 2 command gives time for Messages to open before the other code runs. The value of the delay command can be adjusted as appropriate for the speed of your system.
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
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