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

Question:

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

Reply
Question marked as Solved
Answer:
Answer:

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

Posted on

Question marked as Helpful

Apr 28, 2018 12:36 AM in response to Camelot In response to Camelot

Hi Camelot,


I'm a beginner with AppleScript so I really appreciate the script that you've created. I tested it and found that although the script runs it only returns "Ended" on any page you care to test it on.


So I tried the following to see what would happen and got an error saying that "The variable theResult is not defined." number -2753 from "theResult"


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"

Why would have this happened? Why is it skipping that first line? I even tried writing that part of the script a different way (below) and still the same thing.

if e is greater than or equal to issueDate then

set theResult to "Current"

else if e is less than issueDate then

set theResult to "Ended"

end if

Question marked as Helpful

Apr 28, 2018 12:36 AM in response to lgp007 In response to lgp007

I don't have Google Chrome to test, but it looks like the tested date has a day number, while the others only have the month and year. The Cocoa method used doesn't return a date from something that incomplete, so one solution would be to add a day if there isn't one. Try adding the following to the beginning of the strToDate handler:


if (offset of "," in thisString) = 0 then -- no day, so add one
  set here to (offset of space in thisString)
  tell thisString to set thisString to text 1 thru here & "1," & text here thru -1
end if

There’s more to the conversation

Read all replies

Page content loaded

Apr 20, 2018 8:32 PM in response to lgp007 In response to lgp007

You are comparing an AppleScript date to a string, so the ended part of your script isn't doing what you think it is, either. Since the date you are extracting from the web page only consists of a month and a year, you will need to add a day part and convert that to an AppleScript Date in order to compare it to your other date, or you can just compare the month numbers and year numbers.

Apr 20, 2018 8:32 PM

Reply Helpful
Question marked as Solved

Apr 23, 2018 10:15 AM in response to lgp007 In response to lgp007

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

Apr 23, 2018 10:15 AM

Reply Helpful
Question marked as Helpful

Apr 28, 2018 12:36 AM in response to Camelot In response to Camelot

Hi Camelot,


I'm a beginner with AppleScript so I really appreciate the script that you've created. I tested it and found that although the script runs it only returns "Ended" on any page you care to test it on.


So I tried the following to see what would happen and got an error saying that "The variable theResult is not defined." number -2753 from "theResult"


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"

Why would have this happened? Why is it skipping that first line? I even tried writing that part of the script a different way (below) and still the same thing.

if e is greater than or equal to issueDate then

set theResult to "Current"

else if e is less than issueDate then

set theResult to "Ended"

end if

Apr 28, 2018 12:36 AM

Reply Helpful (1)

Apr 23, 2018 10:35 PM in response to lgp007 In response to lgp007

The script works as expected as far as I can tell.


I don't really know the pages you're pulling content from - I just pulled the first URL you posted: https://comicvine.gamespot.com/howard-the-human/4050-83960/


This appears to have a theEnd date of October 31st 2015.

Comparing this to the issueDate of 'Thursday, March 1, 2018 at 12:00:00 AM' date results in 'Ended'.

If I change the comparison issueDate date to some time in 2013 I get 'Current'.


Based on my understanding, that seems to make sense to me. Maybe there's some other logic that's at fault here?

Apr 23, 2018 10:35 PM

Reply Helpful

Apr 24, 2018 12:49 AM in response to Camelot In response to Camelot

the link you used there should indeed result in "Ended".


here's an example of a page that should result in "Current" https://comicvine.gamespot.com/invincible-iron-man/4050-95596/ but doesn't. Is it maybe reading the listing of issue 1 instead of the last issue issued?


P.S. the script results in "Ended" no matter what page I run it on. even this page results in "Ended". Maybe this line (if theEnd contains "issue-date" then exit repeat) isn't getting registered or something?

Apr 24, 2018 12:49 AM

Reply Helpful
Question marked as Helpful

Apr 28, 2018 12:36 AM in response to lgp007 In response to lgp007

I don't have Google Chrome to test, but it looks like the tested date has a day number, while the others only have the month and year. The Cocoa method used doesn't return a date from something that incomplete, so one solution would be to add a day if there isn't one. Try adding the following to the beginning of the strToDate handler:


if (offset of "," in thisString) = 0 then -- no day, so add one
  set here to (offset of space in thisString)
  tell thisString to set thisString to text 1 thru here & "1," & text here thru -1
end if

Apr 28, 2018 12:36 AM

Reply Helpful (1)

Apr 28, 2018 1:27 AM in response to red_menace In response to red_menace

Thanks, Red for that last little bit of script. it all works brilliantly now 🙂 and below is the complete script for anyone else


use framework "Foundation"

use scripting additions


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 e to my strToDate(theEnd)

set issueDate to (date "Thursday, 1 March 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)

if (offsetof "," inthisString) = 0 then -- no day, so add one

set here to (offsetofspaceinthisString)

tell thisString to set thisString to text 1 thru here & "1," & texthere thru -1

end if

tell current application

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

if (countm) > 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

Apr 28, 2018 1:27 AM

Reply Helpful
User profile for user: lgp007

Question: AppleScript: < and > a set date