Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript: < and > a set date

Hi,

I'm wanting to get the result of "Current" if the date on the website (current eg. https://comicvine.gamespot.com/invincible-iron-man/4050-95596/ ended eg. https://comicvine.gamespot.com/howard-the-human/4050-83960/)(only month and year) comes after a set date (March 2018) and "Ended" if it comes before that date.

The Ended script runs fine, but I can't get the Current part the work unless I reverse the current and ended ifs. Is there something that I'm not doing or need to add?

Thank you in advance

tell application "Google Chrome"

repeat with x from 0 to ((executeactive tab of first windowjavascript "document.getElementsByTagName('p').length") - 1)

set theEnd to executeactive tab of first windowjavascript "document.getElementsByTagName('p')[" & x & "].outerHTML"

if theEnd contains "issue-date" then exit repeat

end repeat

set theEnd to executeactive tab of first windowjavascript "document.getElementsByTagName('p')[" & x & "].innerHTML"

set issueDate to date "Thursday, 1 March 2018 at 12:00:00 am"

if theEnd is greater than or equal to issueDate then set theResult to "Current"

if theEnd is less than issueDate then set theResult to "Ended"

end tell

MacBook Pro with Retina display, macOS High Sierra (10.13.4)

Posted on Apr 20, 2018 7:31 PM

Reply
Question marked as Best reply

Posted on Apr 23, 2018 10:15 AM

As red_ mentioned, you need to convert the text from the page to a date object in order to do any comparisons.

This is a lot trickier than it should be, but here's a revised version of your script that should do the trick.

It uses Apple's NSDataDetectors to extract the date from the string - this should be reliable for various date formats:


use framework "Foundation"

use scripting additions


tell application "Google Chrome"

repeat with x from 0 to ((execute active tab of first window javascript "document.getElementsByTagName('p').length") - 1)

set theEnd to execute active tab of first window javascript "document.getElementsByTagName('p')[" & x & "].outerHTML"

if theEnd contains "issue-date" then exit repeat

end repeat

set theEnd to execute active tab of first window javascript "document.getElementsByTagName('p')[" & x & "].innerHTML"

set e to my strToDate(theEnd)

set issueDate to (date "Thursday, March 1, 2018 at 12:00:00 AM")

if e is greater than or equal to issueDate then set theResult to "Current"

if e is less than issueDate then set theResult to "Ended"

return theResult

end tell


on strToDate(thisString) --

tell current application

set m to its ((NSDataDetector'sdataDetectorWithTypes:(its NSTextCheckingTypeDate) |error|:(missing value))'s matchesInString:thisStringoptions: 0range:{0, length of thisString})

if (count m) > 0 then

set d to (item 1 of m)'s |date|() -- get the NSDate of the first item

return d as date

end if

end tell

return "" -- no match in this string

end strToDate

7 replies

There are no replies.

AppleScript: < and > a set date

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.