dialog input that makes the user click last, current or next month which assigns the text output as March, April or May
what I would like
take the output month and convert March, April, May (or any month) to either 2, 3 or 4 (corresponding number of month) without creating a list that has all the months listed.
this way depends on the format settings in International PrefPane, this works in english
set b to button returned of (display dialog "Pick a month" buttons {"April", "May", "June"})
month of date ("1 " & b & " 2007") as integer --> 4, 5 or 6
set m to button returned of (display dialog "Choose a month:" buttons {"April", "May", "June"})
(offset of m's text 1 thru 3 in " JanFebMarAprMayJunJulAugSepOctNovDec") div 3
Or:
set m to button returned of (display dialog "Choose a month:" buttons {"April", "May", "June"})
(run script m) as integer
This is pretty cool, Kai, and it even works independent from international date format settings,
because the run script command results one of AppleScript’s built-in month constants
I had a look earlier at using the same offset technique with various language/format combinations.
A fair amount of date string parsing was needed to negotiate certain formats (such as Gaeilge/Éire and Português/Portugal/Brasil), but FWIW:
property default_month : missing value
on local_month_data()
tell «data isot3131313130313031» as date
set l to words of (it as Unicode text)
tell words of ((it + 23587200) as Unicode text)
set b to 1
repeat while (item b is l's item b)
set b to b + 1
end repeat
set e to -1
repeat while (item e is l's item e)
set e to e - 1
end repeat
end tell
set l to {}
set s to " "
repeat with n from 1 to 12
tell text from word b to word e of ((it + n * 2419200) as Unicode text)
set l's end to it
set s to s & text 1 thru 3
end tell
end repeat
{l, s}
end tell
end local_month_data
considering case
set {month_list, month_index} to local_month_data()
if default_month is missing value then set default_month to month_list's beginning
repeat
set chosen_month to choose from list month_list with prompt "Choose a month:" default items default_month
if chosen_month is false then error number -128
set default_month to chosen_month's beginning
set month_number to (offset of (default_month's text 1 thru 3) in month_index) div 3
display dialog "You chose month " & month_number & ": " & default_month & "."
end repeat
end considering
Edit: Code adjusted to incorporate some suggestions by Nigel Garvey here.