Date Conversions

I am comparing a date obtained from a web site with one on an Excel spreadsheet. Depending. on conditions I want to know if the next date on the web set is <> or less than the one form the Excel spreadsheet. My issue is that the code below works OK as long as I only need to check one of the drop down dates ( the next one) but if that date does not fit I need to check the next date in the list . The issue stems from the fact that I need to covert the web date into one AppleScript understands. It seems I have to do that outside the tell block that returns the web date. I have not been able to figure out the loop to make this work. this is the code.

tell application "System Events" to tell process "Safari"
	set EndStr to 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
	repeat with ODate from 10 to 50 --Get Group of Option Date on web page
		try
			set OptDate to value of pop up button 1 of group 2 of group ODate of EndStr --Location of Option date
			exit repeat
		end try
	end repeat
end tell
tell application "Microsoft Excel" to set Exdivdate to value of cell ("E3") --For Testing it is in date format
set OptDate to date OptDate --To ensure Applescript will treat the Web date as one it can process

tell application "System Events" to tell process "Safari"
	log OptDate & " " & Exdivdate
	if Exdivdate < OptDate is true then --Option has to be after the ex dividend date
		click pop up button 1 of group 2 of group ODate of EndStr --Opens the Date Drop down box
		key code 125 --Move down next date
		key code 36 --accept date
	end if
end tell

Can you post the URL for the website or is it proprietary?

When I have these issues I use an appleScript handler.

On FixDate (anyDate)
---Do your stuff
return anyDate
end

Then, inside the Excel tell you can do this:

set fixedDate to my FixDate(dateFromExcell)

No, its finance.yahoo I set the url to include the stock I am looking at then switch to the option page that is the page with the drop down box for the next option date that some times I want to increase.

It’s not the excel date that is the problem as that does not change at least when I am looking at a stock. It’s the dropdown box on the web page that changes and I tried using a handler and that did not work but will now try again. thank you

Try this…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local datePopup, Exdivdate, OptDate, dateList, foundFlag
tell application "Microsoft Excel" to set Exdivdate to value of cell ("E3") --For Testing it is in date format
if class of Exdivdate ≠ date then
	if class of Exdivdate = text then
		try
			set Exdivdate to date Exdivdate
		on error
			return
		end try
	else
		return
	end if
end if
tell application "System Events" to tell process "Safari"
	set EndStr to 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
	repeat with ODate from 10 to 50 --Get Group of Option Date on web page
		try
			set datePopup to pop up button 1 of group 2 of group ODate of EndStr --Location of Option date
			exit repeat
		end try
	end repeat
	set OptDate to value of datePopup
	try
		tell me to set OptDate to date OptDate
	on error
		return
	end try
	if Exdivdate ≥ OptDate then -- Option has to be after the ex dividend date
		set foundFlag to false
		click datePopup
		set dateList to name of UI elements of menu 1 of group 1 of tab group 1 of splitter group 1 of window 1
		repeat with i from 1 to count dateList --while Exdivdate > OptDate
			try
				tell me to set OptDate to date (item i of dateList)
			on error
				beep
			end try
			if Exdivdate < OptDate then
				set foundFlag to true
				exit repeat
			end if
		end repeat
		if foundFlag then click menu item i of menu 1 of group 1 of tab group 1 of splitter group 1 of window 1
	end if
end tell

FYI, here’s the handler I use to convert dates in text to appleScript dates:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"

set dateList to {"June 6, 1941", "April 1, 1986", "January 1, 2024", "hamburger"}
set detectedDates to my DetectDates(dateList)

on DetectDates(textWithDate)
   set dateDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeDate) |error|:(missing value)
   set datesFromText to {}
   repeat with thisTextWithDate in (textWithDate as list)
      try
         set matchedDate to (dateDetector's firstMatchInString:thisTextWithDate options:0 range:{location:0, |length|:thisTextWithDate's length})
         
         set DetectedDate to matchedDate's |date| as date
         set end of datesFromText to (DetectedDate)
      on error
         set the end of datesFromText to "Error: " & thisTextWithDate
      end try
   end repeat
   return datesFromText
end DetectDates

Thanks very much it stopped at the “set dateList to name of --”. I have not been able to find a way other than reading each line of the yahoo table into a list to address all the possibilities. However my initial error of not being able to include it in a loop seems to be answered by the command I just did not know about " tell me to set",hopefully I will be able to sort myself out.

Thank you again

What was the error?
I could be it needs a delay after the click to give it time to create the pop up menu.
Try adding a delay of half a second

Delay 0.5

Don’t think that’s it but thank you for the suggestion . Your code ends with the drop down box open . When I get it fixed I will post result here or I will humbly ask for help again. You will note I used key codes to move the selection down and select it. Now you have shown. me how to fix the loop would like to think that is a simple task

Weird. It works for me.

I could be a difference in the URL we are both using.
Here is the URL I’m using for testing…

https://finance.yahoo.com/quote/AAPL/options?date=1702598400&p=AAPL