I edited my previous script to be way better.
It now will create a tab or window if needed then go to the URL
It will wait till the page is done loading.
Then it will find email & password groups and fill them out. in my case it generates a random name and password since I don’t have an account
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- https://www.dividend.com/login/?redirect_url=/login
property myURL : "https://www.dividend.com/login/?redirect_url=/login"
property eNames : {"Robert", "Mark", "Steven", "Joseph", "Anthony", "Edward", "James", "Ethan", "Henry", "Carlos", "Noah", "Arthur", "Louis", "Zachary", "Thomas", "Alexander", "Jacob", "Vincent", "Dylan", "William", "Jack", "Samuel", "Andrew", "Gabriel", "Benjamin", "Lawrence", "Christopher", "Matthew", "George", "Nicholas", "Daniel", "Nathan", "Peter", "Paul", "Harold", "Adam", "Charles", "Aaron", "Kenneth", "Walter", "Patrick", "Philip", "Austin", "Jesse", "Ryan", "Bruce", "Albert", "Michael", "Eugene", "David", "Eric", "Jonathan", "Frank", "Paul", "Jason", "Brian", "Richard", "Jordan", "Keith", "Kevin", "Russell", "Roger", "Jeremy", "Justin", "Timothy", "Greg", "Alan", "Brandon", "Tyler", "Scott", "Wayne", "Jeffrey", "Dennis", "Douglas", "Roy", "Sean", "Carl", "Ronald", "Donald", "Gerald", "Gary", "Randolph", "Terry"}
on run
local myTab, myURLs, UIElements, subElements, loginPane, anElement, anItem, i, uname, emailFlag, pwdFlag, tmp
tell application "Safari"
if it is not running then activate it
set tmp to windows
if (count tmp) = 0 then -- Safari is running but has no windows
make new document
set myTab to current tab of window 1
set URL of myTab to myURL
else
set myURLs to URL of tabs of window 1
if myURL is in myURLs then -- check to see if one of it's tabs already has the URL
repeat with i from 1 to count myURLs
if (item i of myURLs) = myURL then
exit repeat
end if
end repeat
if current tab of window 1 ≠ tab i of window 1 then
set current tab of window 1 to tab i of window 1
end if
else -- no tab or window with URL was found
if (source of current tab of window 1) = "" then -- empty page found, so load URL
set myTab to current tab of window 1
else -- no empty window found, so create a new tab
set myTab to (make new tab at window 1)
set current tab of window 1 to myTab
end if
set URL of myTab to myURL
end if
end if
repeat
if (name of window 1) = "Login - Dividend.com - Dividend.com" then exit repeat
delay 0.2
end repeat
end tell
tell application "System Events"
set anElement to application process "Safari"
set subElements to groups of toolbar 1 of window 1 of anElement -- test if page done loading
repeat with i in subElements -- find group in toolbar that has 'Reload' button
set tmp to contents of i
if exists (button "Add page to Reading List" of tmp) then exit repeat
end repeat
tell tmp
repeat until exists button "Reload this page"
delay 0.2
end repeat
end tell
set flag to false
repeat until flag
try
window 1 of anElement
set anElement to UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1 of anElement
--get anElement
set flag to true
on error --errMsg number errNum
delay 0.2
end try
if anElement = missing value then set flag to false
end repeat
--tell me to display alert "anElement " & ((class of anElement) as text)
set flag to false
repeat until flag
try
set subElements to groups of anElement whose subrole is "AXLandmarkMain"
--get subElements
set flag to true
on error --errMsg number errNum
beep 1
end try
if (subElements = missing value) or ((count subElements) = 0) then set flag to false
if flag then
set loginPane to item 1 of subElements
try
set subElements to groups of loginPane whose subrole is not "AXEmptyGroup"
on error
set flag to false
end try
if (count subElements) = 0 then set flag to false
if not flag then delay 0.2
end if
end repeat
--tell me to display alert "Flags " & (count of subElements)
set emailFlag to false -- **EDIT** forgot to initialize the email flag
repeat with i in subElements -- finds the Email group
set i to contents of i
if (static text 1 of i) exists then
if (value of static text 1 of i) = "Email" then
set anItem to text field 1 of i
set focused of anItem to true
tell me to delay 0.2
set tmp to value of anItem
set uname to tmp
repeat while uname = tmp
set uname to some item of eNames
end repeat
set value of anItem to uname --"Robert"
set emailFlag to true
set focused of text field 1 of i to true -- screen refreshes to remove 'This field is required' notice (if there is one)
tell me to delay 0.2
exit repeat
end if
end if
end repeat
set pwdFlag to false
set subElements to groups of loginPane whose subrole is not "AXEmptyGroup"
repeat with i in subElements -- finds the Password group
set i to contents of i
if (static text 1 of i) exists then
if (value of static text 1 of i) = "Password" then
set anItem to text field 1 of i
set focused of anItem to true
tell me to delay 0.2
set value of anItem to my getPWD() --"mypwd"
set pwdFlag to true
exit repeat
end if
end if
end repeat
if emailFlag and pwdFlag then
repeat with i in subElements -- finds the Login button
set i to contents of i
if (button "Log in" of i) exists then
tell button "Log in" of i to perform action "AXPress"
exit repeat
end if
end repeat
end if
end tell
end run
on getPWD()
local pwd, c
set pwd to ""
set c to random number from 6 to 12
repeat with i from 1 to c
set pwd to pwd & some character of "abcdeefghijklmnopqrstuvwxyz!#$%^*-+?~"
end repeat
end getPWD