Thanks. That’s why I submitted it since I knew that it may not work for like leap years or varing months. just don’t know how to get the current number of days in the month through applescript.
Kai’s script gets the last day of last month relative to the current date. To get next month you need to use the index of the current month and add 1. For instance, this month has index 6. I’m using Jaguar, but in newer versions of os, there are easier ways to get the index. Here’s how I might do it in Jaguar:
set m_list to ¬
{January, February, March, April, May, June, July, August, September, October, November, December}
set m to month of (current date)
repeat with mi from 1 to 12
set list_m to item mi of m_list
if list_m is m then exit repeat
end repeat
-- 12 + 1 = 13
set next_m to (mi mod 12) + 1
set as_m to item next_m of m_list
set i_list to {}
repeat with i from 0 to 24
set end of i_list to GetNextMonth(i)
end repeat
return i_list
--
on GetNextMonth(n)
set m_list to ¬
{January, February, March, April, May, June, July, August, September, October, November, December}
set m to month of (current date)
repeat with mi from 1 to 12
set list_m to item mi of m_list
if list_m is m then exit repeat
end repeat
set next_m to (mi + n) mod 12
if next_m is 0 then set next_m to 12
set as_m to item next_m of m_list
return as_m
end GetNextMonth
If you want a modular subroutine, then you might make it so you can use negative numbers for previous months.
Hi, kel. On the font page of this site (ie. MacScripter.net) at the moment, there’s an article by Adam in which he’s been kind enough to quote several scripts from my DateTips package in ScriptBuilders. There’s one there that does calendar month arithmetic and which might serve the turn. (Get the month of the date it returns.)
Thanks anyway, but I finally got a formula that works to get n months ago or after:
set l to {}
repeat with n from -26 to 26
set n to (n + ((((n div 12) ^ 2) ^ 0.5) + 1) * 12) mod 12
if n is 0 then set n to 12
set end of l to n
end repeat
return l
set l to {}
repeat with i from -25 to 25
set end of l to GetMonthFromCurrent(i) -- i relative to current month index
end repeat
return l
--
on GetMonthFromCurrent(n)
set m_list to {January, February, March, April, May, June, July, August, September, October, November, December}
set cur_date to (current date)
set cur_m to month of cur_date
repeat with m from 1 to 12
set list_m to item m of m_list
if list_m is cur_m then exit repeat
end repeat
set n to ((n + ((((n div 12) ^ 2) ^ 0.5) + 1) * 12) + m) mod 12
if n is 0 then set n to 12
return (item n of m_list) as string
end GetMonthFromCurrent
Nigel, I’ll check out your subroutines later. Need to go somewhere.
GetMonthFromCurrent(1)
on GetMonthFromCurrent(n)
if n < 0 then set n to n + 1
tell (current date) to return (month of (it + (32 * n - (its day)) * days)) as string
end GetMonthFromCurrent
GetMonthFromDate(-1, date "Sunday, 29 February 2004 12:00:0 AM")
on GetMonthFromDate(n, _date)
if n < 0 then set n to n + 1
tell _date to return (month of (it + (32 * n - (its day)) * days)) as string
end GetMonthFromDate
Hi, Bruce. That works very well up to about 18 or 19 months either side of the reference date. Beyond that, too many 32s are added into the mix and it begins to overshoot the target month. Since the handler’s only after the month rather than the precise date, it can mod n by 12 when it receives it. It also needs to special-case n parameter values of 0, so that it returns the current month rather than the previous one. (I don’t think anyone’s specifically going to call it with a parameter of 0, but if it’s called in a repeat as in Kel’s script, it’ll need to cope.)
on GetMonthFromCurrent(n)
set n to n mod 12
if n is 0 then return (month of (current date)) as string
if n < 0 then set n to n + 1
tell (current date) to return (month of (it + (32 * n - (its day)) * days)) as string
end GetMonthFromCurrent
I had to write that article in a rush as I had had pneumonia during the time I would normally have consulted with Nigel about it. I’ve found his Date&Time scripts to be endlessly useful, both because they do very clever things and because I’ve needed to make some of those calculations to write scripts for iCal to do things that iCal doesn’t do natively.