Help with moving @start date AppleScript to JavaScript

Brett Terpstra created some date scripts back in 2010 to create a @start tag (if none is present) and if one is, the script would increment or decrement the date by a day.

Can anyone help me move these to JavaScript?

This one increments the date:

tell application "TaskPaper"
	repeat with _task in «class TPse» as list
		if exists «class TPtg» named "start" of _task then
			set _datestring to («class TGvl» of («class TPtg» named "start" of _task))
			set _fmt to (text items 6 thru 7 of _datestring) & "-" & (text items 9 thru 10 of _datestring) & "-" & (text items 1 thru 4 of _datestring) as string
			set _val to (my getDate(_fmt)) + (1 * days) as string
			set «class TGvl» of («class TPtg» named "start" of _task) to my formatDate(_val)
		else
			tell _task to make «class TPtg» with properties {name:"start", «class TGvl»:my formatDate(date string of (current date))}
		end if
	end repeat
end tell

on formatDate(_date)
	set theDate to date _date
	set _year to theDate's year
	copy theDate to b
	set the month of b to January
	set _month to (1 + (theDate - b + 1314864) div 2629728)
	if (count of text items of (_month as string)) < 2 then set _month to "0" & (_month as string)
	set _day to theDate's day
	if (count of text items of (_day as string)) < 2 then set _day to "0" & (_day as string)
	set _fmt to _year & "-" & _month & "-" & _day as string
	return _fmt
end formatDate

on getDate(_fmt)
	return date _fmt
end getDate`

And this one decrements the date:

tell application "TaskPaper"
	repeat with _task in «class TPse» as list
		if exists «class TPtg» named "start" of _task then
			set _datestring to («class TGvl» of («class TPtg» named "start" of _task))
			set _fmt to (text items 6 thru 7 of _datestring) & "-" & (text items 9 thru 10 of _datestring) & "-" & (text items 1 thru 4 of _datestring) as string
			set _val to (my getDate(_fmt)) - (1 * days) as string
			set «class TGvl» of («class TPtg» named "start" of _task) to my formatDate(_val)
		else
			tell _task to make «class TPtg» with properties {name:"start", «class TGvl»:my formatDate(date string of (current date))}
		end if
	end repeat
end tell

on formatDate(_date)
	set theDate to date _date
	set _year to theDate's year
	copy theDate to b
	set the month of b to January
	set _month to (1 + (theDate - b + 1314864) div 2629728)
	if (count of text items of (_month as string)) < 2 then set _month to "0" & (_month as string)
	set _day to theDate's day
	if (count of text items of (_day as string)) < 2 then set _day to "0" & (_day as string)
	set _fmt to _year & "-" & _month & "-" & _day as string
	return _fmt
end formatDate

on getDate(_fmt)
	return date _fmt
end getDate

( Wednesday 13th is going to be a little busy for me, but I should be able to look at that on Thurs evening, if no-one else has sketched translations by then )

1 Like

Thank you @complexpoint—I appreciate it!

@complexpoint is going to be embarrassed by how much quicker I get my script done then he does! :smile: Type type type…

2 Likes

Both of you rock—greatly appreciated!

1 Like

Alas, I’m completely outclassed there :slight_smile:

1 Like

For anyone looking for the scripts that were created in response to this topic, they are here:

1 Like