Name Folder with Week Number and Dates?

This may be beyond KM, but does anyone know (and want to share with me) a macro for adding the week of the year number and the dates to a folder name? For example: My Folder Week 23 - Jun 01-07

I could also supply the week number via a user prompt. The big thing is that I want an automated way of taking that week number and turning it into a date range.

PS: Here's the week numbers and dates I'm talking about, but I need KM to work across years as dates change per week number. https://www.epochconverter.com/weeks/2020

I am looking at this and wondering, how would you expect it to handle things like week 1 ? Where it spans the years

Week 01	December 30, 2019	January 5, 2020

I always thought creating Dictionaries would be fitting, for this type of dataset, but I don't know how to make effective dictionaries and parse that data, I'll await the guru responses.

This would be my solution, based on a simple python3 script. Which as of now works based off a typed pattern of the following

dt.w(Week Number Optional)-(Year Optional)!

This defaults to the current year and week if only dt.w! is typed.

2020-06-01_09-15-38 (1)

If the is of interest, let me know and I will upload it.

1 Like

One approach to getting the week number:

Nth week in year from given date.kmmacros (19 KB)

JS Source
(() => {
    'use strict';

    const main = () => {
        const
            s = Application('Keyboard Maestro Engine')
            .getvariable(
                'folderDate'
            ),
            maybeDate = new Date(s);
        return isNaN(maybeDate) ? (
            'Not a valid date :: "' + s + '"'
        ) : weekNumberOfYearFromDate(maybeDate);
    };

    // weekNumberOfYearFromDate :: Date -> Int (1-52)
    const weekNumberOfYearFromDate = date => {
        const dteStartOfYear = new Date(new Date().getFullYear(), 0, 1);
        return Math.ceil((
            ((date - dteStartOfYear) / 86400000) +
            dteStartOfYear.getDay() + 1
        ) / 7);
    };

    return main();
})();

I think this can be done in KM, maybe with some scripting.
But I'm unclear what you want to use for input data.
Do you simply want to get the Date Range as text given an input of the Week Number?
You could then combine that text in any way you want to build a folder or file name.

OK, I think maybe this will do what you want.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example Output

image

image

Validation

You can validate my formulas using this site:

Technically the time should be 00:00, but since we are only dealing with days, I ignored the time. Correcting for Time Zone and DST is a royal PITA, but we don't need it here.

MACRO:   Calculate the Start/End Date for a Given U.S. Week Number [Example]

-~~~ VER: 1.0    2020-06-01 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Calculate the Start-End Date for a Given U.S. Week Number [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Calculate the Start and End Date for a Given U.S. Week Number

NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.

REQUIRES:

  1. KM 8.0.2+
  • But it can be written in KM 7.3.1+
  • It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
    .
  1. macOS 10.11.6 (El Capitan)
  • KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees. :wink:

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    • ALL Actions that are shown in the magenta color

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

4 Likes

I would have NEVER came up with this. @JMichaelTX is on top of the game.
To match the EpochConverter date list that @iampariah referenced , I had to modify your macro from -1 to -2. I don't know which one is technically correct, but it matches.

Good lesson to bookmark. Calculations is one of my many KM weaknesses.

Thanks JMTX

2 Likes

Thank you very much, @JMichaelTX. That was exactly what I needed. I turned it into a sub macro that I call at folder creation time, prompting the user for the week number. The sub is called again within a For Each Finder Selection to rename all the folders at the end of the year to prepare them for the next year.

Thank you so much for the incredible help. Can I buy you a beer?

PS: As @kcwhat noted, changing your -1 to -2 aligned the dates with the Epoch Converter; the -1 had them out of date.

1 Like

The ICUDateTime token supports ICU date week references, including the year of the week of the year

Symbol Meaning Example(s )
y year yy 96
y or yyyy 1996
Y year of "Week of Year" Y 1997
w week of year w 27
ww 27

So assuming you want Monday - Sunday weeks:

So My Folder Week 23 - Jun 01-07 would be:

image

My Folder Week %ICUDateTime%ww - MMM% %ICUDateTimeMinus%0%Mondays%dd%-%ICUDateTimePlus%0%Sundays%dd%

Note that across the end of the month you would get this:

My Folder Week 26 - Jun 29-05

So you may want to think about handling this.

Also note that the ICUDateTimePlus token only accepts the current date, so you would have to use a different solution for if you wanted to work with an arbitrary time, otherwise this will work for any case where you know the relative time you want (eg if you run it on Wednesday, and want it for next week, you could make that work).

3 Likes

I'm glad it worked for you. I also thought that a Sub-Macro would be the best ultimate solution, but figured that you could deal with that. :wink:

Thanks to you and @kcwhat for pointing that out.

My solution is for the US week numbering system.
As your Epoch ref points out:

All weeks are starting on Monday and ending on Sunday .
Please note that there are multiple systems for week numbering, this is the ISO week date standard (ISO-8601), other systems use weeks starting on Sunday (US)

2 Likes