0

I'm a graphic designer and need an organized folder scaffold for all of my projects. I have a work flow now that:

  • asks for the project name
  • asks where the project folder needs to be stored
  • then creates the sub folders that I will be using.

Such as a folder named "Project 1" then inside are the sub folders named "images", "Photoshop", "drafts", "Illustrator" and so on.

Scaffoldenter image description here

I am trying to create a folder action that sorts files that are added to the project root folder into the an appropriate sub folder, either by file extension or by part fo the file names.

For example:

  • .jpg files shoudl be moved to the image folder
  • .psd files should be moved to the Photoshop folder
  • file names that contain the word "draft" should be moved to the drafts folder.

What I've tried so far is to make a folder action that does this but it sends those files to the folder originally writen in the folder action. If I make a folder action that sends .psd files to the Photoshop folder into "Project 1", then that action to the next folder "Project 2" will still send those files to the Photoshop folder in "Project 1", instead of in "Project 2".

Any suggestions on how to do this?

4
  • I don't have an answer on how to write the folder action such that it dynamically chooses the right root folder (Project 1, Project 2, etc...). However, I remembered Post Haste, which might be handy for you as well. Also this other question might be helpful. Jun 30, 2021 at 15:44
  • For an Automator folder action, you need to specify the watched folder when saving the workflow. An AppleScript folder action is a little more flexible, in that you can use the same script and specify the folder via the Folder Actions Setup service. Either way, you will need to specify a folder action for each watched folder.
    – red_menace
    Jun 30, 2021 at 16:16
  • JSYK... The Run Shell Script action shown in the screenshot exhibits a needless over use of the mkdir command and actually only needs to be executed twice, e.g. mkdir "$1/$2"; cd "$1/$2" || exit; mkdir 'After Effects' 'Assets' 'Audition' 'Drafts' 'Excel' 'Final' 'Fonts' 'Illustrator' 'Images' 'Photoshop' 'Premier' 'Word' -- Note that the single-quotes used in the second mkdir command are only necessary for 'After Effects', however, my coding style is typically to quote all strings for safety and/or visual clarity as appropriate. Jun 30, 2021 at 16:27
  • Also, the screenshot in your OP excludes important information that's normally visible, e.g., what type of Automator workflow it is, whether or not you've made the logical disconnect between certain actions, etc. Please adjust the size of the Automator workflow to include that without being overly large. That said, is the screenshot actually your Folder Action or just the workflow to create the hierarchical folder structure? If you had also, included the exact specifications of what goes where, I would have posted a working solution as an answer. Jun 30, 2021 at 16:28

3 Answers 3

1

Here is a way to go about achieving the goal. (It is not the only way to achieve the goal.)

First I'd make a placeholder template Folder Action using Automator and after creating it, disable it in Folder Action Setup, but leave the workflow available where it gets saved. This will be opened at the end of creating a new project folder to then be assigned to it and saved as its name, thus leaving the placeholder template Folder Action available for the next new project folder.

The images of the two workflows are placed at the bottom so as to make the answer easier to read.

You will need to add to the example shell script code, as all of the conditions were not expressed in your question.



Placeholder template Folder Action

Example shell script code:

#!/bin/bash

    # f = fully qualified pathname
    # d = directory pathname
    # fn = filename with extension
    # n = filename without extension
    # e = filename extension

shopt -s nocasematch

for f in "$@"; do

    d="${f%/*}"
    fn="${f##*/}"
    # n="${fn%.*}"
    e="${fn##*.}"

    cd "${d}" || exit
    [ -w "${d}" ] || exit

    [[ ${e} == jpg ]] && mv -n "${f}" './Images/'
    [[ ${e} == psd ]] && mv -n "${f}" './Photoshop/'
    [[ ${fn} =~ draft ]] && mv -n "${f}" './Drafts/'

    [ -f "${f}" ] && mkdir -p 'Other'; mv -n "${f}" './Other/'

 done

Note: The last test before done handles any case not already defined. Obviously you can choose not to use it and remove it from the example shell script code.



Create New Project Folder Structure

For an Automator workflow to create the new project folder, use the screenshot of the following workflow:

The example shell script code of the Run Shell Script action is:

mkdir "$1/$2" || exit
cd "$1/$2" || exit
mkdir 'After Effects' 'Assets' 'Audition' 'Drafts' 'Excel' 'Final' 'Fonts' 'Illustrator' 'Images' 'Photoshop' 'Premier' 'Word'

open "${HOME}/Library/Workflows/Applications/Folder Actions/Foobar.workflow"

Note: Change the name of the Folder Action workflow in the example shell script code from Foobar.workflow to whatever its actual name is.

Also note in the image below of the Create New Project Folder Structure workflow how there are logical breaks between some of the actions and in the end the value of the two variables are separate from the rest of the workflow. While the workflow shown in the question works, nonetheless, in my opinion this is a better way of doing it as it breaks the workflow into logical action blocks, which makes it easier to follow/understand what the workflow is doing.

Technically the Create New Project Folder Structure workflow only needs one variable and it could be structured as, e.g:

  • Ask For Text action
  • set Value of Variable action
    • Variable: [New Project]
  • Ask for Finder Items action
    • [Optons] [√] Ignore this action's input
  • Get Value of Variable action
    • Variable: [New Project]
  • Run Shell Script action

While this does work and has less actions than what's show in the question, nonetheless, as I mentioned earlier about logical action blocks within the workflow which makes it easier to follow/understand what the workflow is doing.



Folder Action Setup

Folder Action Setup can be used to work with as needed any Folder Action created.

Folder Action Setup can be opened using Spotlight by pressing ⌘ Space and start typing its name, then once selected, press Enter.



enter image description here


enter image description here

0

Here is a different approach which does not use Automator.

This following AppleScript code, in short, prompts you to choose a name for your new Art Project and where to create the Project and its directories.

After the Project’s directories structure has been created, a new Folder Action will then be automatically created, saved, and attached to your new Project folder.

The only thing that really needs to be done is to edit some of the values in the script object script artProjectsFileProcessor. This is the part of the code which tells the Folder Action what to do with the files that get added to the Project folder. I added a few conditional statements to get you started on the right track.


global createProject, projectName, newProjectFolderLocation, artProjectsFileProcessor

property pathToFolderActions : ((path to scripts folder as text) & "Folder Action Scripts:")
property artProjectsFileProcessorFolderAction : "Art Projects Files Processor.scpt"
property subFolderNames : "After Effects
    Assets
    Audition
    Drafts
    Excel
    Final
    Fonts
    Illustrator
    Images
    Photoshop
    Premiere
    Word"

tell application "System Events" to set folderActionExists to exists of ¬
    alias (pathToFolderActions & artProjectsFileProcessorFolderAction)

--if not folderActionExists then
store script artProjectsFileProcessor in ¬
    pathToFolderActions & artProjectsFileProcessorFolderAction replacing yes
--end if

activate
set newProjectFolderLocation to POSIX path of ¬
    (choose folder with prompt "Choose A Location To Create Yor New Project Directory")

tell application "System Events"
    set existingFolders to name of folders of folder newProjectFolderLocation
end tell

set projectName to createProject()

if projectName is in existingFolders then
    repeat while projectName is in existingFolders
        display dialog "A project with that name already exists." & linefeed & ¬
            "Please choose a different name for your new project" buttons ¬
            {"Cancel", "OK"} default button "OK"
        set projectName to createProject()
        delay 0.1
    end repeat
end if

do shell script "echo " & quoted form of subFolderNames & " | xargs -I {} mkdir -p " & ¬
    quoted form of POSIX path of newProjectFolderLocation & quoted form of projectName & "/{}"

attachFolderActionToFolder(projectName, newProjectFolderLocation, artProjectsFileProcessorFolderAction)

tell application "System Events"
    if not folder actions enabled then set folder actions enabled to true
end tell

set alertChoice to button returned of (display alert ¬
    "Art Project Created" message "Your new art project " & projectName & ¬
    " has been created in the directory... " & ¬
    newProjectFolderLocation & linefeed & linefeed & ¬
    "Folder Actions for this project are enabled and files are ready to be processed." buttons ¬
    {"Show In Finder", "OK"} default button 2 giving up after 20)

if alertChoice = "Show In Finder" then
    do shell script "open -R " & quoted form of POSIX path of ¬
        newProjectFolderLocation & quoted form of projectName
end if

on createProject()
    activate
    set projectName to text returned of ¬
        (display dialog "Entering a new project name will create a project folder with all necessary sub-folders" & ¬
            linefeed & linefeed & "After your project directories structure has been created,
 a new folder action will be created and automatically attached to your new project folder.
 It will then be ready to process any files added to it" default answer ¬
            "My New Project" buttons {"Cancel", "Create My Project"} ¬
            default button 2 cancel button 1 with title "New Project Creation")
    return projectName
end createProject

to attachFolderActionToFolder(projectName, newProjectFolderLocation, artProjectsFileProcessorFolderAction)
    tell application "Folder Actions Setup"
        launch
        delay 1
        try
            set addedFolderAction to make new folder action with properties ¬
                {name:projectName, path:(newProjectFolderLocation & projectName)}
        end try
        try
            delay 0.5
            tell addedFolderAction to make new script with properties ¬
                {name:artProjectsFileProcessorFolderAction, path:(pathToFolderActions & artProjectsFileProcessorFolderAction)}
        end try
    end tell
    delay 1
    tell application "Folder Actions Setup" to quit
end attachFolderActionToFolder

script artProjectsFileProcessor
    property afterEffectsFiles : {}
    property assetsFiles : {}
    property auditionFiles : {}
    property draftsFiles : "Draft"
    property excelFiles : {"xls"}
    property finalFiles : {}
    property fontsFiles : {"ttf"}
    property illustratorFiles : {"ai", "eps"}
    property imagesFiles : {"png", "jpg", "bmp"}
    property photoshopFiles : {"psd"}
    property premierFiles : {}
    property wordFiles : {"doc", "txt"}
    
    on adding folder items to theFolder after receiving theNewItems
        repeat with thisItem in theNewItems
            tell application "System Events"
                set nameExtension to name extension of thisItem
                set theName to name of thisItem
                if theName contains draftsFiles then
                    move thisItem to folder "Drafts" of theFolder
                else if nameExtension is in afterEffectsFiles then
                    move thisItem to folder "After Effects" of theFolder
                else if nameExtension is in assetsFiles then
                    move thisItem to folder "Assets" of theFolder
                else if nameExtension is in auditionFiles then
                    move thisItem to folder "Audition" of theFolder
                else if nameExtension is in excelFiles then
                    move thisItem to folder "Excel" of theFolder
                else if nameExtension is in finalFiles then
                    move thisItem to folder "Final" of theFolder
                else if nameExtension is in fontsFiles then
                    move thisItem to folder "Fonts" of theFolder
                else if nameExtension is in illustratorFiles then
                    move thisItem to folder "Illustrator" of theFolder
                else if nameExtension is in imagesFiles then
                    move thisItem to folder "Images" of theFolder
                else if nameExtension is in photoshopFiles then
                    move thisItem to folder "Photoshop" of theFolder
                else if nameExtension is in premierFiles then
                    move thisItem to folder "Premier" of theFolder
                else if nameExtension is in wordFiles then
                    move thisItem to folder "Word" of theFolder
                end if
            end tell
        end repeat
    end adding folder items to
end script

I saved this code as ”Art Project Creator.scpt” to my /Users/MYSHORTNAME/Library/Scripts/Applications/Finder … folder. Doing this allows me to run .scpt files directly from the Script Menu in the menu bar, when Finder is the active app.

This code could also be saved as an Application file and will run every time it is double clicked in Finder. Another option could be to create a new Automator Quick Action, and insert this code into a “Run AppleScript” command to the Workflow, allowing the code to run via the Quick Actions Contextual Menu in Finder.

enter image description here


This first animation demonstrates the creation of a new Art Project with its sub-directories.(Remember a new Folder Action will be automatically created, saved, and attached to your new Project folder.)

enter image description here


This second animation demonstrates adding files to the new Project Folder and the results of files’ new locations, processed from the code in its newly attached Folder Action

enter image description here

4
  • RE: "Because of the time it is now, I will fine-tune the code and add some descriptive comments tomorrow." -- This is a good start and I'm interested to see what else you add. That said, I'd like to make a couple of suggestions. 1. With property pathToFolderActions the path to workflows folder is normally used for Automator created Folder Action. The path for AppleScript .scpt based Folder Actions is ~/Library/Scripts/Folder Action Scripts, for which you can use property pathToFolderActions : ((path to scripts folder as text) & "Folder Action Scripts:") instead. Aug 3, 2021 at 14:41
  • 2. The tell application "Finder" to set folderActionExists to exists … should really use System Events, as it incurs less overhead and Finder should be avoided in favor of System Events wherever applicable. 3. This one is the most glaring issue, sorry… The use of Capital Case messages are much harder to read, goes agains the industry standard, and certainly not done by first rate developers. That's not to say never use Capital Case as in some circumstance, albeit very few, it may lend itself appropriate, e.g., as the title to a message. Aug 3, 2021 at 14:42
  • Also, please, do not use Capital Case in the code comments for the same reasons. -- As I started writing this before your last update I'm leaving the now deleted quoted portion of my first comment. Aug 3, 2021 at 14:42
  • Since nobody else wants to comment or acknowledge my brilliant usage of the very seldom used store script command for the process of creating and activating a Folder Action, I have no choice but to give myself an honorary +1
    – wch1zpink
    Aug 8, 2021 at 13:03
-1

My approach:

Since you already have a lot of files, I assume they are inside a root folder. A folder quick action can:

  1. Rename it with the requested text.
  2. Store the path in a variable so you can search and move files inside it.
  3. Duplicate a template from somewhere else in your HD with all the possible folders pre-arranjed and copy them to the path.
  4. After the folder structure for your given project is ready, search for filetype X. Search for folder X. Move filetype X to folder X. Repeat for filetype Y. Etc. If you search inside the path, you wont mess with other projects.
  5. At the end, look for empty folders and delete them, so you will only get the folders you need.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .