Need some scripting help with custom folder naming problem

I’m doing this with on big sur and can not upgrade the os for other hardware n software reasons.

I am trying to make some automated folder creation scripts.
Everyday I have to make a folder on 2 separate drives with the same info and empty subfolders: DAY_001_day_month_year_name

***the next day it will have to make the day one increment higher so DAY_002_day_month_year_name
and so on.

I started doing this with automator and then realized there’s no wild cards and its gonna need some apple script or bash.


Part of the problem is that only one of the destinations is there everyday- the RAID drive
There are 6 other drives used for the duplicate version. Gofer_01 through 6 and they get used randomly.
and sent away for backup.

Objective: use automator plus some script or just a standalone script to create a workflow or app that will-

Find the RAID, identify the most recent folder DAY_004… and make a DAY_005…
then
-Look for one of 6 external hard drives and if it finds any of them

-make a copy of the one it just made in the RAID.

I am trying to use a combination of automator and bash or apple script I have scraped from youtube videos and other repositories.

-not sure the right combination of these since I want to run the script through streamdeck

Im kinda stuck now with some code and automator. see image.
tried some script but it kept giving me errors

trying to add an image here
All help appreciated.

You should post your code as it may provide some useful context for what you’re attempting to do.

Here is a simple script that gets a list of ‘day’ folders from the ‘raid’ drive and duplicates the one with the highest number to a disk whose name begins with ‘gofer’ .

tell application "Finder"
	set t to disk "raid"
	set d to (folders of t whose name begins with "day") as alias list
	--> {alias "raid:day001:", alias "raid:day002:", alias "raid:day003:"}
	set ld to last item of d
	set ldn to name of ld
	--> "day003"
	
	set dk to first disk whose name begins with "gofer"
	set curFol to folders of dk
	if not (exists folder ldn of dk) then
		duplicate folder ldn of t to dk
		--> folder "day003" of disk "gofer_1" of application "Finder"
	end if
end tell

There are probably a dozen or more other approaches you could take but this is a simple one. Obviously, it doesn’t confirm anything and will presumably error out if the necessary conditions are not in effect (eg disks aren’t mounted) but you can add those things in.

I wrote a version using “System Events”, as I hate using finder for file work as it is slow.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- https://www.macscripter.net/t/create-folder-structure-based-on-filenames-move-files-inside/70683/16

property myVol : "raid"
property bvol : "gopher"

on run
	local volumes, backupvol, dayFolders, dayFolder, d
	tell application "System Events"
		set volumes to name of every disk
		if myVol is not in volumes then return false
		set dayFolders to name of every folder of disk myVol whose name contains "Day"
		if (count dayFolders) > 0 then
			my bubblesort(dayFolders)
			set d to 0
			repeat with i from (count dayFolders) to 1 by -1
				set dayFolder to item i of dayFolders
				try
					set d to (text -3 thru -1 of dayFolder) as integer
					exit repeat
				end try
			end repeat
			set dayFolder to "Day_" & text -3 thru -1 of ("00" & (d + 1))
		else
			set dayFolder to "Day_001"
		end if
		if dayFolder is not in (name of folders of folder myVol) then
			make new folder at folder (myVol & ":") with properties {name:dayFolder}
		end if
	end tell
	set backupvol to ""
	repeat with avol in volumes
		if avol contains bvol then
			set backupvol to contents of avol
			exit repeat
		end if
	end repeat
	if backupvol = "" then return
	tell application "System Events"
		if dayFolder is not in (name of folders of folder backupvol) then
			make new folder at folder (backupvol & ":") with properties {name:dayFolder}
		end if
	end tell
end run

on bubblesort(alist as list)
	local i, j, sw
	script M
		property sList : alist
	end script
	repeat with j from (count M's sList) to 2 by -1
		repeat with i from 2 to j
			if (item i of M's sList) < (item (i - 1) of M's sList) then
				set sw to (item i of M's sList)
				set (item i of M's sList) to (item (i - 1) of M's sList)
				set (item (i - 1) of M's sList) to sw
			end if
		end repeat
	end repeat
end bubblesort

Thank you so much for getting me started …this helped alot
I cant seem to find a way to attach an image of my code.
My code doesn not appear the same way as your in my reply…i copy pasted it from script editor.

I think I understand the numbering and naming process you created…

I get a little lost with a few parts.

-Can i change the names of the properties so they mean something to me (so long as I change them everywhere?)
example:
property myBigRaidVol : “BIG7RAID”
property runnervol : “RWRUNER”

set volumes to
-names of every drive
should i enter al l the potential destintion drives other than the raid here?

-can you explain the avol vs bvol
I see that bvol is any volume (mounted i presume) with the name that starts with " "


Here’s how my code is shaping up

property myVol : “BIG7RAID”

property bvol : “RWRUNNER”

on run

local volumes, backupvol, dayFolders, dayFolder, d

tell application “System Events”

set volumes to

/Volumes/RWRUNNER_001_4TB_NVME

/Volumes/RWRUNNER_002_4TB_NVME

/Volumes/RWRUNNER_003_4TB_NVME

/Volumes/RWRUNNER_004_4TB_NVME

/Volumes/RWRUNNER_005_4TB_NVME

if myVol is not in volumes then return false

set dayFolders to name of every folder of disk myVol whose name contains “Day”

if (count dayFolders) > 0 then

my bubblesort(dayFolders)

set d to 0

repeat with i from (count dayFolders) to 1 by -1

set dayFolder to item i of dayFolders

try

set d to (text -3 thru -1 of dayFolder) as integer

exit repeat

end try

end repeat

set dayFolder to “Day_” & text -3 thru -1 of (“00” & (d + 1))

else

set dayFolder to “Day_001”

end if

if dayFolder is not in (name of folders of folder myVol) then

make new folder at folder (myVol & “:”) with properties {name:dayFolder}

end if

end tell

set backupvol to “”

repeat with avol in volumes

if avol contains bvol then

set backupvol to contents of avol

exit repeat

end if

end repeat

if backupvol = “” then return

tell application “System Events”

if dayFolder is not in (name of folders of folder backupvol) then

make new folder at folder (backupvol & “:”) with properties {name:dayFolder}

end if

end tell

end run

on bubblesort(alist as list)

local i, j, sw

script M

property sList : alist

end script

repeat with j from (count M’s sList) to 2 by -1

repeat with i from 2 to j

if (item i of M’s sList) < (item (i - 1) of M’s sList) then

set sw to (item i of M’s sList)

set (item i of M’s sList) to (item (i - 1) of M’s sList)

set (item (i - 1) of M’s sList) to sw

end if

end repeat

end repeat

end bubblesort

Thanks so much for your reply.

I have a little xcode experience but its pretty limited.

Q: does curFol mean current folder …that part confuses me a bit.

note //Only one of the 5 gofer drives will ever be mounted at any given time.

curFol is a variable so the name can be anything. Within the script, it will be a list of folders on dk, which is whichever ‘gofer’ drive is mounted. Thanks for clarifying… I had assumed, based on your initial explanation, that only one of the ‘gofer’ drives would be mounted at one time but wasn’t certain.

By the way, when you include code in your post, you should format it as code. The easiest way to do that is to put three back ticks on a line by themselves above your code and again below your code.

```
Your code here
```

thanks for the help.
see augmented script below
hope to make a few more changes and then mount the drives and test it out.

property myVol : "BIG7RAID"
property bvol : "RWRUNNER"

on run
	local volumes, backupvol, dayFolders, dayFolder, d
	tell application "System Events"
		set volumes to 
		
		

/Volumes/RWRUNNER_001_4TB_NVME
/Volumes/RWRUNNER_002_4TB_NVME
/Volumes/RWRUNNER_003_4TB_NVME
/Volumes/RWRUNNER_004_4TB_NVME
/Volumes/RWRUNNER_005_4TB_NVME

		
		
		
		if myVol is not in volumes then return false
		set dayFolders to name of every folder of disk myVol whose name contains "Day"
		if (count dayFolders) > 0 then
			my bubblesort(dayFolders)
			set d to 0
			repeat with i from (count dayFolders) to 1 by -1
				set dayFolder to item i of dayFolders
				try
					set d to (text -3 thru -1 of dayFolder) as integer
					exit repeat
				end try
			end repeat
			set dayFolder to "Day_" & text -3 thru -1 of ("00" & (d + 1))
		else
			set dayFolder to "Day_001"
		end if
		if dayFolder is not in (name of folders of folder myVol) then
			make new folder at folder (myVol & ":") with properties {name:dayFolder}
		end if




Create your volumes list like this:

set volumes to {"RWRUNNER_001_4TB_NVME", "RWRUNNER_002_4TB_NVME", "RWRUNNER_003_4TB_NVME", "RWRUNNER_004_4TB_NVME", "RWRUNNER_005_4TB_NVME"}

By the way, approximately how many ‘DAY_’ folders will there be on ‘BIG7RAID’?

I don’t know how any of your script can work when you are using POSIX paths for your volumes.
AppleScript only works with HFS paths. POSIX paths are only used when paths have to be sent to “Do shell script” commands.

1 Like

Well, here is my revised version.

tell application "System Events"
	-- set srcVol to disk "raid"
	set srcVol to disk "BIG7RAID"
	set initDirs to (name of folders of srcVol) -- all folders on source disk
	set srcDirs to {} -- DAY_ folders on source disk
	considering case -- exclude any non-'DAY_' folders
		repeat with f in initDirs
			if f begins with "DAY_" then set end of srcDirs to f
		end repeat
	end considering
	set lastDir to last item of srcDirs
	
	-- test lastDir
	set cDir to count of srcDirs
	set l3 to text -3 thru -1 of lastDir as integer
	if not (cDir = l3) then
		display dialog "Folder count mismatch" with icon stop
	end if
	
	-- get backup disk, test that only 1 is mounted
	-- set backDiskList to disks whose name begins with "gofer_"
	set backDiskList to disks whose name begins with "RWRUNNER_"
	if (count of backDiskList) is not 1 then display dialog "Too many backup disks" with icon stop
	-- set backDisk to first disk whose name begins with "gofer_"
	set backDisk to first disk whose name begins with "RWRUNNER_"
	
	-- make folder on backup disk
	set existingDirs to folders of backDisk
	if not (exists folder lastDir of backDisk) then
		make new folder at backDisk with properties {name:lastDir}
		display notification "Folder " & lastDir & " created on " & (name of backDisk)
	else
		tell me to display dialog "Folder " & lastDir & " already exists " & (name of backDisk) with icon caution
	end if
	
end tell

I included a couple of tests but when running various versions of the script, I found that the list of DAY_ folders was always sorted automatically by name. I don’t know if this should be an undefined behaviour or not but it has been perfectly consistent for me. Among these, I added a few alerts in case something untoward happened.

In terms of time to run, it’s probably not an issue but it took anywhere from 100-200 milliseconds on my decrepit machine. I have a Finder variant as well and it typically takes around 1500 milliseconds to run. Initially I was only using a handful of DAY folders and the same Finder variant takes about 250 milliseconds when there are less than 20.

Unless I misunderstand your point, AppleScript works with POSIX paths for quite a few years by now.

There are cases where only HFS paths can be used (usually when scripting certain 3rd party apps) but it’s not a rule.

By work with you mean it could convert HFS to POSIX and back again, but whenever dealing with files or aliases inside Applescript, it has always been HFS only. POSIX paths are only used when sending to the shell. As far as I know.

You can use POSIX paths directly in AppleScript, for example:

tell application "System Events"
exists folder "~/Library"
end tell

From the ASLG, p49

set notesFile to POSIX file "/Users/myUser/Feb_Meeting_Notes.rtf"
tell application "TextEdit" to open notesFile

To flesh it out…

set orig to (path to home folder) & "today1.txt" as text -- hfs path
set pp to POSIX path of orig -- posix path
set pf to POSIX file pp -- file object

tell application "TextEdit" to open pf
--> document "today1.txt" of application "TextEdit"

That said, you can’t (apparently) combine the last two lines into a single one.

I did not know that. You learn something new every day.

oh yeah. also it wasn’t always like this. AppleScript didn’t accept POSIX paths for several years after introduction of OS X. i myself discovered that AppleScript started accepting POSIX paths by a chance. i don’t remember when it started but probably about 15 years ago.

tell application "TextEdit" to open my POSIX file pp
1 Like

I don’t know the extent to which you have a say in how your folders are named, but this is not a good naming convention. If I were to try and deduce the line of reasoning that led to this particular format, I’ll assume that the goal was to be able to have folders that are named according to and indicative of the date to which the files in a given folder pertain (e.g. the date the files were created or processed, or the date of an event that is documented within the files, etc.); but also to have it such that when the folders are sorted by name lexicographically, they will also be in the chronological date order.

A naming convention like "dd-mm-yyyy_DescriptionOfContents"—even with its use of two-digit day and month numerals—won’t preserve date chronology when ordering folders lexicographically. For instance, were you to archive documents relating to the following historical events in folders named by this convention, their chronological ordering would be as follows:

  1. "15-04-1912_Titanic"
  2. "28-06-1919_TreatyOfVersailles"
  3. "01-01-2000_OMGY2KLOL"

but Finder or Terminal would order them alphabetically like so:

  1. "01-01-2000_OMGY2KLOL"
  2. "15-04-1912_Titanic"
  3. "28-06-1919_TreatyOfVersailles"

which is actually as far away from the desired order as it’s possible to get in this instance.

Thus, it would seem that the solution that seemed most obvious was to take this naming convention and prefix it with a three-digit number between Day-001 and Day-366 to force folders to sort according to the day of the year, which, all being well, means that the folder pertaining to the date 1 January 2024 will be prefixed with "Day-001" (i.e. "Day-001#01-01-2024NewYearsDay"), and each subsequent folder gets prefixed incrementally with each passing day until the folder that will pertain to New Year’s Eve this year will be named like "Day-366#31-12-2024NewYearsEve".

Assuming this is more-or-less what the current plan going forward looks like (apart from the choice of punctuation I’ve used to illustrate the nomenclature above), the same problem will arise again when we enter into 2025. It could be that the numerical prefixes will continue incrementing, e.g. "Day-367#01-01-2025NewYearsDay", which doesn’t lend itself to identifying any specific dated folder by its associated prefix (hence, its position in the list isn’t something that can be quickly determined). It’s also not eminently readable as a naming format, no matter the choice of punctuation used.


A Better Naming Convention

yyyy-mm-dd_DescriptionOfContents

or

yyyymmddDescriptionOfContents

The ISO-8601 specification pertains to the formatting of dates and times, defining an internationally-recognised standard that aims towards a universal set of conforming nomenclatures that confer utility, flexibility, and practicality. You can read about it at your leisure, but the basic three-part date format is "yyyy-mm-dd", e.g. 2024-01-15, which you get by taking the components of your current (UK-formatted) date, namely the day, followed by the month, followed by the year, and reversing their order. The year must be expressed as a four-digit number, and the month and day are each expressed as two-digit numbers with a leading zero where necessary (01-09).

When written in this manner, all dates that are ordered chronologically will automatically be in lexicographic (dictionary/alphabetic) order as well, and vice versa. Thus, the date forms the first part of the folder name, after which the descriptive title can follow. Whether or not to include punctuation is up to you, however, you’ll save yourself future grief by adhering to the following:

  1. Punctuation should be used sparingly, at most to delimit date components, and optionally to separate the date from the descriptive title. Use a single hyphen ("-") to delimit date components (do not use ":" or "/", even if permitted by the filesystem, as these are both used to delimit directories in various path notations). If punctuating after the date and before the description, use a different character to your date delimiter, e.g. "_" or "#"—omitting punctuation here is also a valid choice.

  2. Avoid using underscores: these are lexicographically positioned after uppercase letters but before lowercase letters, which can result in less helpful sorting when applied to descriptive titles (i.e. folders or files all pertaining to the same date, which are then assigned their order according to the part after the date). A single underscore is fine to separate the date from the description, as this will become a key fixture of your naming convention, meaning the 11th character of your folder name will be its one and only underscore.

  3. The descriptive title portion of the name is best formatted using “Title Case” (the first letter of each word is capitalised, along with all letters that form acronyms, whilst remaining letters are lowercased, i.e. as chapter titles in books often appear), with all whitespace and punctuation removed. For example, this post’s title would be formatted as:

    NeedSomeScriptingHelpWithCustomFolderNamingProblem
    
  4. Consider omitting all punctuation, including within the date. While this would no longer adhere to the ISO-8601 specification, which prescribes delimiting date-time components, for a file naming convention, writing the date simply as "yyyymmdd" (e.g. 20240115) confers a few advantages, including shorter file names, improved readability (it doesn’t seem like this will be the case, but when presented with lists of files and folders, the hyphenated date can become jarring to skim through quickly), and the natural formation of a purely numerical prefix followed by a purely alphabetic suffix, e.g.

    20240112NeedSomeScriptingHelpWithCustomFolderNamingProblem
    

Of course, there’s always the possibility I’ve interpreted your intentions and your needs completely wrongly, and none of this is applicable to your current endeavour. But, keep it all in mind for the future, as it could save you a lot of headaches.