Cannot Run 2 Routines in a Single script

I have asked about these routines before and for that I apologise, but I have for me at least different type of problem. I have 2 routines which if I run separately work but if I combine them as two routines in one script they do not.


my CloseSafari()
tell application "Safari" to quit
delay 4
tell application "Safari" to tell window 1 to tell current tab to set URL to "http://www.dividend.com/login/"
delay 4
my GetDividendStocks()


on CloseSafari()
	tell application "Safari"
		activate
		tell application "System Events" to tell application process "Safari" --User
			try --the three line icon on left of page
				click button 1 of group 1 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
			end try
			delay 2
			try
				--clicks "Logout" item in drop dwn box
				click static text "Logout" of UI element "Logout" of group 5 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
				delay 2
			end try
			tell application "Safari" to tell window 1 to tell current tab to set URL to "http://www.dividend.com/login/" --Home Page
			delay 3
			click button "Reload this page" of group 3 of toolbar 1 of window 1
		end tell
	end tell
	delay 2
end CloseSafari

--LoginDividend.com V5 01/312024
--a Revision putting "User" and "Password" into a seperate tell blocks seems to work
--This works but only if Dividend.com is open and on start page 
--Working on loging out then in
on GetDividendStocks()
	tell application "Safari"
		activate
		--tell application "System Events" to tell application process "Safari" to set MidUrl 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
		tell application "System Events" to tell application process "Safari" --User
			set MidUrl 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
			repeat 4 times
				try
					
					set value of text field 1 of group 6 of MidUrl to "mitchbvi@mac.com"
					exit repeat
				end try
				delay 1
			end repeat
		end tell
		delay 1
		tell application "System Events" to tell application process "Safari" --Password
			set value of text field 1 of group 2 of group 6 of MidUrl to "Div10182022#"
		end tell
		delay 1
		tell application "System Events" to tell application process "Safari"
			click button "Log in" of group 3 of group 6 of MidUrl
			delay 4
			--Had to change "MidURL" definition to include new group number
			set MidUrl to group 10 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
			click static text "Best Dividend Capture Stocks" of UI element "Best Dividend Capture Stocks" of MidUrl
			delay 5
			--Open "All Filters" box
			click button 2 of group 8 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1 --"Best Dividend Capture Stocks in January - Dividend.com"
			--end tell
		end tell
	end tell
end GetDividendStocks

I built the “close safari” routine in case for some reason I was still logged into the Dividend.com site. These routines are part of a larger program. If I make sure I am logged out of Dividend.com the second routine works. As you can see I have tried quitting “safari” and re-opening it between the 2 routines also using a try block with a 1 second delay in case it is a timing problem. I created the variable “MidUrl” to reduce size of script but as the routine “GetDividendStocks” works when run by itself that is I think irrelevant.

Wow there is a lot wrong in this script.
First why are you closing Safari right before you wan too interact with a Window in Safari.
Second, try at all costs from using nested tells.
In you “Close_Safari()” script do this as an example…

on CloseSafari()
	tell application "Safari" to activate
	tell application "System Events" to tell application process "Safari" --User
		try --the three line icon on left of page
			click button 1 of group 1 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
		end try
		delay 2
		try
			--clicks "Logout" item in drop dwn box
			click static text "Logout" of UI element "Logout" of group 5 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
			delay 2
		end try
		tell application "Safari" to tell window 1 to tell current tab to set URL to "http://www.dividend.com/login/" --Home Page
		delay 3
		click button "Reload this page" of group 3 of toolbar 1 of window 1
	end tell
	delay 2
end CloseSafari

or you can get rid of the need for two tell blocks by doing this…

on CloseSafari()
	tell application "System Events" to tell application process "Safari" --User
		set frontmost to true -- this will do the same as activate from before
		try --the three line icon on left of page
			click button 1 of group 1 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
		end try
		delay 2
		try
			--clicks "Logout" item in drop dwn box
			click static text "Logout" of UI element "Logout" of group 5 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
			delay 2
		end try
		tell application "Safari" to tell window 1 to tell current tab to set URL to "http://www.dividend.com/login/" --Home Page
		delay 3
		click button "Reload this page" of group 3 of toolbar 1 of window 1
	end tell
	delay 2
end CloseSafari

I have lots more to figure out but I’ll wait for your response as to why you close?

You don’t need to click the “Reload this page” button after you change the URL. Safari will load it anyways.

Also why did you reload the URL when you were just going to quit Safari?

Here’s what I have so far just for the CloseSafari() handler

on CloseSafari()
	local i, myURL, flag, subElement
	tell application "Safari" to set myURL to URL of current tab of window 1
	if myURL does not start with "https://www.dividend.com/premium/" then return false
	tell application "System Events" to tell application process "Safari" --User
		set frontmost to true
		set subElement 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
		try --the three line icon on left of page
			click button 1 of group 1 of group 1 of subElement
		on error
			return false
		end try
		repeat with i from 15 to 0 by -1
			if exists static text "Logout" of UI element "Logout" of group 5 of subElement then exit repeat
			delay 0.2
		end repeat
		set flag to false
		if i ≠ 0 then -- didn't time-out, so "Logout" exists
			--clicks "Logout" item in drop-down box
			click static text "Logout" of UI element "Logout" of group 5 of subElement
			set flag to true
		end if
	end tell
	if flag then waitPageLoaded()
	tell application "Safari" to tell window 1 to tell current tab to set URL to "http://www.dividend.com/login/" --Home Page
	waitPageLoaded() -- waits for webpage to finish loading
	return true
end CloseSafari

on waitPageLoaded()
	tell application "System Events" to tell application process "Safari"
		repeat until exists (button 1 of groups of toolbar 1 of window 1 whose title = "Reload this page")
			delay 0.3
		end repeat
	end tell
end waitPageLoaded

The close handler arose because if I had logged on to Dividend.com and not logged off I had to close it even if in the meantime I had closed Safari. If I start from scratch with Dividend.com opened to the login page this does what I want.

--LoginDividend.com V5 01/312024
--a Revision putting "User" and "Password" into a seperate tell blocks seems to work
--This works but only if Dividend.com is open and on start page 
--Working on loging out then in
tell application "Safari"
	activate
	tell application "System Events" to tell application process "Safari" --User
		click button "Reload this page" of group 3 of toolbar 1 of window 1
		set MidUrl 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
		set value of text field 1 of group 6 of MidUrl to "mitchbvi@mac.com"
	end tell
	delay 1
	tell application "System Events" to tell application process "Safari" --Password
		set value of text field 1 of group 2 of group 6 of MidUrl to "#######"
	end tell
	delay 1
	tell application "System Events" to tell application process "Safari"
		click button "Log in" of group 3 of group 6 of MidUrl
		delay 4
		--Had to change "MidURL" definition to include new group number
		set MidUrl to group 10 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1
		click static text "Best Dividend Capture Stocks" of UI element "Best Dividend Capture Stocks" of MidUrl
		delay 5
		--Open "All Filters" box
		click button 2 of group 8 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window 1 --"Best Dividend Capture Stocks in January - Dividend.com"
	end tell
end tell

I think the problem is I need to open a dropdown box from the user name, and then complete the data. I have tried code the clicks the box but that does not work. It may sound nuts but if I start on the site and click manually on th 1st box and then run the code above it works

Since my last response I have been working on a solution without success. However I think I can make the problem simpler. As the reader will have seen to access the web site I am using requires a user name and password. What I have found is that I cannot always even enter the user name. In fact if I start with the webpage open in most cases I can enter the user name with code. Sometimes If I have run that more than once I need to refresh the web page manually. This is the code that works with Safari open to the login page for Dividend.com.

tell application "System Events" to tell application process "Safari" --User
	set MidUrl 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
	set value of text field 1 of group 6 of MidUrl to "mitchbvi@mac.com"
end tell

What I just cannot work out is how to open the web page and run that code.

I am sure you are correct about using another platform but at this point I am heavily invested in what I have written, of which this is a small part. So I have found a work around to my issue but I do not understand why it works so if I can impose once again maybe you can tell me.
By opening the Display Dialog box after opening the dividend.com web site I can enter the user name .

More confusing for me is that I have to use a shell script to launch the web site using AppleScript does not work!

Thank you for your help, I used to think hard wiring program boards for IBM machines was difficult

do shell script "open -a Safari 'http://www.dividend.com/login/'"
delay 4
activate "Scrip_debugger"
display dialog " "
tell application "Safari" to activate
delay 2
tell application "System Events" to tell application process "Safari" --User
	set MidUrl 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
	set value of text field 1 of group 6 of MidUrl to "mitchbvi@mac.com"
end tell

1st it is worth something would want to be sure where ever it went to, it was to you.

2nd : However as of now still not working. It stops at “set focused to true” and error " index is out of range etc" . I checked and it seems that the group can change so even when I get it going will have to set a loop. When I checked it had changed to group 9 so tried that, then back to 6 and then 5 as per your code none of that worked.

BTW I get references from UI browser as looking at the HTML always seems more difficult for me, is that where you source the reference from?

Don’t know if it’s relevant but I am using “Script Debugger”.

Wanted to respond but now will shut everything down and try again.

Thanks