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.
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.
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).
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?
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?
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:
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
Technomorph. Thanks for your comments–my shortcut definitely needs better error handling. I’ve demonstrated one approach in the following snippet. Another approach is to skip the file if it doesn’t return a match. Whichever approach is used, I agree that a fairly restrictive regex pattern (such as that used below) is a good idea.
Yeah RegEx’s can be trickey and need some full testing, but it’s fun learning them and in the end the can help prevent further issues later.
If it doesn’t match then you’ll have to go back and rename your files and reprocess again. If you’re dealing with hundreds of files that weren’t perfect that can suck.
Also you can use RegExs to capture portions of the file name and rename correctly.
Just FYI your RegEx is different from mine. Was that just for the testing?
Technomorph. I tested numerous regex patterns and approaches, and the one I posted was just one of many that worked.
Just as a general aside, there’s another shortcut approach that can be used to extract the color value, and the following is a simple example. The regex pattern assumes that the file naming is rock-solid reliable, which is probably not the case, but the approach used is an interesting alternative.
Perhaps moot at this point, but added for those anticipating difficulty with poorly crafted filenames. Rather than rely on unknown and possibly missing delimiters you could break the filenames using the already-known color values. This makes it much harder for whoever is out there maliciously crafting your filenames to break your process. You could also include typo’d color names if necessary.
Just the basics. No file-type filtering etc.
tell application "Finder"
set sourceFolder to alias "Macintosh HD:Users:username:Desktop:pngs"
set l to every file of sourceFolder as alias list
repeat with thisFile in l
set filePath to thisFile as text
set AppleScript's text item delimiters to {":"}
set fileName to text item -1 of filePath
set AppleScript's text item delimiters to {"Black", "White", "Yellow", "Red", "Blue", "Green", "Orange", "Purple", "Turquoise","Violet"}
copy text items of fileName to textItemsOfFileName
set AppleScript's text item delimiters to ""
if length of textItemsOfFileName > 1 then --This filename contains a color name
set prefixLength to the length of (characters of item 1 of textItemsOfFileName)
set suffixLength to the length of (characters of item 2 of textItemsOfFileName)
set foldercolorName to text (prefixLength + 1) thru -(suffixLength + 1) of fileName
end if
foldercolorName
try
set targetFolder to ("" & sourceFolder & foldercolorName & ":") as alias
on error
set targetFolder to make new folder at sourceFolder with properties {name:foldercolorName}
end try
move thisFile to targetFolder
end repeat
end tell
(*
Correctly subfolders every item of
1236846A-Red-MSG_043 copy.tif
1236846A-Red_MSG_043.tif
1236846A-Red-MSG_043 copy 2.tif
855846A-Black_MSG_085.tif
855846A-BlackMSG_085 copy.tif
525846A-Red_MSG_002.tif
525846A-Yellow_MSG_875.tif
75846BBlueMSG982 copy.tif
75846B-Blue_MSG_982.tif
75846B Blue-MSG_982 copy.tif
*)