I am a novice, I am not a programmer but have been able to get AppleScript to do quite a bit for me, mostly by trial and error. I am in need of some help accomplishing two things:
How do I set this property to return a month name (May) vs. a month number(5)
property last_month : (month of (current date)) - 1
How do set this property to have a leading zero if the month number is less than zero (06)
property email_subject_month : month of (current date) as integer
Here is the beginning of the script, any other feedback is welcome and appreciated.
Thank you,
Mike
Run on the first workday of every month
set image_path to (path to the desktop folder as string)
tell application “Finder” to set email_attachment to items of folder image_path whose name is “Re-Price Showroom.png”
property current_month : month of (current date)
property last_month : (month of (current date)) - 1
property email_subject_month : month of (current date) as integer
property email_subject_year : year of (current date) as integer
property email_subject : “(” & email_subject_year & “-” & email_subject_month & “) Re-Pricing Showroom”
property email_body : "Welcome to " & current_month & "! " & return & return & ¬
"As a reminder, please ensure to re-price those items that have changed lease rates for " & current_month & ", and those prices now expired from " & last_month & "
tell application “Mail”
set new_email to make new outgoing message with properties {subject:email_subject, content:email_body}
tell new_email
end tell
end tell
set textMonth to item ((month of (current date)) - 1) of {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
set paddedMonth to text -2 through -1 of ("0" & (month of (current date) as number))
Or a handler for the text month conversion with some error handling. You won’t need error handling if you’re calling Applescript’s “date” functionality to get the month, but this has it if you’re getting the month number some other way.
set textMonth to month_numeral_to_text((month of (current date)) - 1)
on month_numeral_to_text(monthNumber)
try
set monthNumber to monthNumber as integer
on error
display dialog "The handler \"month_numeral_to_text\" Expected numeric input." buttons {"Cancel"} default button "Cancel"
end try
if monthNumber > 12 then display dialog "The handler \"month_numeral_to_text\" received a month value greater than 12." buttons {"Cancel"} default button "Cancel"
set theMonth to item monthNumber of {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
end month_numeral_to_text
Please use the “applescript” tags when posting Applescript.
You won’t want to use properties to hold the date values because property values are set when a script’s compiled, not when it’s run (unless you write code into the script to change the values at run time). Normal run-handler variables should be OK here.
set now to (current date) -- Call this function just once for efficiency and in case the date changes while the script's running!
set current_month to now's month
set last_month to month of (now - (now's day) * days) -- This produces December in January.
set email_subject_month to current_month as integer
set email_subject_year to now's year
set email_subject to "(" & email_subject_year & "-" & email_subject_month & ") Re-Pricing Showroom"
set email_body to "Welcome to " & current_month & "! " & return & return & ¬
"As a reminder, please ensure to re-price those items that have changed lease rates for " & current_month & ", and those prices now expired from " & last_month & "."
Since the month constants are being spliced into text here, they’re coerced to text automatically, giving the month names in English.