C-Command Software Forum

Help with script for nested folders

Users: I am in search of help with a line of script that would create a series of nested folders within one Library.
I need:
- Year (beginning 1850) [YYYY] through 2021
- Months YYYY-01 through YYYY-12 (for each year - nested)
- Days for each month YYYY-MM-DD for each month (nested)
I also would need instruction about how to insert said script in the Terminal Command line (or other location) on Mac using Mojave through Big Sur.

It is my hope that this would be a very simple task for a user more advanced than I and express my sincere appreciation in advance.


Here’s a script that shows how to do this:

use framework "Foundation"

set _calendar to my NSCalendar's currentCalendar

set _startComponents to my NSDateComponents's alloc's init
_startComponents's setYear:1850
_startComponents's setMonth:1
_startComponents's setDay:1
set _startDate to _calendar's dateFromComponents:_startComponents

set _endComponents to my NSDateComponents's alloc's init
_endComponents's setYear:2021
_endComponents's setMonth:12
_endComponents's setDay:31
set _endDate to _calendar's dateFromComponents:_endComponents

set _dayComponents to my NSDateComponents's alloc's init
_dayComponents's setDay:1

set _yearFormatter to my makeFormatter("yyyy")
set _monthFormatter to my makeFormatter("yyyy-MM")
set _dayFormatter to my makeFormatter("yyyy-MM-dd")

set _date to _startDate
repeat until (_date's compare:_endDate) is my NSOrderedDescending
    tell application "EagleFiler"
        tell library document 1
            set _yearFolder to my ensureFolder(root folder, my format(_yearFormatter, _date))
            set _monthFolder to my ensureFolder(_yearFolder, my format(_monthFormatter, _date))
            set _dayFolder to my ensureFolder(_monthFolder, my format(_dayFormatter, _date))
        end tell
    end tell
    set _date to _calendar's dateByAddingComponents:_dayComponents toDate:_date options:0
end repeat

on makeFormatter(_format)
    set _locale to my (NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
    set _formatter to my NSDateFormatter's alloc's init
    set _formatter's dateFormat to _format
    return _formatter
end makeFormatter

on format(_formatter, _date)
    set _nsString to _formatter's stringFromDate:_date
    return _nsString as Unicode text -- Only works on a separate line
end format

on ensureFolder(_parentRecord, _name)
    tell application "EagleFiler"
            return first library record of _parentRecord whose name is _name
        on error
            tell _parentRecord's library document
                return add folder name _name container _parentRecord
            end tell
        end try
    end tell
end ensureFolder

Do you need to run it from Terminal? Or can you just copy/paste it into the Script Editor app and run it once?

I just found this, Michael. Thank you very much. I don’t know whether to do it from Script Editor or Terminal (makes no difference to me) but I will ask a friend to help. You are very kind!

It would probably be easiest to copy and paste it into a new window in Script Editor and then click Run.

I got it to run and it’s creating the folders now for me. Thanks a million, Michael. Very much appreciate your help.


Michael, I completed the task but with lots of hangs and restarts. I suspect it’s because a new library is being created for each day. Tried turning off indexing and meta-data indexing per manual instructions but still v-e-r-y slow. Can you please modify (or tell me how to modify) the script so that I end up with only years and months. I will then just insert entries with a date stamp or identifier into the correct month. Sorry for any trouble. Thank you. Steve.

It would be helpful if you could record some samples during the hang.

It’s just creating a new folder, not a new library.

It may simply be that there is overhead in having and displaying so many folders in the source list and updating that each time a folder is added. You could try collapsing the Library portion of the source list until the script is done.

You could delete the line that begins with set _dayFolder.