Applescript to input text in website - automation

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

Related

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

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

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.

Long delay when password-protecting PDF files using Preview

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

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.

Use Applescript to pass Path and File Name to Save as Print Dialog box for PDF's

I have a script that opens an email and then I want to save it as a PDF. The script works but I cannot figure out how to pass the path and folder name to the dialog box that results after I call Save as a PDF. This is my script so far.
global FlName
tell application "Mail"
set theMsg to selection
open selection
set Dialogresult to the button returned of (display dialog "Process this email and Print as PDF" buttons {"Yes", "No"} default button "Yes")
if Dialogresult is "Yes" then
repeat with selectMsg in theMsg
tell selectMsg
--set background color to red --Show message processed
set FlName to subject
set check to count every word of FlName
--display dialog check
set FlName to (my FixFileName(FlName)) --strip bad characters
end tell
end repeat
set process_name to "Mail"
activate application process_name
tell application "System Events"
tell process process_name
--display dialog "proposed File Name" & return & FlName
keystroke "p" using command down
delay 2
set PDFref to sheet 1 of window 1
click menu button "PDF" of PDFref
click menu item "Save as PDF…" of menu 1 of menu button "PDF" of PDFref
tell application "Mail"
display dialog "Proposed Name " & my FlName default answer FlName & "change in the Box if Not OK"
set FlName to text returned of result
end tell
keystroke FlName
end tell
end tell
else
set Dialogresult to the button returned of (display dialog "Close this eMail" buttons {"Yes", "no"} default button "No")
if Dialogresult is "Yes" then
close window 1
end if
end if
end tell
on FixFileName(str) --Deletes characters that cannot be used in file names
set fixed_string to {}
set bad_char to {":", "/"}
repeat with c from 1 to (count every character in str)
if bad_char contains (character c of str) then
set end of fixed_string to "-"
else
set end of fixed_string to (character c of str)
end if
end repeat
fixed_string as string
end FixFileName
Peter
One other possiblity to consider is writing a separate PDF Applescript and adding it to the PDF pulldown menu via "Edit Menu". (Not sure what application you would have to TELL... Check out this link for a lead: http://hints.macworld.com/article.php?story=2004122222145585) Here's the excerpt from Mac Help:
You can add your own items, such as applications and AppleScript
scripts, to the PDF pop-up menu. For example, you can add an
AppleScript script that applies a Quartz filter to a PDF file, or you
can add an application that opens the PDF file immediately after it’s
created.
If you create a Print workflow plug-in in Automator, that plug-in is
added automatically. For more information, open Automator, choose Help
Automator Help, and search for “Print workflow plug-in.”
To add an item to the PDF pop-up menu:
Choose File > Print.
Choose Edit Menu from the PDF pop-up menu.
Click the Add (+) button and select the item you want to add.
For example, if you want to open a newly created PDF file in Adobe
Acrobat, select Adobe Acrobat. If you’ve created an AppleScript script
that applies a Quartz filter to a document, choose that script.
An alias to the item is saved in the PDF Services folder in your
Library folder.
If you're unable to set the name of the PDF when it's generated, but know where it was saved and what it was saved as, use AppleScript to rename and move the file to where you want instead. It might be kind of kludgy to select the location and the name, using a display dialog to let the user choose the name and a choose folder to allow them to select the location.
Following #Chuck's answer, yes that is kind of kludgy, but there is the choose file name command, which lets the user choose the name as well as the location...
set the FlName to (choose file name with prompt "Choose a name and location for the new PDF document:" default name "untitled.pdf")
However, this does not actually create the file. The commands below do.
open for access FlName
close access FlName