• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Quickie Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Quickie Question


  • Subject: Re: Quickie Question
  • From: Arthur J Knapp <email@hidden>
  • Date: Sat, 30 Mar 2002 16:16:48 -0500

> Date: Sat, 30 Mar 2002 02:17:20 -0800
> Subject: Re: Quickie Question
> From: Chuck Ross <email@hidden>

>> Now if only someone would come up with a vanilla way to obtain
>> the month as an integer, right Emmanuel? ;-)

> I saw something like this once. ...

I'm sorry, I was making a joke. Emmanuel is credited with the
famous "French Vanilla" AppleScript routine:

set theDate to the current date
copy theDate to b
set the month of b to January
set monthNum to (1 + (theDate - b + 1314864) div 2629728)


This was then futher enhanced by someone called "SeaSoft Systems",
(I think he also goes by the name "Richard Hartman"). ;-)

Having no real life to speak of, I reviewed this and can offer a
slightly simpler solution (in the long tradition of incrementally
improving on every offering, however brilliant and well-executed). My
small modification replaces the "set monthNum..." statement above by:

set monthNum to (1 + (theDate - b) div N)

Interestingly, this works for any integer N in the range [2419200 <=
N <= 2505600], so you can pick one that suits your fancy. Proof of
this is left as an exercise for interested readers who also have no
life. (Hint: consider the number of seconds in the shortest month).


Finally, Nigel Garvey put the icing on the cake with this:

on monthNum from dateObj -- "French Vanilla" variant

-- Hack to get round bug when subtracting one pre-1904 date
-- from another.
--
copy dateObj to dateTemp
tell dateTemp's year
if it comes before 1904 then set dateTemp's year to it + 2000
end tell
--
-- End hack

copy dateTemp to dateTemp2
set month of dateTemp2 to January

return (1 + (dateTemp - dateTemp2) div 2.5E+6)
end monthNum


> ... I can't be sure, but I think it was
> done by Matt Petrowski as a FileMaker Pro calc, but I could be wrong. I
> just remembered the basic technique and translated it to AppleScript.
> If by "vanilla" you mean we can use the standard scripting additions,
> then this should work.
>
> set theDate to current date
> set monthNum to ((((offset of ((characters 1 thru 3 of ((month of
> theDate) as string))
> as string) in "JanFebMarAprMayJunJulAugSepOctNovDec") - 1) / 3) +
> 1) as integer


This funny little handler is a Nigel Garvey variation on the basic
technique that you have shown above:

property kStrMonths : "umanebarprayunulugepctovec"

on monthIndex(mon)
return (offset of (text 2 thru 3 of ("" & mon)) in kStrMonths) div 2
end monthIndex

set d to current date
monthIndex(month of d)


Finally, there is nothing wrong with a series of if-then statements,
they are actually quite fast:

on MonthNumber(m)
if (m = January) then
return 1
else if (m = February) then
return 2
else if (m = March) then
return 3
else if (m = April) then
return 4
else if (m = May) then
return 5
else if (m = June) then
return 6
else if (m = July) then
return 7
else if (m = August) then
return 8
else if (m = September) then
return 9
else if (m = October) then
return 10
else if (m = November) then
return 11
else -- if (m = December) then
return 12
else
error m
end if
end MonthNumber

set d to current date
MonthNumber(month of d)



P.S. I'll give everyone three guesses to come up with who wrote this:

on IsThisInstanceOfThisWeekdayInMonthOfThisDate(d, k, i)
return d's weekday = k and ((d's day) - 1) div 7 + 1 = i
end IsThisInstanceOfThisWeekdayInMonthOfThisDate

;-)


{ Arthur J. Knapp, of <http://www.STELLARViSIONs.com>
<mailto:email@hidden>
try
<http://www.MacScripter.net/>
on error number -128
end try
}
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Quickie Question
      • From: Applescript User Kreme <email@hidden>
  • Prev by Date: Re: Quickie Question
  • Next by Date: Re: Random Numbers fasters than the osax
  • Previous by thread: Re: Popup buttons
  • Next by thread: Re: Quickie Question
  • Index(es):
    • Date
    • Thread