Hi,
Very often I need to calculate dates for filling documents with specific informations from our database.
Lately I got documents from a client, which only included the week number in their file name, not the first date of the week (e.g. ‘data_w19_analysis.indd’ vs. ‘data_230410_analysis.indd’).
So how to get the dates of a given week number to fill the documents?
Finally I came up with the solution below, which includes calling a Python script doing the date calculations.
To test the script, you will need to download the Python script and place it on your desktop.
The handler «getdatesforweeknum» offers several options to customize the output (first weekday, week number mode, etc.).
Happy scripting!
property mytitle : "Dates of a Week Number"
-- 1. example: get all dates of a week
set yearstr to "2010"
set weeknumstr to "52"
set {weekdates, errmsg} to my getdatesforweeknum(yearstr, weeknumstr, missing value)
if weekdates is false then
my dsperrmsg(errmsg, "--")
return
else
choose from list weekdates with prompt "Dates of the week " & weeknumstr & "/" & yearstr with empty selection allowed
end if
-- 2. example: Thursday of the given week
set {weekdates, errmsg} to my getdatesforweeknum(yearstr, weeknumstr, "3")
if weekdates is false then
my dsperrmsg(errmsg, "--")
return
else
choose from list weekdates with prompt "Thursday of week " & weeknumstr & "/" & yearstr with empty selection allowed
end if
-- I am returning the dates of a certain week number in a given year
on getdatesforweeknum(yearstr, weeknumstr, weekdaynumstr)
try
-- firstweekday specifies the first day of the week
-- "0" is Monday (the default), "6" is Sunday.
set firstweekday to "6"
-- "U":
-- Week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
-- All days in a new year preceding the first Sunday are considered to be in week 0.
--
-- "W":
-- Week number of the year (Monday as the first day of the week) as a decimal number [00,53].
-- All days in a new year preceding the first Monday are considered to be in week 0.
set weekmode to "U"
-- "1": Monday is 1 and Sunday is 7
-- "2": Monday is 0 and Sunday is 6
set weekdaymode to "2"
-- you can put the Python script wherever you want, just set
-- the toolpath variable accordingly
set toolpath to ((path to desktop as text) & "weeknums.py") as text
set qtdtoolpath to quoted form of POSIX path of toolpath
set command to "/usr/bin/python " & qtdtoolpath & " -y " & yearstr & " -f " & firstweekday & " -w " & weekmode & " -d " & weekdaymode
set output to do shell script command
set outputlines to paragraphs of output
-- "1" -> "01"
if length of weeknumstr is 1 then
set weeknumstr to "0" & weeknumstr
end if
set weeknumdates to {}
repeat with outputline in outputlines
set {wnumstr, wnumdatestr, wdaynumstr} to my gettxtitems(tab, outputline)
-- we found a matching week number
if wnumstr is equal to weeknumstr then
-- the user wants all seven dates of the week
if weekdaynumstr is missing value then
set weeknumdates to weeknumdates & wnumdatestr
else
-- the user just wants one specific date of the week
if weekdaynumstr is equal to wdaynumstr then
set weeknumdates to weeknumdates & wnumdatestr
end if
end if
end if
end repeat
return {weeknumdates, missing value}
on error errmsg
return {false, errmsg}
end try
end getdatesforweeknum
-- I am returning the text items of a text separated by the given delimiter
on gettxtitems(delim, txt)
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {delim}
set txtitems to text items of txt
set AppleScript's text item delimiters to olddelims
return txtitems
end gettxtitems
-- I am indicating if a given item path exists
on itempathexists(itempath)
try
set itemalias to itempath as alias
return true
on error
return false
end try
end itempathexists
-- I am displaying error messages to the user
on dsperrmsg(errmsg, errnum)
tell me
activate
display dialog "Sorry, an error occurred:" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with title mytitle with icon stop
end tell
end dsperrmsg