xcode ios simulator: how to set focus on specific window - xcode

I have n instances of xcode ios simulator and I want to switch a focus on instance with specified id.
I tried next things:
Switching focus using "open": open -a Simulator --args -CurrentDeviceUDID <id>. But it opens the last focused simulator instance instead of the instance with passed id.
Switching focus using simctl tool. I tried to open URL in the specific instance of the simulator xcrun simctl openurl <id> "https://www.google.com", but it doesn't change the focus.
using applescript - it still focusing on the last focused simulator instance:
tell application "System Events" to tell process "Simulator"
perform action "AXRaise" of window 0
end tell
Do you have any idea, how to do this?

The following example AppleScript code is one method that works for me:
Example AppleScript code:
tell application "Simulator"
activate
set winName to the name of ¬
the first window ¬
whose visible is true ¬
and index is not 1
end tell
tell application "System Events" to ¬
perform action "AXRaise" of ¬
window winName of ¬
process "Simulator"
Notes:
The example AppleScript code assumes you have two Simulator windows visible, and it will toggle raising between the two. Obviously this is just an example and you'll need to modify and incorporate the methodology into you own code.
In your example AppleScript code, window n refers to its index as in:
index (integer) : The index of the window, ordered front to back.
    The quoted line above is from the AppleScript dictionary for Simulator in Script Editor.
The z-order of index starts at: 1
Using System Events and perform action "AXRaise" of window ... can be done by it's name property or it's index property.
The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional 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. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Related

Is there a way to pause a YouTube video in Google Chrome using AppleScript?

What I'm trying is to type tell application "Google Chrome" to pause and make audio or video in Google Chrome pause. It wouldn't work, since it thinks pause is a variable.
Is there any way to either pause audio or video in all applications or just Google Chrome?
From comments:
The following example AppleScript code will play/pause a YouTube video in the active tab of the front window of Google Chrome: tell application "Google Chrome" to tell active tab of front window to execute javascript "document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
#user3439894 this one works perfectly, since I only want to pause the video in the active tab. Is there any way to check if the video is paused so it doesn't unpause a video? Also could you write this comment as an answer so I can accept it as the correct answer?
There may be an easier way using JavaScript to ascertain if the active tab of the front window in Google Chrome has a YouTube video playing, however, as I do not know the code for it, here is a way that works for me.
The following example AppleScript code, shown below, was tested in Script Editor under macOS Catalina 10.15.7 and macOS Big Sur 11.2.3 using Google Chrome (Version 90.0.4430.93 (Official Build) (x86_64)) with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Example AppleScript code:
if not running of ¬
application "Google Chrome" then return
tell application "Google Chrome"
if (window count) = 0 then return
set atx to the active tab index of its front window
end tell
tell application "System Events"
-- # macOS Big Sur System Events bug issue.
-- # If running macOS Big Sur, uncomment the next
-- # two lines if System Events reports an error.
-- run
-- delay 0.5
tell application process "Chrome"
if (the value of ¬
attribute "AXTitle" of ¬
radio button atx of ¬
tab group 1 of ¬
group 1 of ¬
the front window) ¬
does not end with "Audio playing" then return
end tell
end tell
tell application "Google Chrome" to ¬
tell the active tab of its front window to ¬
execute javascript ¬
"document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
Notes:
As coded, the execute javascript only happens if there is a YouTube video playing in the active tab of the front window of Google Chrome, thus pausing it. The code, as coded, cannot cause a paused video to play, regardless of the active tab or any tab of any window.
The -- # macOS Big Sur System Events bug issue. section in the example AppleScript code is an attempted workaround to a System Events issue in macOS Big Sur, in that the exact same code without it runs without issue in macOS Catalina and may not work or needs adjusting. I am still trying to troubleshoot the issue in general. In my experience the issue has not been reproducible at will and as such alternate methods may have to be used if issue continues and cannot be diagnosed.
Use of other than exact versions tested under may be partially to blame for some errors, especially any that involve a change in the hierarchical UI element structure in any UI Scripting scenario using System Events.
Is there any way to either pause audio or video in all applications or just Google Chrome?
Typically pressing the Play/Pause key on the keyboard should pause a video in Google Chrome, and most other apps as well.
It wouldn't work, since it thinks pause is a variable.
pause is not part of the AppleScript dictionary in Google Chrome and why it shows as a variable.
In Google Chrome, running AppleScript code containing JavaScript needs the Allow JavaScript from Apple Events menu item checked under: View > Developer -- Note that this used to be the default, however, at some time is was changed. For more information: https://support.google.com/chrome/?p=applescript
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional 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. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
This following AppleScript code will pause or play any instance of a YouTube video in any tab of any window in Google Chrome (whether visible or not).
pauseOrPlayYoutubeInChrome("ytp-play-button ytp-button", 0)
to pauseOrPlayYoutubeInChrome(theClassName, elementnum)
tell application "Google Chrome"
set youtubeTabsRef to a reference to (tabs of windows whose URL contains "youtube")
repeat with youtubeTabs in youtubeTabsRef
execute youtubeTabs javascript "document.getElementsByClassName('" & ¬
theClassName & "')[" & elementnum & "].click();"
end repeat
end tell
end pauseOrPlayYoutubeInChrome
My solution consists of running 2 AppleScript in a specific environment.
It works for me using 1 window of Brave Browser(I think it is the same for google chrome) with 2 tabs on youtube.
First script force opening Brave browser, second run keystroke "K"
tell application "Brave Browser"
if it is not running then launch
activate
end tell
tell application "System Events"
keystroke "k"
end tell
I have created a Quick Action on Automator and then I have followed this answer: https://apple.stackexchange.com/a/401262
And when trying to test it as a Keyboard Shortcut when using another Application, I was receiving an error like this "com.automator.runner.xpc is not allowed to send keystrokes", so I realised that I need to give accessibility privileges(on Security and Privacy) to all applications I want the script to work.
And After that, it works, but sometimes it gives the same error and it works again after some minutes(and never come back again) so I think this can help someone too.
This will pause or play the youtube video on the tab of the Brave Browser that is active.

How to click on a button in an app from the status bar with Apple Script

I have see how to click on menu items but this application does not have those. I was wondering how to click on the button Connect
I tried click at {818, 320} but it did not work
Here is the scrip, i was able to open the app with this
tell application "System Events"
tell UI element "Hotspot Shield"
tell menu bar item 1 of menu bar 2
click
delay 2
get the actions of button "Connect"
end tell
end tell
end tell
FWIW One would not normally use tell UI element "Hotspot Shield" rather use tell application process "Hotspot Shield", however, looking at this through Accessibility Inspector, I do not see a way to click the "Connect" button. Even using a Watch Me Do action in Automator, while it will record the process, it won't play it back successfully.
That said, here is a workaround solution that works for me on macOS Mojave using AppleScript and the third-party command line utility Cliclick.
Looking at the cropped screen shot in your OP and using the approximate center of the Connect button to be at 320 for the y axis and using AppleScript to get the position of the menu bar item for use as the approximate x axis, the following example AppleScript code should work for you too.
tell application "System Events" to ¬
tell application process "Hotspot Shield" to ¬
tell menu bar item 1 of menu bar 2
set xPos to first item of (get its position)
click
end tell
delay 0.5
do shell script "/Applications/cliclick c:" & xPos & ",320"
Note that the value of the delay command may need to be adjusted to ensure the Connect button is visible before the click event takes place using cliclick.
Adjust the path to cliclick as needed. I placed it in /Applications just for testing purposes.
The usual caveats, regarding System Preferences > Security & Privacy > Privacy, apply.
I am not affiliated with the developer of Cliclick, just a satisfied user of the product.
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.

Perform actions on a certain window of the application with AppleScript

I'm building a macOS app for managing locations sent to an iOS device via GPX file in Debug mode, so I need to have to Xcode windows opened for each project. I got an AppleScript which presses Debug -> Simulate Location -> "my gpx file name" for continuous location updates. However, it works only if the window with an iOS project (through which I simulate the location) is frontmost.
I want the script to be able to perform said actions just on a window with a certain name (or rather whose name contains ...), no matter of the hierarchy. I don't want it to be brought to the front or activated, as I might be coding something in a second one while the script runs.
I tried many ways, but with my limited understanding of AppleScript I either get an error (like "Can't locate menu item 1 in bla-bla-bla of the process Xcode) or the window activates and doesn't work in the "background".
repeat while true
tell application "System Events" to tell process "Xcode"
click menu item "MyLocation" of menu 1 of menu item "Simulate Location" of menu 1 of menu bar item "Debug" of menu bar 1
end tell
delay 0.2
end repeat
Is it even possible to perform any action on inactive window? I thought it is since the system lets you scroll without focusing the window.

AppleScript to block Safari pop-up windows

I am trying to automate the blocking of pop-up windows in Safari.
I have tried the following defaults write operation
defaults write com.apple.Safari WebKit2JavaScriptCanOpenWindowsAutomatically -boolean false
But this fails to do it. I have also tried setting it as true but even that didn't help. I am currently trying to find a way to do it using AppleScript.
This is what I have written so far -
CMD_ACTIVATE='tell application "Safari" to activate'
CMD_NEWTAB='tell application "System Events" to keystroke "," using {command down}'
osascript -e "$CMD_ACTIVATE" -e "$CMD_NEWTAB"
This opens up the preferences but then I draw a blank. Anyone with any suggestions on how to proceed? Also I don't really need a solution to this only in AppleScript any other way to do it would also be helpful.
Note: I have been using Mac OS only for a week now, and am not that well versed in the nuances of this OS, so please be a bit descriptive when answering.
Thanks.
Blocking pop-up window is done via Safari / preferences / tab Security where you need to set the correct checkbox.
This preference seems to be stored in your library / preferences file com.safari.plist which contains the flag com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptCanOpenWindowsAutomatically with value Yes when pop-up are blocked.
However, it may not be the only place to be changed and in any cases, it is not officially documented, so that place can be changed by Apple any time. This is not recommended to use it.
Going back to Applescript, because Safari is quite poor in terms of applescript handling events, you are forced to go via GUI scripting. That’s what you’ve started, but keep in mind that if Apple changes the layout of Safari preference window, your script must be reviewed.
When going through GUI scripting (which, again, should only be when no other solution found) you must understand structure of GUI objects. Window contains button, check box, tool bar... in a hierarchy model. For instance the preference window in Safari contains bellow the tool bar, an object "group 1" with itself contains many objects depending of the tool bar current selection. Once you understand that concept, the script below, which does what you're looking for, will be easy to understand with many comments:
tell application "Safari" to activate
tell application "System Events"
keystroke "," using {command down}
delay 0.2 -- leave sometime to open window
tell window 1 of process "Safari"
click button 6 of toolbar 1 -- Security button is number 6
delay 0.2
-- check if check box not yet set and set it.
if (value of checkbox 5 of group 1 of group 1) = 0 then click checkbox 5 of group 1 of group 1
end tell
delay 0.2
click button 1 of window 1 of process "Safari" -- click on red / close button
end tell
I am running on Safari 10.0.3. If your version is different, the preference window may be different. Then the script must be adjusted: the Security tab button may not longer be the number 6 in your version,...

How to automate clicking a specific button repeatedly with AppleScript?

I'm trying to write a script that will:
Open a particular program
Click on a button at a particular spot within the program window
Repeat the click a certain number of times with a specified delay between clicks
I'm planning to use iCal to schedule running the script, but what should the actual script look like? Could it be run on the background without making any windows visible?
Most native applications support UI scripting like this:
reopen application "Finder" -- open a default window if there are no open windows
tell application "System Events" to tell process "Finder"
repeat 3 times
click button 2 of window 1
delay 1
end repeat
end tell
If you get an error like "Access for assistive devices is disabled", enable access for assistive devices from the accessibility preference pane.
If you have Xcode, you can use Accessibility Inspector to inspect UI elements. Or use something like this:
reopen application "Finder"
tell application "System Events" to tell process "Finder" to tell window 1
{class, value} of UI elements of UI elements
description of UI elements
properties of some UI element
attributes of some UI element
value of attribute "AXFocused" of some UI element
actions of button 2
end tell
If click doesn't work, try perform action "AXPress", set selected to true, or set focused to true.

Resources