Convert todays date to Roman Numerals

Hey Bill,

A good learning exercise. :smile:

But since you’re doing all the heavy-lifting in AppleScript let’s simplify just a bit:

try
  
  tell (current date)
    set yearInt to its year
    set monthInt to its month as integer
    set dayInt to its day
  end tell
  set RomanYear to ""
  repeat with i from 1 to (count (yearInt as string))
    set RomanYear to item (((item -i of (yearInt as string)) as integer) + 1) of item i of ¬
      {{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, ¬
        {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, ¬
        {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, ¬
        {"", "M", "MM", "MMM"}} & RomanYear
  end repeat
  set RomanMonth to item monthInt of {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XII", "XII"}
  set RomanDay to item dayInt of {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XXI", "XXII", "XXXII", "XXIV", "XXV", "XXVI", "XXVII", "XXVIII", "XXIX", "XXX", "XXXI"}
  set the clipboard to RomanDay & "-" & RomanMonth & "-" & RomanYear
  
on error e number n
  set e to e & return & return & "Num: " & n
  if n ≠ -128 then
    try
      tell current application to button returned of ¬
        (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
          default button "OK" giving up after 30)
      if ddButton = "Copy" then set the clipboard to e
    end try
  end if
end try

This requires only one ‘Execute AppleScript Action’, and the AppleScript itself will set the clipboard.

Of course this is only for today’s date (current date).

If you want to be able to enter a custom date you can add some code similar to this to the beginning of the script:

try
  
  tell (current date)
    set yearInt to its year
    set monthInt to its month as integer
    set dayInt to its day
  end tell
  
  set defaultDateString to (yearInt & "/" & monthInt & "/" & dayInt) as text
  
  tell current application
    set userDateString to text returned of (display dialog "Enter a date of the form: YYYY/MM/DD" default answer defaultDateString as text)
  end tell
  
  if userDateString ≠ defaultDateString then
    set AppleScript's text item delimiters to "/"
    set {yearInt, monthInt, dayInt} to text items of userDateString
  end if
  
on error e number n
  set e to e & return & return & "Num: " & n
  if n ≠ -128 then
    try
      tell current application to button returned of ¬
        (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
          default button "OK" giving up after 30)
      if ddButton = "Copy" then set the clipboard to e
    end try
  end if
end try

You can also use the ‘Prompt for User Input’ to use KM instead of AppleScript to ask the user for a custom date string, but I won’t get into that at the moment.

-Chris

1 Like