Safari - AppleScript does not move to the next section of a bookdown online book - safari

I am writing a script to print a section of a bookdown online book as PDF, then move to the next section, and so on.
The print part works (the key codes are from this page):
tell application "Safari"
activate
tell application "System Events"
key code 35 using command down -- activate print menu item
end tell
delay 0.5
set i to 0
repeat while i < 15
set i to i + 1
delay 0.1
tell application "System Events"
key code 48 -- press tab 15 times
end tell
end repeat
tell application "System Events"
key code 49 -- press space
end tell
set i to 0
repeat while i < 2
set i to i + 1
delay 0.1
tell application "System Events"
key code 125 -- press down key twice
end tell
end repeat
tell application "System Events"
key code 36 -- enter
end tell
set i to 0
repeat while i < 16
set i to i + 1
delay 0.1
tell application "System Events"
key code 125 -- press tab to get to "save"
end tell
end repeat
tell application "System Events"
key code 36 -- enter to cleck on save
end tell
end tell
Problem
Now that I have printed the current section and I am back on Safari, I can click manually on the right arrow and move to the next section, but I can't manage to have the script to do that.
I have tried to add the following to the script above:
tell application "System Events"
key code 124 -- right arrow to enter the next page
end tell
Or even to "reopen" Safari, but nothing happens.
tell application "Safari"
activate
tell application "System Events"
key code 124 -- right arrow to move to the next section
end tell
end tell
How can I have AppleScript "turn the page" and move to the next section?
Also, I welcome suggestions to improve the script! I wonder if it would be easy to avoid repeating "tab" 15 times. I have looked at the Accessibility Inspector and found that "PDF" in the print menu corresponds to NSPopUpButtonCell. I have tried to use select NSPopUpButtonCell of its sheet but it did not work.

I can click manually on the right arrow and move to the next section, but I can't manage to have the script to do that.
How can I have AppleScript "turn the page" and move to the next section?
If you are trying to programmatically click the right-arrow, as shown in the image below, then the following example AppleScript code can do that:
tell application "Safari" to ¬
tell document 1 to ¬
do JavaScript ¬
"document.getElementsByClassName('fa fa-angle-right')[0].click();"
Notes:
This requires Allow JavaScript from Apple Events to be check on the hidden Develop menu.
To unhide the hidden Develop menu:
Safari > Preferences… > Advanced > [√] Show Develop menu in menu bar
Update to address:
Also, I welcome suggestions to improve the script! I wonder if it would be easy to avoid repeating "tab" 15 times.
Here is how I'd use the JavaScript from above and coded to avoid using the key code and or keystroke System Events commands, especially tabbing around the UI.
Example AppleScript code:
tell application "System Events"
tell application process "Safari"
set frontmost to true
set i to 0
repeat until (its frontmost = true)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu item "Print…" of ¬
menu 1 of ¬
menu bar item "File" of ¬
menu bar 1
tell its front window
set i to 0
repeat until (exists menu button "PDF" of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu button "PDF" of sheet 1
set i to 0
repeat until (exists menu item "Save as PDF" of ¬
menu 1 of menu button "PDF" of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu item "Save as PDF" of ¬
menu 1 of ¬
menu button "PDF" of ¬
sheet 1
set i to 0
repeat until (exists button "Save" of sheet 1 of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click button "Save" of sheet 1 of sheet 1
set i to 0
repeat while (exists sheet 1)
delay 0.1
set i to i + 1
if i ≥ 100 then return
end repeat
end tell
end tell
end tell
tell application "Safari" to ¬
tell document 1 to ¬
do JavaScript ¬
"document.getElementsByClassName('fa fa-angle-right')[0].click();"
Notes:
Since this type of AppleScript script is using UI Scripting, I have included an error handling in the form of a repeat loop to wait up to two seconds for the target UI element to become available to be acted upon for most of the targets, however the last repeat loop waits longer because it has to wait until the Save as PDF to complete. A simple delay command with an appropriate value could be used instead, but with the include delay of a tenth of a second in the repeat loops it shouldn't have to wait any longer than need be. In other words, I'd only use simple delay command if I want to slow the script down from going through the various events of the UI. It's doubtful that it would need to be adjusted, but obviously do so as/if necessary.
If the timeout is reached, the script aborts at that point without any error message. The single-line if i ≥ 20 then return statements can be turned into a full if block and include an error message via the display dialog, display alert, or display notification command as wanted.

Related

apple script to rotate display

Hi I am trying to write an apple script to rotate displays in macOS 12.3
I have reviewed the below however apple has changed the display menu
https://apple.stackexchange.com/questions/319938/keyboard-shortcut-flip-screen
I am having trouble selecting the cell menu item for an external display
I have tried using Accessibility Inspector, but I don't know enough to get it to work
display menu
Accessibility Inspector
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
delay 1
tell application "System Events"
tell process "System Preferences"
tell window 1
click button "Display Settings…"
tell cell of outline row of outline 1 of scroll area 1 of sheet 1 of window 1
set selected to false
end tell
--tell pop up button "Rotation:" of tab group 1 of (first window whose name is "LG HDR 4K") of application process "System Preferences"
-- if (value) contains "Standard" then
-- click
-- click menu item "90°" of menu 1
-- my clickConfirmButton()
-- else
-- click
-- click menu item "Standard" of menu 1
-- end if
-- end tell
end tell
end tell
end tell
end tell
courtesy of oluckyman's comment on this gist
tell sheet 1
select row 2 of outline 1 of scroll area 1 -- the row with external monitor
tell pop up button "Rotation:"
set theRotation to value of it
click
tell menu "Rotation:"
if theRotation = "Standard" then
click menu item "90°" -- your preferred rotation
else
click menu item "Standard"
end if
end tell
end tell
delay 1
if exists of button "Confirm" of sheet 1 then
click button "Confirm" of sheet 1
end if
click button "Done"
end tell
https://gist.github.com/v1c77/96affa87de94045d29cdc5f9cb8c1847

Is there any apple script to set the mission control keyboard options to null?

Steps to do:
Open System Preferences
Click on Mission Control
Under the Heading "Keyboard and Mouse Shortcuts" do the following
Select "-" for Mission Control
Select "-" for Application Windows
Select "-" for Show desktop
Note: Required for bigsur OS
[Image][1]
[1]: https://i.stack.imgur.com/syhEF.png
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina a 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:
-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
-- # Open System Preferences to Mission Control.
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.expose"
end tell
tell application "System Events"
tell window 1 of application process "System Preferences"
-- Wait until UI ellements are available.
set i to 0
repeat until exists checkboxes of group 2
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
-- # Uncheck any checked ckeckboxes.
tell group 2
repeat with i from 1 to count checkboxes
if value of checkbox i is equal to 1 then click checkbox i
end repeat
end tell
-- # Set all pop up buttons to: -
tell group 1
repeat with i from 1 to count pop up buttons
if value of pop up button i is not "-" then
click pop up button i
pick menu item "-" of menu 1 of pop up button i
end if
end repeat
end tell
end tell
end tell
tell application "System Preferences" to quit
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.
Following script disables mission controls and set the shortcut options to null.
tell application "System Preferences"
activate
delay 1
set the current pane to pane id "com.apple.preference.expose"
delay 1
tell application "System Events"
tell checkbox "Group windows by application" of group 2 of window "Mission Control" of application process "System Preferences"
if (get its value) = 1 then click it
tell application "System Events"
tell checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
if (get its value) = 1 then click it
tell application "System Events"
tell checkbox "When switching to an application, switch to a Space with open windows for the application" of group 2 of window "Mission Control" of application process "System Preferences"
if (get its value) = 1 then click it
tell application "System Events"
tell checkbox "Displays have separate Spaces" of group 2 of window "Mission Control" of application process "System Preferences"
if (get its value) = 1 then click it
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end tell
tell application "System Events" to tell process "System Preferences"
tell window "Mission Control"
tell pop up button 1 of group 1
if value is not "-" then
click
delay
pick menu item "-" of menu 1
end if
end tell
tell pop up button 2 of group 1
if value is not "-" then
click
delay
pick menu item "-" of menu 1
end if
end tell
tell pop up button 3 of group 1
if value is not "-" then
click
delay
pick menu item "-" of menu 1
end if
end tell
end tell
end tell
quit
end tell

Possible to script the "Export" command in Microsoft Outlook 2019 on Apple OS?

I'm trying to automate export of my calendar on Outlook 2019 on Apple OS 10.13; I don't see the Export command in the scripting dictionary of the application. Am I missing it? Is there a better way to automate this?
What is Apple OS? Is it MacOS???
My Microsoft Outlook version is "version 16.29 (19090802)", the code below works on my MacOS 10.12.6. You can try if it works on your machine.
tell application "Microsoft Outlook"
activate
delay 0.3
end tell
tell application "System Events"
tell process "Microsoft Outlook"
set theExportAsName to "Outlook for Mac Archive.olm"
set theExportToFolder to "/Users/admin/Desktop"
--input the file name and the destination folder path you want to export as
tell menu item "Export..." of menu "File" of menu bar item "File" of menu bar 1
click
delay 0.5
end tell
--show the Export dialog (1/3), we need to deal with three dialogs totally.
--knowledge point 1: how to click menu item in APP menu bar
tell group 1 of group "What do you want to export?" of front window
if value of radio button "Items of these types:" is not 1 then
click radio button "Items of these types:"
end if
end tell
--1st we choose export items
tell group 1 of group 1 of group "What do you want to export?" of front window
if value of checkbox "Mail" is not 0 then
click checkbox "Mail"
end if
if value of checkbox "Calendar" is not 1 then
click checkbox "Calendar"
end if
if value of checkbox "Contacts " is not 0 then
click checkbox "Contacts "
end if
end tell
tell group 2 of group 1 of group "What do you want to export?" of front window
if value of checkbox "Notes" is not 0 then
click checkbox "Notes"
end if
if value of checkbox "Tasks" is not 0 then
click checkbox "Tasks"
end if
end tell
--2nd we check Calendar, uncheck all the others
click button "Continue" of front window
delay 0.5
--3rd click Continue button to go to next dialog window (2/3)
tell text field 1 of sheet 1 of front window
set value to theExportAsName
end tell
--1st we set the file name we want to use
set the clipboard to theExportToFolder
keystroke "G" using {command down, shift down}
delay 0.5
keystroke "v" using {command down}
delay 0.5
keystroke return
delay 0.5
--2nd we set the destination folder path we want to use
tell sheet 1 of front window
click button "Save"
delay 0.5
end tell
--3rd click Save button to go to the last dialog window (3/3)
tell front window
click button "Finish"
end tell
--1st click Finish button to finish this task
end tell
end tell
Note 1: the delay duration is longer than needed, I set it to big number to make the code run slowly, to make you see what happened. You can test then set smaller delay value to speed up the running.
Note 2: to these three dialog window, I all use the code below to get information about the UI elements - to see how to reference them.
tell application "Microsoft Outlook"
activate
delay 0.3
end tell
tell application "System Events"
tell process "Microsoft Outlook"
tell front window
set uiElems to entire contents
end tell
end tell
end tell
Note 3: I write some comments to explain each step of the code. You can test it on your machine. Please let me know if it helps.

Applescript to input text in website

set query to text returned of (display dialog "Enter Query" default answer "" buttons {"Input", "Cancel"} default button 1)
open location "http://www.soundcloud.com/"
delay 1
tell application "System Events"
key code 48
end tell
delay 1
query
So i'm trying to make an applescript that:
1) Prompts with dialogue box to enter data
2) Opens up webpage
3) Presses tab to get to first text box
4) Pastes data from step 1
I've figured out how to get to up to step 4 but when i print the query variable it only prints it within the applescript not the webpage.
set query to text returned of (display dialog "Enter Query" default answer "" buttons {"Input", "Cancel"} default button 1)
open location "http://www.soundcloud.com/"
delay 1
tell application "System Events"
key code 48
delay 1
keystroke query
end tell
I needed to add keystroke into the commandline
Try this instead:
set query to text returned of (display dialog "Enter Query" default answer "" buttons {"Input", "Cancel"} default button 1)
tell application "Safari"
if not (exists document 1) then reopen
activate
set URL of document 1 to "http://rna.tbi.univie.ac.at/cgi-bin/RNAfold.cgi"
delay 3
do JavaScript "document.getElementsByName('SCREEN')[0].value=" & quoted form of query & "" in document 1
end tell

scripting "system preferences" "language & text"

I need to change, using applescript, the listbox "region", in the system preferences|language & text section. I can't access this listbox - see code below.
tell application "System Preferences"
activate
get the name of every pane of application "System Preferences"
set the current pane to pane id "com.apple.Localization"
get the name of every anchor of pane id "com.apple.Localization"
reveal anchor "Formats" of pane id "com.apple.Localization"
end tell
GUI scripting is always a challenge iand isn't always reliable but here you go
tell application "System Preferences"
activate
get the name of every pane of application "System Preferences"
set the current pane to pane id "com.apple.Localization"
get the name of every anchor of pane id "com.apple.Localization"
reveal anchor "Formats" of pane id "com.apple.Localization"
end tell
tell application "System Events"
tell application process "System Preferences"
click pop up button 1 of tab group 1 of window 1
repeat 23 times
keystroke (ASCII character 30) -- got to the top
end repeat
repeat 21 times
keystroke (ASCII character 31) -- down to second to last
end repeat
keystroke return
end tell
end tell