Setting a Popup Buttons Value from Applescript - applescript

I'm trying to set an external applications settings with an applescript. I currently have
tell application "System Events"
tell process "Pro Tools"
click menu item "Save Copy In..." of menu "File" of menu bar 1
tell pop up button 4 of window "Save Copy In..."
click
set value to "AIFF"
end tell
end tell
end tell
But nothing happens at the set value point. Any help would be gratefully received! Attached is a screenshot of where I'm stuck at.

I think you just need to wait until the "Save Copy In..." window appears. Try this code, which adds a delay loop looking for the window.
tell application "System Events"
tell process "Pro Tools"
tell menu bar 1's menu "File"
click menu item "Save Copy In..."
end tell
repeat until (exists window "Save Copy In...")
delay 0.1
end repeat
tell window "Save Copy In..."'s pop up button 4
click
-- the menu does not appear inthe PUB's view heirarchy until after it's clicked
tell menu 1
click menu item "AIFF" -- 'pick' should work too
end tell
end tell
end tell
end tell

Try to insert a short delay to make sure the menu is available and rather than setting the value pick the menu item.
tell application "System Events"
tell process "Pro Tools"
click menu item "Save Copy In..." of menu "File" of menu bar 1
tell pop up button 4 of window "Save Copy In..."
click
delay 0.2
pick menu item "AIFF" of menu 1
end tell
end tell
end tell
The command pick is not documented, it's a synonym of click.

Related

Applescript Click item in system menu bar after opening it

I can open the bluetooth dropdown menu item with this code but I don't know how to actually click on any of the items in the menu.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
end tell
What would be a command to click on something in the open bluetooth menu?
This following AppleScript code should accomplish what you are trying to achieve. Just replace the "Mac Pro" part of the code with the name of the item you want to click.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
click checkbox "Mac Pro" of scroll area 1 of window 1
key code 53 -- Press escape key
end tell
This following AppleScript code will return the names of the checkboxes so you can easily know your options to use in the first code.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
set checkBoxNames to name of checkboxes of scroll area 1 of window 1
end tell

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 use applescript export markdown to html in typora

I want use Applescript to repeat the processing of export markdown to HTML on app typora
I think it can be two-part
first: click export to HTML
second: choose the target directory and click save
but I have a problem with first step
tell application "System Events"
tell application "Typora"
open "path/to/my/markdown.md"
end tell
tell process "Typora"
click (menu item "HTML" of menu "export" of menu item "export" of menu "file" of menu bar item "file" of menu bar 1)
end tell
end tell
this code doesn't work. no window popped up (ask me to choose target directory) after I run this script
This works for me:
activate application "Typora"
delay 1
tell application "System Events"
tell process "Typora"
click (menu item "HTML" of menu "export" of menu item "export" of menu "file" of menu bar item "file" of menu bar 1)
delay 1
click button "Save" of sheet 1 of window 1
click button "Replace" of sheet 1 of sheet 1 of window 1
end tell
end tell
A useful command for finding more UI elements is: entire contents of window 1

AppleScript - how do I select a menu item in a pop up button that has no title?

Relatively new to AppleScript here...
I'm trying to create an AppleScript to automate a File/Save Page As... action in Firefox. Specifically, I need to select "Web Page, complete" from the Save As... dialog instead of the default "All Files" selection in the pop up button at the bottom of the dialog box. (I'm using Firefox and this option specifically because I want to save the current html contents after some JavaScript code has run - to parse out values for subsequent processing).
I've been able to hack my way around this problem by selecting the pop up menu (which has no title) by using:
((pop up buttons of window "Save As") whose description is "All Files")
and by sending the key stroke "w" to select "Web Page, complete" in the pop-up menu.
I'm trying to find a more robust way of doing this instead of relying upon the fact that "w" selects the menu item that I want. I tried:
click menu item "Web Page, complete" of
((pop up buttons of window "Save As") whose description is "All Files")
but that didn't work. In looking at Accessibility Inspector, it looks like there is a menu between the pop up button (drop down list) and the menu item but I can't figure out how to refer to it.
Any help would be appreciated. Here's the full script:
tell application "Firefox" to activate
delay 0.25
tell application "System Events"
tell process "Firefox"
set frontmost to true
click menu item "Save Page As…" of menu "File" of menu bar 1
delay 0.25
repeat until window "Save As" exists
delay 0.5
end repeat
click ((pop up buttons of window "Save As") whose description is "All Files")
delay 0.5
-- This didn't work:
click menu item "Web Page, complete" of ((pop up buttons of window "Save As") whose description is "All Files")
-- This works but only because the first entry is "Web Page, complete"
keystroke "w"
keystroke return
delay 0.5
set outputfilename to "foo3.html" as text
keystroke outputfilename
keystroke return
delay 0.5
end tell
end tell
try this
activate application "Firefox"
tell application "System Events"
tell process "Firefox"
set frontmost to true
click menu item "Save Page As…" of menu "File" of menu bar 1
repeat until window "Save As" exists
delay 0.2
end repeat
tell window "Save As"
tell pop up button 1 of group 1
if value is not "Web Page, complete" then
click
delay 0.5
pick menu item "Web Page, complete" of menu 1
end if
end tell
set outputfilename to "foo3.html"
keystroke outputfilename
click button "Save"
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