Script will not run

This is a very generic question. I have a script that runs when called from the Script Editor but not when called from the drop down list on the menu bar. I used dialog boxes to try and trace where it stalls and it seems to me in the tell block that starts --Set up Stock. The display dialog in that block appears then the script stops or at least the commands following the display dialog “Symbol is " & SYM & " Will delay for 5” do not run. Obviously I am scraping data from the Web and that can have a lot of issues but as I said it works perfectly when run from the script editor.

Thanks

Peter


--Test TradeView V3 07172020
display dialog "This will close Excel if open without saving the current work book" buttons {"Exit", "Continue"}
if button returned of result is "Exit" then
	return
else
	tell application "applications:Microsoft office 2011:Microsoft Excel.app"
		activate
		close active workbook without saving
	end tell
	tell application "Safari" to quit
end if
my positionwindows()
--Calc no of stocks in the analysis
tell application "applications:Microsoft office 2011:Microsoft Excel.app"
	--To get the last used cell in a Column in this case Column 1
	set Rowcnt to (count of rows of used range of active sheet)
	tell active sheet to set LastRow to (first row index of (get end cell Rowcnt of column 1 direction toward the top)) --To find the last used cell in a Column
end tell
repeat with i from 3 to LastRow
	--Get the Stock symbol from Excel
	tell application "applications:Microsoft office 2011:Microsoft Excel.app"
		set SYM to value of cell ("A" & i)
	end tell
	--Set up Stock
	tell application "System Events"
		tell process "Safari"
			display dialog "Symbol is " & SYM & " Will delay for 5"
			delay 5
			--Select Block that accepts stock symbol
			select group 1 of text field 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
			click text field 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
			--Enter Stock symbol
			set the value of text field 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 to SYM
		end tell
	end tell
	display dialog "Set Time interval"
	--Set up Time Interval
	tell application "Safari" to activate
	display dialog "In Interval Routine"
	tell application "System Events"
		tell process "Safari"
			--1st Select the Interval Box
			set Y to 10 --The time interval box @ time this was written was 14 but could  change
			repeat 10 times
				try
					click group 1 of group Y 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
					exit repeat
				end try
				set Y to Y + 1
			end repeat
			--delay 1
			try --Group 7 is always 1 day 
				tell group 7 of group Y 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
					set {xPosition, yPosition} to position
					set {xSize, ySize} to size
				end tell
				click at {xPosition + (xSize div 2), yPosition + (ySize div 2)}
			end try
		end tell
	end tell
	--Accept Stock and Time Interval
	tell application "System Events"
		tell process "Safari"
			--Apply Date and Symbol. Twice ??
			click static text 1 of group 2 of group 28 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 0.5
			click static text 1 of group 2 of group 28 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 tell
	end tell
	--Get Sell Neutral and Buy Values
	tell application "System Events"
		tell process "Safari"
			set Y to 10
			repeat 10 times --If the 1st Group is correct the others will be the same
				try
					set Sell to value of static text 1 of group 11 of UI element 1 of scroll area 1 of group Y 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
					exit repeat
				end try
				delay 1
				set Y to Y + 1
			end repeat
			delay 3
			set Neutral to value of static text 1 of group 13 of UI element 1 of scroll area 1 of group Y 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
			set Buy to value of static text 1 of group 15 of UI element 1 of scroll area 1 of group Y 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 tell
	end tell
	activate me
	tell application "applications:Microsoft office 2011:Microsoft Excel.app"
		--display dialog SYM & "	 " & Sell & " " & Neutral & " " & Buy
		set value of cell ("AA" & i) to Sell --Sell
		set value of cell ("Ab" & i) to Neutral --Neutral
		set value of cell ("AC" & i) to Buy --Buy
		set value of cell ("AD" & i) to "1 Day" --this needs to be capable of being set as an option
		--Rank Stocks Based on Trade View	
		if value of cell ("AC" & i) > ((value of cell ("AA" & i)) + (value of cell ("AB" & i))) then
			set value of cell ("AE" & i) to 1 --Buy > Neutral + Sell
		else if value of cell ("AC" & i) > ((value of cell ("AB" & i))) then
			set value of cell ("AE" & i) to 2 --Buy > Neutral
		else if value of cell ("AC" & i) > ((value of cell ("AC" & i))) then
			set value of cell ("AE" & i) to 3 --Buy > Sell
		else if value of cell ("AA" & i) > ((value of cell ("AC" & i))) then
			set value of cell ("AE" & i) to 4 --Sell > Buy
		end if
	end tell
end repeat

on positionwindows()
	tell application "Finder"
		set allbounds to bounds of window of desktop
		set Win1 to item 1 of allbounds
		set Win2 to item 2 of allbounds
		set Win3 to item 3 of allbounds
		set Win4 to item 4 of allbounds
	end tell
	set DivCapFolder to alias "Macintosh HD:Users:petermitchell:Dropbox:Investing:DividendCapture2020:Processed:"
	tell application "Finder"
		open last item of (sort (get files of DivCapFolder) by creation date) as alias
		delay 5
	end tell
	tell application "applications:Microsoft office 2011:Microsoft Excel.app"
		set bounds of window 1 to {-1920, 23, -960, 1080}
		activate object sheet "Data"
	end tell
	tell application "Safari"
		activate
		open location "https://www.tradingview.com/widget/technical-analysis/"
		set bounds of window 1 to {-960, 0, 0, 1080}
		delay 4
	end tell
end positionwindows