The following is my script for creating Password protected files, but I do not know why it takes around 12 seconds for the first checkbox to get clicked!
The second click works fine though.
I have tried several things including this: Applescript delay issue with no success.
activate application "Preview"
tell application "System Events"
tell process "Preview"
keystroke "p" using command down
delay 0.5
tell front window
repeat until exists sheet 1
delay 0.5
end repeat
tell sheet 1
click menu button "PDF"
repeat until exists menu 1 of menu button "PDF"
delay 0.02
end repeat
click menu item "Save as PDF" of menu 1 of menu button "PDF"
end tell
end tell
repeat until exists sheet 1 of sheet 1 of front window
delay 0.2
end repeat
tell sheet 1 of sheet 1 of front window
click button "Security Options..."
end tell
tell window "PDF Security Options"
set selected to true
set focused to true
-- NOTE: for some reason there is a delay of about 12 seconds here, I do not know why
try
tell checkbox "Require password to open document"
click
end tell
keystroke VarPassword
keystroke (ASCII character 9)
keystroke VarPassword
tell its checkbox "Require password to copy text, images and other content"
click
end tell
keystroke FixedPassword
keystroke (ASCII character 9)
keystroke FixedPassword
click button "OK"
end try
end tell
repeat until exists sheet 1 of sheet 1 of front window
delay 0.2
end repeat
keystroke "g" using {command down, shift down}
repeat until exists sheet of sheet 1 of sheet 1 of front window
delay 0.2
end repeat
delay 0.5
keystroke SaveFolder
delay 0.5
click button "Go" of sheet of sheet 1 of sheet 1 of front window
set value of text field 1 of sheet 1 of sheet 1 of front window to FileName
click button "Save" of sheet 1 of sheet 1 of front window
end tell
end tell
Related
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
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.
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.
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
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