Automator script to automatic toggle menu setting in Simulator.app - xcode

I need to automate the process of enrolment of Face ID and Touch ID for my UITests. For this purpose, I'm working on an automator script.
My current automator script, which at the moment can automatic click "Enrolled" in the menu:
on run {input, parameters}
if application "Simulator" is running then
tell application "System Events"
set theName to name of the first process whose frontmost is true
end tell
tell application "Simulator" to activate
tell application "System Events"
tell process "Simulator"
tell menu bar 1
tell menu bar item "Hardware"
tell menu "Hardware"
tell menu item "Face ID"
tell menu "Face ID"
click (menu item "Face ID" where its name starts with "Matching")
end tell
end tell
end tell
end tell
end tell
end tell
end tell
tell application theName to activate
end if
return input
end run
The problem is as following. I need to determine, if the device already is enrolled. There is a checkmark, which shows the current state. I have tried to check if the checkmarks is there or not. But I have not been able to make it work yet.
So my question. How can I do, so the script only will press the 'Enrolled' menu item, if the checkmark isn't there?

There's an attribute of menu items called AXMenuItemMarkChar, which is either set to the character representing a check mark next to the menu item (if checked), or missing value.
use application "System Events"
tell application "Simulator" to activate
tell process "Simulator" to tell menu bar 1 to ¬
tell menu bar item "Hardware" to tell menu "Hardware" to ¬
tell menu item "Face ID" to tell menu "Face ID" to ¬
tell menu item "Enrolled"
if the value of the attribute "AXMenuItemMarkChar" is not "✓" then ¬
click it
delay 1 -- !important
return the value of the attribute "AXMenuItemMarkChar"
end tell
In my testing, the attribute returns the correct value if the Simulator app is in focus at the time. The return value of this script should always be "✓", because if the menu item isn't checked, then the script proceeds to click it and put a check mark next to it; and if the menu item is checked, then there's nothing to do but confirm that it is by getting the attribute's value.
One of the issues with your script is that you had the wrong reference to a non-existent menu item (menu item "Face ID" of menu "Face ID" of menu item "Face ID"), and then proceeded to filter it with a where clause against a name that had a different value; hence it would return no value, and you'd have no menu item to click.
Hopefully my script will work for you. Let me know if you have any problems.
The AppleScript contained in this answer was tested on MacOS 10.13. It is an example script to illustrate how to meet your objective. The delay command may need to be adjusted according to your system, and the script may benefit from some error-handling to make it robust.

Related

Menu with different names

i´m using a Software with different Projects (Project 1, Project 2 etc.)
Is there a way to click a menu that contains "Project" and ignore all behind ?
What i try but it´s not working:
thx for help
Mike
tell application "Nuendo 12"
activate
end tell
tell application "System Events"
tell process "Nuendo 12"
click menu item whose title starts with "Project" of menu 1 of menu bar item "Fenster" of menu bar 1
end tell
end tell
It's quite close.
You have to specify that you want to find the first menu item which matches the condition, and you have to move the whose condition to the end
tell application "Nuendo 12"
activate
end tell
tell application "System Events"
tell process "Nuendo 12"
click (1st menu item of menu 1 of menu bar item "Fenster" of menu bar 1 whose title starts with "Project")
end tell
end tell
#vadian has already given you the appropriate syntax for what you asked, so this is merely a supplement to that in case what you're actually trying to do is to switch between the windows housing each project, if these windows happened to be called "Project 1", "Project 2", etc...
If this is the case, you can invoke a window's AXRaise action, which will brings it in front of the other windows belonging to the same application process, and give it the focus when the application process also next receives focus:
tell application id "com.apple.SystemEvents" to tell ¬
process "Nuendo 12" to tell the first window ¬
whose name starts with "Project" to (perform ¬
the action named "AXRaise")
tell application "Nuendo 12" to activate
[ Note: "AXRaise" may have a different name if you're locale doesn't fall under one of the English-language domains. ]

Applescript menu bar item toggle if title of attribute is... (or exists)

I need a applescript that clicks a menu item if the title of the item has a specific name.
Here the concrete case (Sorry... I'm not allowed to embed pictures yet, so please click):
If the menu item 2 is "Enable calibration" it should be clicked. If the title is "Disable calibration" it should stop (I just put a "false" for testing in the code).
Here my first code but it doesn't work (Syntaxerror):
tell application "System Events" to tell process "SoundID Reference"
if the title of the attribute "AXMenuItem" is "Disable calibration" then click it
else
return false
end if
end tell
My second try was with the code "exists" but it doesn't work as well (just nothing happens):
tell application "System Events" to tell process "SoundID Reference"
if menu item "Enable calibration" of menu 1 exists then click it
end tell
So any advices how I could solve this?
Finally I found another solution how to solve this problem with the "try"-command:
tell application "System Events" to tell process "SoundID Reference"
tell menu bar item 1 of menu bar 2
try
click menu item "Enable calibration" of menu 1
end try
end tell
end tell

How to take user voice input in applescript

I want to be able to take in input from the user in a program and have them speak it instead of type it into a text box. Is there any way to do this?
Any help would be appreciated.....
If you change the shortcut used to start dictation (System Preferences > Keyboard > Dictation > Shortcut) to either Press Left Command Key Twice or Press Either Command Key Twice, you can trigger this shortcut using System Events (provided the necessary accessibility privileges are granted):
delay 0.1
tell application "System Events" to repeat 2 times
key down command
key up command
end repeat
Alternatively, you can trigger the menu item:
tell application "System Events" to tell ¬
(the first process whose frontmost is true) to tell ¬
menu bar 1 to tell ¬
menu bar item "Edit" to tell ¬
menu "Edit" to tell ¬
menu item "Start Dictation" to ¬
if exists then click it
Edited to correct the referencing of the menu item chain, as highlighted by #wch1zpink, which previously prevented it working in *Google Chrome*.
#CJK really deserves the vote love, as this is only an adjusted extension of his thorough answer...
As taken from #CJK 's solution...
"Alternatively, you can trigger the menu item:"
tell application "System Events" to tell ¬
(the first process whose frontmost is true) to tell ¬
menu bar 1 to tell ¬
menu "Edit" to tell ¬
menu item "Start Dictation" to ¬
if exists then click it
Worked in almtost every application I tested the above code with... It did not work in Google Chrome.
However, this minor adjustment to his original code, Will work in Google Chrome.
tell application "System Events" to tell (the first process whose frontmost is true)
tell menu bar 1 to tell menu bar item "Edit"
tell menu "Edit"
try
click menu item "Start Dictation"
end try
end tell
end tell
end tell
For either of these solutions to actually "Enable Dictation" the mouse cursor must be in a field which allows text input, before the code is run.

Selecting Pop Up Menu Buttons in AppleScript

I want to automate clicking a specific pop down menu's item.
For Example, I want to change the Value of "Message receive Sound" to something else. How can I do this with AppleScript? And how can I do this with other pop down menus in AppleScript?
(To open the iMessage Settings menu, shown in the image, type CMD COMMA, once you open iMessage)
Note: I have successfully done this Automator, I just want to do it in applescript.
It's called GUI scripting. You have to identify the reference to the UI element(s).
GUI scripting strongly depends on the system version. If an update changes the UI structure the script will brake.
This selects the sound "Popcorn" in the sound popup menu. It's for El Capitan. In systems < 10.11 the UI elements may be different and the process name might be "iChat"
tell application "System Events"
tell process "Messages"
set frontmost to true
if not (exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")) then
keystroke "," using command down
repeat until exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")
delay 0.1
end repeat
end if
tell (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")
tell pop up button 4 of group 1
click
delay 0.2
click menu item "Popcorn" of menu 1
end tell
end tell
end tell
end tell

In Applescript, how can I find out if a menu item is selected/focused?

I have a script for OS X 10.5 that focuses the Search box in the Help menu of any application. I have it on a key combination and, much like Spotlight, I want it to toggle when I run the script. So, I want to detect if the search box is already focused for typing, and if so, type Esc instead of clicking the Help menu.
Here is the script as it stands now:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
click helpMenuItem
end tell
end tell
And I'm thinking of something like this:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
set searchBox to menu item 1 of menu of helpMenuItem
if (searchBox's focused) = true then
key code 53 -- type esc
else
click helpMenuItem
end if
end tell
end tell
... but I get this error:
Can’t get focused of {menu item 1 of menu "Help" of menu bar item "Help" of menu bar 1 of application process "Script Editor" of application "System Events"}.
So is there a way I can get my script to detect whether the search box is already focused?
I solved my problem by working around it. I still don't know how to check if a menu item is selected though, so I will leave this topic open.
You need to use attribute AXMenuItemMarkChar.
Example:
tell application "System Events"
tell process "Cisco Jabber"
set X to (value of attribute "AXMenuItemMarkChar" of menu item "Available" of menu "Status" of menu item "Status" of menu "File" of menu bar item "File" of menu bar 1) is "✓" -- check if Status is "Availible"
end tell
end tell
If the menu item is checked, the return value is ✓, otherwise it is missing value.
Note: This test only works if the application whose menus are being inspected is currently frontmost.
The built in key shortcut Cmd-? (Cmd-Shift-/) already behaves like this. It moves key focus to the help menu's search field if it is not already focused, and otherwise dismisses the menu.
Using /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app you can use the built-in accessibility system to look at properties of the UI element under the mouse. Take special note of the cmd-F7 action to lock focus on an element and the Refresh button. Sadly the element and property names don't directly match those in the script suite, but you can look at the dictionary for System Events or usually guess the right terminology.
Using this you can determine two things. First, the focused property isn't on the menu item, but rather there is a text field within the menu item that is focused. Second, the menu item has a selected property.
With this, I came up with:
tell application "System Events"
tell (first process whose frontmost is true)
set helpMenuItem to menu bar item "Help" of menu bar 1
-- Use reference form to avoid building intermediate object specifiers, which Accessibility apparently isn't good at resolving after the fact.
set searchBox to a reference to menu item 1 of menu of helpMenuItem
set searchField to a reference to text field 1 of searchBox
if searchField's focused is true then
key code 53 -- type esc
else
click helpMenuItem
end if
end tell
end tell
Though this still doesn't work. The key event isn't firing as far as I can tell, so something may still be hinky with the focused property on the text field.
Anyway, your click again solution seems much easier.
I just came across the need to do this myself for some file processing in Illustrator.
Here is what I came up with:
tell application "Adobe Illustrator"
activate
tell application "System Events"
tell process "Illustrator"
set frontmost to true
set activeMenuItem to enabled of menu item "Unlock All" of menu "Object" of menu bar item "Object" of menu bar 1
if activeMenuItem is true then
tell me to beep 3
else
tell me to beep 2
end if
end tell
end tell
end tell
Done.
This worked with no problem and could be used to iterate a file. I'll probably have to do this many more times in my future automation.
Good luck!
This worked for me to toggle between two menu items, based on which one is selected, using the "selected" property:
tell application "System Preferences"
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 2 of tab group 1 of window 1
click
delay 0.2
set appControl to menu item "App Controls" of menu 1
set fKeys to menu item "F1, F2, etc. Keys" of menu 1
if selected of appControl is true then
click fKeys
else
click appControl
end if
end tell
end tell

Resources