Applescript to input text to find bar in browser - applescript

I can start a "Find" operation with the following applescript:
activate application "Firefox"
tell application "System Events"
tell process "Firefox"
click menu item "Find" of menu 1 of menu bar item "Edit" of menu bar 1
end tell
end tell
How can I then input a string to the find search?

activate application "Firefox"
tell application "System Events"
tell process "Firefox"
click menu item "Find" of menu 1 of menu bar item "Edit" of menu bar 1
end tell
keystroke myString
keystroke return
end tell

Related

Applescript clicking unknown menu item?

I'm using the LG OnScreen Control app to switch between different monitor layouts which have no names in the menu item as it uses pictures. I want to write an applescript that clicks either the first one or the 9th one, my favourite selections. =)
How can I click them? I only managed to select them with the below code
ignoring application responses
tell application "System Events" to tell process "OnScreen Control"
click menu bar item 1 of menu bar 2
end tell
end ignoring
delay 0.1
do shell script "killall System\\ Events"
tell application "System Events" to tell process "OnScreen Control"
tell menu bar item 1 of menu bar 2
click menu item "Screen Split" of menu 1
end tell
end tell
UPDATE1: Ah silly me, I figured I can use "key code 125" several times to click the down arrow and "key code 76" to hit enter.
ignoring application responses
tell application "System Events" to tell process "OnScreen Control"
click menu bar item 1 of menu bar 2
end tell
end ignoring
delay 0.1
do shell script "killall System\\ Events"
tell application "System Events" to tell process "OnScreen Control"
tell menu bar item 1 of menu bar 2
click menu item "Screen Split" of menu 1
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 125
key code 76
end tell
end tell
Wondering though if there is a better and quicker way to execute this? Addressing the sub-items directly despite not having a name?
UDPATE2 with solution thanks to #user3439894
ignoring application responses
tell application "System Events" to tell process "OnScreen Control"
click menu bar item 1 of menu bar 2
end tell
end ignoring
delay 0.1
do shell script "killall System\\ Events"
tell application "System Events" to tell process "OnScreen Control"
tell menu bar item 1 of menu bar 2
click menu item "Screen Split" of menu 1
click menu item 9 of menu 1 of menu item "Screen Split" of menu 1
end tell
end tell
After:
click menu item "Screen Split" of menu 1
Use:
click menu item 9 of menu 1 of menu item "Screen Split" of menu 1
Getting rid of all the key code commands

Applescript to click menu item of status bar app

I have an app called Fenêtre, when looking process name using top command it gives the name Fene?~Btre H.
I would like to click the item called 'a.py' under its menubar item as shown in figure.
My attempt:
attempt 1
tell application "System Events" to tell process "Fenêtre"
tell menu bar item 1 of menu bar 1
click
click menu item "Show all" of menu 1
end tell
end tell
Error:
$ osascript a.applescript
a.applescript:121:157: execution error: System Events got an error: Can’t get menu item "Show all" of menu 1 of menu bar item 1 of menu bar 1 of process "Fenêtre". (-1728)
Note that, when I run only first and last line of attemp1 it runs good, when I add middle lines it fails to run.
attempt 2
ignoring application responses
tell application "System Events" to tell process "Fenêtre"
click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Fenêtre"
tell menu bar item 1 of menu bar 2
click menu item "a.py" of menu 1
-- click menu item 1 of menu 1 -- another try
end tell
end tell
Updates (Still get errors)
tell application "System Events" to tell process "Fenêtre"
get entire contents of menu bar 2
end tell
This gives:
{menu bar item 1 of menu bar 2 of application process "Fenêtre" of application "System Events"}
References:
Applescript: on clicking Menu Bar item via gui script
applescript click menu bar option
https://superuser.com/questions/587815/can-applescript-osascript-be-used-to-click-menu-extra-menu-items
Applescript to show Apple menu bar items
Is AppleScript UI Scripting very slow in general, or is it my script, or something else?
Clicking an applications menu bar item with AppleScript
Thanks a lot.
Use a bundle identifier instead of the app name:
tell application "System Events"
tell (first application process whose bundle identifier is "BUNDLE_IDENTIFIER_HERE")
tell menu bar item 1 of menu bar 1
click
click menu item "Show all" of menu 1
end tell
end tell
end tell

Localized menu bar item strings in iTunes

The following code will disable shuffle mode in iTunes:
tell application "System Events"
tell process "iTunes"
click menu item "Off" of menu 1 of menu item "Shuffle" of menu 1 of menu bar item "Controls" of menu bar 1
end tell
end tell
However it only works if the system language is English.
I would prefer not to use indices, so I tried using localized string of on "Off", "Shuffle" and "Controls", but it doesn't seem to work.
Test code:
tell application "iTunes" to get localized string of "Shuffle"
Is there another way of getting the localized string or at least avoid having to hardcode the menu bar item indices?
You need to know the key's name because iTunes don't use the english menu's title as the key name.
To know the key name (the "Localizable.strings" file in the iTunes bundle is a plist format):
Open the TextWrangler application, drag the
"/Applications/iTunes.app/Contents/Resources/English.lproj/Localizable.strings"
file and drop it on the icon of the TextWrangler application.
Search "string>Shuffle<" in the document to get the key
name.
set iTunesPath to "/Applications/iTunes.app" as POSIX file as alias
set contr to localized string "8d2vvyy7c7" in bundle iTunesPath
set shuf to localized string "atmqaez8y8" in bundle iTunesPath
set off to localized string "ve0frz64yk" in bundle iTunesPath
activate application "iTunes"
tell application "System Events"
tell process "iTunes"
click menu item off of menu 1 of menu item shuf of menu 1 of menu bar item contr of menu bar 1
end tell
end tell
I opened the Accessibility Inspector in Xcode's Development Tools to see what objects I could find enumerating through UI elements
First I ran this to get a good idea, which returns the name I need to start, menu bar 1
tell application "System Events to tell process "iTunes"
UI elements
end tell
After this you can get a list of elements to work with, once you run tell menu bar 1 to display its UI elements.
{menu bar item "Apple" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "iTunes" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "File" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Edit" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "View" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Controls" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Store" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Window" of menu bar 1 of application process "iTunes" of application "System Events", menu bar item "Help" of menu bar 1 of application process "iTunes" of application "System Events"}
I'm thinking next you could do use what is returned here to iterate through and find the item, store this in a variable and make it less dependent on name, since the index won't change based on the language and this can be verified in Accessibility Inspector which will show object references.
I tried something along these lines:
tell menu bar 1
set a to UI Elements
return item 6 of a
end tell
Output:
menu bar item "Controls" of menu bar 1 of application process "iTunes" of application "System Events"
All right, going further you will want to find the specific menu item and value. Which values are being affected by the language changes too.
tell application "System Events" to tell process "iTunes"
tell menu bar 1
set a to UI elements
set b to item 6 of a
set c to UI elements of b
-- c = {menu "Controls" of menu bar item "Controls" of menu bar 1 of application process "iTunes" of application "System Events"}
tell menu "Controls"
set d to item 16 of UI elements
tell menu "Shuffle" of d
--click menu item 1 (On)
--click menu item 2 (Off)
end tell
end tell
end tell
end tell

Applescript to "start Dictation" and "stop Dictation" depending on menu

I have on Applescript to start and another one to stop Dictation. I tried to run them together and it only starts Dictation but does not stop it. I tried the key code way but this script is to be used with VoiceOver and that method does not work. Is there a way to have this script check if start/stop is ready and to act accordingly?
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu bar 1
tell menu bar item "Edit"
tell menu 1
click menu item "Start Dictation"
end tell
end tell
end tell
end tell
end tell
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu bar 1
tell menu bar item "Edit"
tell menu 1
click menu item "Stop Dictation"
end tell
end tell
end tell
end tell
end tell
You can use the exists command
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu 1 of menu bar item "Edit" of menu bar 1
if exists of menu item "Start Dictation" then
click menu item "Start Dictation"
else if exists of menu item "Stop Dictation" then
click menu item "Stop Dictation"
end if
end tell
end tell
end tell
--
Or you can use a word in the name of the menu, like this:
tell application "System Events"
tell (get first process whose frontmost is true)
tell menu 1 of menu bar item "Edit" of menu bar 1
click (first menu item whose name contains " Dictation") -- start or stop
end tell
end tell
end tell

in the GUI script below, why isn't "click button "Advanced..." working?

tell application "System Preferences"
activate
end tell
tell application "System Events"
set preferencesLocked to false
tell process "System Preferences"
delay 1
click menu item "Security & Privacy" of menu "View" of menu bar 1
delay 2.5
if title of button 4 of window 1 is "Click the lock to make changes." then
set preferencesLocked to true
click button "Click the lock to make changes." of window 1
end if
end tell
if preferencesLocked is true then
delay 2.5
activate application "SecurityAgent"
tell application "System Events"
tell process "SecurityAgent"
set value of text field 1 of scroll area 1 of group 1 of window 1 to "admin"
set value of text field 2 of scroll area of group 1 of window 1 to "XXXXXXX"
click button "Unlock" of group 2 of window 1
tell application "System Events"
tell process "SecurityAgent"
**click button "Advanced..."**
end tell
end tell
end tell
end tell
end if
end tell
You just have to replace the three periods with an ellipsis character:
tell application "System Preferences"
reveal pane id "com.apple.preference.security"
activate
end tell
tell application "System Events"
if title of button 4 of window 1 of process "System Preferences" is "Click the lock to make changes." then
click button "Click the lock to make changes." of window 1 of process "System Preferences"
delay 5
tell process "SecurityAgent"
set value of text field 2 of scroll area 1 of group 1 of window 1 to "password"
click button 2 of group 2 of window 1
end tell
end if
tell process "System Preferences"
click button "Advanced…" of window 1
end tell
end tell

Resources