Applescript: Repeat until reCaptcha occurs - applescript

I am pretty new in coding, especially with applescript. I managed to make the following code work:
tell application "Safari"
repeat
delay 7.5
set the URL of document 1 to "https://url.com"
delay 2
tell document 1
do JavaScript "document.getElementById(\"id1\").click()"
do JavaScript "document.getElementById(\"id2\").click()"
do JavaScript "document.getElementById(\"id3\").click()"
do JavaScript "document.getElementById(\"id4\").click()"
do JavaScript "document.getElementById(\"id5\").click()"
do JavaScript "document.getElementById(\"id6\").click()"
delay 0.25
end tell
tell application "Safari"
activate
end tell
tell application "System Events"
delay 0.25
tell process "Safari" to key code 48
delay 0.5
key code 21
end tell
delay 0.25
tell application "Safari"
tell document 1
do JavaScript "document.getElementById(\"book\").click()"
end tell
end tell
end repeat
end tell
Instead of repeating the code all the time I would like to make the code repeat until the Google reCaptcha occurs which pops up after the last javascript action.

My recommendation would be to restructure your repeat statement like this...
set done to false
repeat while not done
if reCaptcha = "something" then
set done to true
end if
end repeat

Related

Apple script find and replace text

So, right now my code clicks on a textarea in safari then it adds a word using keystroke, however I was wondering if there is a way to find and replace a word in that area and also is there a way to replace a highlighted word without keystroke?
Current code:
Sorry its very very poorly formatted since I am pretty bad at applescript
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
to clickID(theId)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end tell
end clickID -- All the code up to this point did is allow me to click on IDs and classes in safari.
tell application "Safari" to activate
tell application "System Events"
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit"})
end tell
end tell
end tell
delay 2 -- All the code up to this point from the last comment did is open a new tab in safari.
repeat 5 times
set randomlist to {"1","2","3","4"} -- List of numbers that will be randomly selected and put in the texarea
set randomword to some item of randomlist
clickClassName("tool-button wikiEditor-toolbar-spritedButton", 0) -- This clicks the bold button on the sandbox page
tell application "System Events"
tell process "Safari"
keystroke randomword -- Uses keystroke to put replace the text inside the bold formatting area
end tell
end tell
delay 1
clickID("wpSave") -- Clicks save
delay 2
tell application "Safari" to set the URL of the front document to "https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit" -- Goes back to sandbox and repeats the process
delay 2
end repeat
If you want to test this then put in your wikipedia username in the link and login into your account. Pretty much its a wikipedia bot that will just put stuff in my wikipedia sandbox

Determining when QuickTime stops recording using AppleScript

I'm using the following AppleScript to determine when the user clicks the stop recording button in the task bar:
tell application "QuickTime Player"
tell document 1
activate
new screen recording
delay 1
tell application "System Events" to key code 49
delay 2
repeat until (new screen recording) is false
end repeat
end tell
end tell
Though instead the script keeps relaunching QuickTime.
You can put the result of the new screen recording command into a variable.
Use the exists command to check this document
tell application "QuickTime Player"
activate
set tdoc to new screen recording --> document "Screen Recording"
delay 1
tell application "System Events" to key code 49
delay 2
repeat while exists tdoc
delay 1
end repeat
-- the recording is stopped
tell front document
-- do something with the front document ("Untitled")
end tell
end tell

How to wait for webpage to fully load before proceeding script?

How do you wait for a webpage to fully load before proceeding the script?
I know you can use delay 4 if you would want it to wait 4 seconds but this is not safe enough.
In VBA you have a simple code that always works and it goes like this:
Dim x As String
x = "https://na6.salesforce.com/p/attach/NoteAttach?pid=" & Range("T34").Value & "&parentname=" & Range("T35").Value & "&retURL=%2F0018000000sKz8K%3Fpb0%3Dtrue"
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate (x)
IE.Visible = True
SetParent IE.Hwnd, Application.Hwnd
Do
DoEvents
Loop Until IE.READYSTATE = 4
What would be the equivalent of this on mac with safari for applescript?
I´ve seen many versions on the net but only workarounds similar to the delay or for a specify value to return or to count words on the page and so on.
I simply want the above version which is 100% perfect.
Thank you in advance:)
You can access the ready state of a page by calling "document.readyState" from either Safari or Chrome.
tell application "Safari"
if not (exists document 1) then reopen
tell current tab of window 1 to set URL to "https://stackoverflow.com/questions/24500011/how-to-wait-for-webpage-to-fully-load-before-proceeding-script"
set the_state to missing value
repeat until the_state is "complete"
set the_state to (do JavaScript "document.readyState" in document 1)
log "busy"
delay 0.2
end repeat
log "complete"
end tell
I do it in this way:
tell document 1 of application "Safari"
set URL to ""
set URL to "/slowly loading page/"
repeat until name is not "about:blank"
end repeat
end tell
Or, even easier:
tell front document of application "Safari"
set URL to "/slowly loading page/"
repeat until length of (source as text) is not 0
end repeat
end tell
Furthermore, there’s no need to do it without any delay:
repeat until length of (source as text) is not 0
delay 0.5
end repeat
Googled a bit and found this code that works for Safari Version 5.1.10 (6534.59.10) on Snow Leopard.
From http://macscripter.net/viewtopic.php?id=35176
tell application "Safari"
activate
repeat until SafariWindowHasLoaded(1) of me is true
end repeat
beep
end tell
on SafariWindowHasLoaded(inWindowIndex)
tell application "System Events" to ¬
tell application process "Safari"
set theStatusText to name of static text 1 of group 1 of window inWindowIndex as text
if theStatusText begins with "Contacting" or ¬
theStatusText begins with "Loading" or ¬
theStatusText begins with "Waiting" then
set theReturnValue to false
else
set theReturnValue to true
end if
end tell
return theReturnValue
end SafariWindowHasLoaded
Only problem I had was that this script was for an older version of Safari and the Status Bar element was in a different group than mine.
At http://hints.macworld.com/article.php?story=20071015132722688 it was suggested to change it to
static text 1 of group 2
and it worked!
I find getting the JavaScript "document.readyState" value the best way to determine if a page has finished loading, but that requires Safari to Developer > Allow JavaScript from Apple Events. Since that's is a security risk, I adjust Safari's preferences to enable it when it's needed, then disable it.
tell application "Safari" to open location ¬
"https://neeva.com/search?q=demon%20cats&c=Image&src=typedquery" -- random site
Wait_For_Safari()
--Feed cats and do other worthy things.
on Wait_For_Safari()
tell application "System Events" to set originatingApp to ¬
name of first application process where frontmost is true --in case we aren't executing from Safari
Allow_Apple_Events() --enable AllowJavaScriptFromAppleEvents so we can get JS's "document.readyState"
tell application "Safari"
set readyState to missing value
repeat until readyState is "complete" --wait for readyState = complete
set readyState to (do JavaScript "document.readyState" in document 1)
delay 0.2
end repeat
delay 0.2
end tell
Disallow_Apple_Events() --Disable AllowJavaScriptFromAppleEvents so our pants aren't down
tell application originatingApp to activate --return to wherever we started
end Wait_For_Safari
on Allow_Apple_Events()
do shell script "/usr/bin/defaults write com.apple.Safari AllowJavaScriptFromAppleEvents 1" --1 = enabled
end Allow_Apple_Events
on Disallow_Apple_Events()
do shell script "/usr/bin/defaults write com.apple.Safari AllowJavaScriptFromAppleEvents 0" --0 = disabled
end Disallow_Apple_Events

Applescript keystroke wont work in BBEdit

tell application "BBEdit"
activate
tell window 1
repeat 100 times
repeat 5 times
key code 124
end repeat
--select insertion point after character 5
keystroke return
--keystroke key code 36
end repeat
end tell
end tell
gives error
error "BBEdit got an error: Can’t get keystroke \"
\" of window 1." number -1728 from keystroke "
" of window 1
All I want to do is emulate moving the cursor 5 spaces and press the return key lots and lots of time.
Seemed simple enough...
delay 0.2
tell application "BBEdit"
activate
tell window 1
repeat 100 times
repeat 5 times
tell application "System Events"
key code 124
end tell
end repeat
tell application "System Events"
keystroke return
end tell
end repeat
end tell
end tell

Applescript run Safari in background

I have an applescript that is now doing what I want it to do: Open a specific URL, close the page and wait 2 seconds then do it again. The script works.
The problem is that when I run this on my machine, and I am trying to do something else at the same time, the Safari windows keeps popping up. I would really like to run this in the background so that I can continue to work on whatever I am doing.
The code I have is:
set theURL to "https://sites.google.com/site/whatever/"
repeat 100 times
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab --with properties {URL:theURL}
set URL of document 1 to theURL
on error
open location theURL
end try
delay 2
try
close window 1
end try
end tell
tell application "Safari"
quit
end tell
delay 1
end repeat
Anyone have a solution to this one?
Safari is coming to the front because you are activating it within the loop.
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "https://sites.google.com/site/whatever/"
delay 2
close window 1
quit
end tell
delay 1
end repeat
EDIT
set myURLs to {"https://www.google.com/", "http://www.yahoo.com/"}
repeat with aUrl in myURLs
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to aUrl
delay 2
close window 1
quit
end tell
delay 1
end repeat
end repeat

Resources