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

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

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

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

Setting a Popup Buttons Value from 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.

Using AppleScript In Adobe Acrobat PRO DC to Reduce File Size

I am trying to use AppleScript to open a PDF in Adobe Acrobat Pro and reduce the file size using the "Reduce File Size" option when saving the file.
Trying to click this element
Which, according to Accessibility Inspector, has the following hierarchy:
Accessibility Inspector Image
My first step was tracking down all the elements in the window with set allElem to entire contents of window "Save As PDF". The output suggests this element is not a button or checkbox:
group "Reduce File Size" of group " My Computer " of UI element 1 of group 1 of group 1 of window "Save As PDF" of application process "AdobeAcrobat" of application "System Events"
static text "Reduce File Size" of group 1 of group "Reduce File Size" of group " My Computer " of UI element 1 of group 1 of group 1 of window "Save As PDF" of application process "AdobeAcrobat" of application "System Events"
text field 1 of static text "Reduce File Size" of group 1 of group "Reduce File Size" of group " My Computer " of UI element 1 of group 1 of group 1 of window "Save As PDF" of application process "AdobeAcrobat" of application "System Events"
group 1 of group "Reduce File Size" of group " My Computer " of UI element 1 of group 1 of group 1 of window "Save As PDF" of application process "AdobeAcrobat" of application "System Events"
Without a button to click, I tried getting around the issue by sending keystrokes to the window.
tell window "Save As PDF"
activate
keystroke tab
keystroke tab
keystroke tab
keystroke tab
delay 1
key code 36 --return key
end tell
But the window "Save As PDF" does not appear to be coming to the front; therefore, the keystrokes are not registering to that particular window.
Similarly
tell group "Reduce File Size" of group " My Computer " of UI element 1 of group 1 of group 1 of window "Save As PDF" of application process "AdobeAcrobat" of application "System Events"
set {xPosition, yPosition} to position
set {xSize, ySize} to size
click at {xPosition + (xSize div 2), yPosition + (ySize div 2)}
end tell
doesn't work because the click event is not occurring in the "Save As PDF" window for some reason.
So now I'm wondering two things:
Is there actually a button or checkbox that I can click (am I somehow missing something with the available click events, e.g. can you send a click event to the static text?).
Why are the keystrokes and click events not registering in the window?
After a lot of trial and error, it appears as though activate window "Save As PDF" was not truly activating the window. Ultimately, user495470's answer solved my issue.
tell application "System Events" to tell process "Acrobat Pro"
perform action "AXRaise" of window "Save As PDF"
end tell
set frontmost to true
This finally brought Adobe Acrobat to the front window AND activated it. Before, all my keystrokes or clicks were registering in the Script Editor window (or terminal window if I ran if from there), instead of the "Save As PDF" window.
The final code now becomes
tell application "System Events"
tell process "Acrobat Pro"
delay 1
click menu item "Save As..." of menu "File" of menu bar 1
delay 2
--Bring "Save As PDF" window to front
tell application "System Events" to tell process "Acrobat Pro"
perform action "AXRaise" of window "Save As PDF"
end tell
set frontmost to true
--Tab 4 times To Select Checkbox
delay 0.5
keystroke tab
keystroke tab
keystroke tab
keystroke tab
key code 36 --return key
delay 0.5
--Tab 4 times to select "Choose Different Folder..." button
keystroke tab
keystroke tab
keystroke tab
keystroke tab
key code 36 --return key
delay 0.5
end tell
end tell
It's not exactly elegant, but it works. I'm still surprised that clicking the "Choose Different Folder..." was not achieved through:
Click Button "Choose Different Folder..." of group 2 of UI element 1 of group 1 of group 1 of window "Save As PDF"

AppleScript toggling checkmarks beside menu items

This is an extension of a previous question
Applescript: on clicking Menu Bar item via gui script
On the highlighted menu item below for the f.lux menu bar, if you click it, there will be a checkmark indicating that the "Disable for an hour" feature has been enabled. What I'm trying to do is write a GUI AppleScript for f.lux where the user can decide to toggle the check mark by typing a keyword in Alfred followed by a 1 or a 0 where 1 serves to enable the "Disable for an hour" and 0, serves to keep it unchecked.
This is a screen shot of the menu bar for f.lux
I am however having a hard time figuring out what attribute to adjust for the "Disable for an hour" menu item in order to toggle the checkmark. Here is the code, but I get an unexpected token error when compiling it via applescript editor. So far what I'm trying to do is target the "menu item mark character" attribute indicated by the arrow in screenshot above, but I'm not sure if this is the right approach to toggle the "Disable for an hour" item. Can someone please give me advice?
on alfred_script(q)
set myOption to q as integer
ignoring application responses
tell application "System Events" to tell process "Flux"
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 "Flux"
tell menu bar item 1 of menu bar 2
if myOption is 1 then
set ✓ to value of attribute "AXMenuItemMarkChar" of menu item "Disable for an hour" of menu 1
else if myOption is 0 then
set nil to value of attribute "AxMenuItemMarkChar" of menu item "Disable for an hour" of menu 1
end if
end tell
end tell
end alfred_script
This seems to work:
tell application "System Events" to tell process "Flux"
tell menu bar item 1 of menu bar 2
set v to (value of attribute "AXMenuItemMarkChar" of menu item "Disable for an hour" of menu 1) as string
if (v = "" and myOption = 1) or (v is not equal to "" and myOption = 0) then
click menu item "Disable for an hour" of menu 1
else
key code 53
end if
end tell
end tell

Resources