How to automatically create the sub folders using part of file names?

Hi, I’m a complete noob when it comes to Apple script/quick actions, and I’m trying to see if I can get help from here.

I have hundreds of files that have a particular name format.

Sku number-color_initial_sequence number.tif

Example:
525846A-Red_MSG_002.tif
855846A-Black_MSG_085.tif
525846A-Yellow_MSG_875.tif
1236846A-Red_MSG_043.tif
75846B-Blue_MSG_982.tif

There are many of the same colors in different SKU numbers, and I’m looking for Applescript/Quick action to make subfolders only using the color name. And place the corresponding color images automatically.
Notice that after the first SKU number, a hyphen is used, and then the following items are separated by the underscore.

Here is one way to do this.

set asrc to choose folder
tell application "System Events"
	set imageList to path of files of asrc whose visible is true and ¬
		name extension is "tif"
	-- set leftStrings to {} -- left side of filename, ie SKU and colour	
	set src to path of asrc
	set colours to {} -- list of colours
	
	-- cycle through each file
	repeat with eachFile in imageList
		set nameFile to name of file eachFile
		if nameFile contains "-" and nameFile contains "_" then
			set AppleScript's text item delimiters to "_"
			set prescore to text item 1 of nameFile -- get everything before first underscore
			-- set end of leftStrings to contents of prescore
			
			set AppleScript's text item delimiters to "-"
			set colour to last text item of prescore -- get after hyphen, ie colour
			if colours does not contain colour then -- if no colour folder yet exists
				set end of colours to colour
				set fcol to make new folder at folder src with properties {name:colour}
			end if
			-- move file to matching colour folder
			move file eachFile to folder colour of folder src
		end if
	end repeat
end tell

What it does is:

  • choose the folder that contains the files to organise
  • make a list of files with a “tif” extension and cycle through them
    • split the file name, first on “_” and then on “-”
      eg 525846A-Red_MSG_002.tif becomes 525846A-Red becomes Red
    • if a matching colour folder does not yet exist, create it
    • move each file to its matching colour folder

If a file does not contain both a dash and an underscore, it will be skipped. If desired, some action could be taken.

If a specific folder is used routinely, that could be set instead of using choose folder.

1 Like

FWIW, the following is a shortcut suggestion. It prompts for the source folder but can be changed to work as a service on a folder selected in a Finder window. The desired file extension needs to be set in the Filter action (it’s currently tif).

File to Folder.shortcut (23.2 KB)

Whoa! Thank you so much for the quick solution!!
However, I have no idea how to use this.
Someone made me the similar script in the past(different usage) and it’s extension is ‘workflow’
And once I install it with Automator, I can right click on the desired folder and open the context menu and choose ‘quick actions’ then I can able to pick the script.
How do I make sure script work the same way?

wow, I didn’t know there was a shortcut function!
I will give this a try and report back.
Thank you!

I confirm that this script works great!
Is it possible to make this work with any extensions? Not just tif.
Mainly, I will need one for ‘jpg’, but maybe not limiting the extension will be a better idea just in case?

Gunsmoke. You can add file extensions to the Filter action as follows:

This shortcut does not filter on file extensions and works on every file in the selected folder. Files that do not contain the desired string are skipped:

File to Folder.shortcut (23.0 KB)

2 Likes

Thank you, thank you, thank you!!!
You rock!

I think that Peavine has put together a good solution but to address your general question and for reference:

The simplest way is to create an applescript (extension: scpt) which is most easily done with code here by clicking the ‘Open in Script Editor’ button below the code. Once the script is open, clicking the ‘Run’ button will execute it. If you save it as a script, then it will automatically take on the ‘scpt’ extension. Then, you can either just open it when you wish to use it, or you can put the document in the ‘scripts’ folder, and then run it from the Script menu (on the right side of the menu bar near the clock). If you don’t see such a menu item, then it is typically enabled in Script Editor’s Preferences > General.

Alternatively, you could save the script as an ‘application’ — there is a drop down menu in the dialogue when you first save a script; you can also use File > Export. This creates an ‘application’ that you can double-click to run. Note that in order to comply with Apple’s security measures, some configuration is required with the details dependent upon the OS version.

You could also use Automator to create a workflow (or application) and then embed the code within using the ‘Run Applescript’ action. The code might require tweaking to be more general. Automator also has the abilitiy to create a ‘service’ and then you could use the right-click method to run it.

Very often, there are multiple approaches to take when automating such activities. There are numerous posts on this site in case you want to learn more about them, and of course, you can always post questions.

Try this, slightly different, illustrating more techniques

set asrc to choose folder
tell application "System Events"
   set imageList to path of files of asrc whose visible is true
   set src to path of asrc
   repeat with thisFile in imageList
      set nameFile to name of file thisFile
      set AppleScript's text item delimiters to {"_", "-", "."}
      set fileNameItems to text items of nameFile
      if the (count of fileNameItems) = 5 then
         set {prescore, colorLabel, code, numberCode, nameExtension} to fileNameItems
         if nameExtension is in {"tif", "tiff", "jpg", "jpeg"} then
            set folderPath to src & colorLabel
            if not (exists folder folderPath) then
               set folderPath to make new folder at folder src with properties {name:colorLabel}
            end if
            move file thisFile to folderPath
          --  move file thisFile to folder colorLabel of folder src
         end if
      end if
   end repeat
end tell