skip to Main Content

I need to create an application or folder action where users can drop files. The folder will then:

  1. Check the first 13 digits of each filename and create a new directory using those 13 digits
  2. Move all files with the same first 13 digits in to the relevant folder
  3. Move the whole folder to a preset directory on the server

JPG names will be:

  • 00319-BB01-01-C1 HighResDig.jpg 00319-BB01-01-C1 HighResPrint.jpg
  • 00319-BB01-01-C1 LowResDig.jpg 00319-BB01-01-C1 AI.jpg
  • 00319-BB01-01-C1 Catalogue.jpg 00319-BB01-01-C1 Web.jpg
  • 00319-BB01-01-S1 HighResDig.jpg 00319-BB01-01-S1 HighResPrint.jpg
  • 00319-BB01-01-S1 LowResDig.jpg 00319-BB01-01-S1 AI.jpg
  • 00319-BB01-01-S1 Catalogue.jpg 00319-BB01-01-S1 Web.jpg
  • 00319-BB01-01-S2 HighResDig.jpg 00319-BB01-01-S2 HighResPrint.jpg
  • 00319-BB01-01-S2 LowResDig.jpg 00319-BB01-01-S2 AI.jpg
  • 00319-BB01-01-S2 Catalogue.jpg 00319-BB01-01-S2 Web.jpg
  • 00320-BB01-01-C1 HighResDig.jpg 00320-BB01-01-C1 HighResPrint.jpg
  • 00320-BB01-01-C1 LowResDig.jpg 00320-BB01-01-C1 AI.jpg
  • 00320-BB01-01-C1 Catalogue.jpg 00320-BB01-01-C1 Web.jpg
  • 00320-BB01-01-S1 HighResDig.jpg 00320-BB01-01-S1 HighResPrint.jpg
  • 00320-BB01-01-S1 LowResDig.jpg 00320-BB01-01-S1 AI.jpg
  • 00320-BB01-01-S1 Catalogue.jpg 00320-BB01-01-S1 Web.jpg
  • 00320-BB01-01-S2 HighResDig.jpg 00320-BB01-01-S2 HighResPrint.jpg
  • 00320-BB01-01-S2 LowResDig.jpg 00320-BB01-01-S2 AI.jpg
  • 00320-BB01-01-S2 Catalogue.jpg 00320-BB01-01-S2 Web.jpg

At the moment, the JPGs are being created automatically using a Photoshop Droplet. The droplet then creates a directory on the users desktop with all versions of the file. This folder is called JPGs.
It would be useful if I could create a folder action for the Desktop>JPGs folder to then automatically run the script to create the new directory and move the files. Creating the files can take between 2 seconds and 1 minute depending on how many are being created at once.

I have some partially working code to create the new folders, but I cannot get it to automatically run based using a folder action in Automator

on run {input, parameters}

    set chosenFolder to (choose folder)
    tell application "Finder" to set fileList to files of (chosenFolder)

    repeat with aFile in fileList
        set {name:Nm, name extension:Ex} to info for (aFile as alias)
        if Ex is missing value then set Ex to ""
        if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
        set dateFolder to text 1 thru 13 of Nm
        set sourceFile to quoted form of POSIX path of (aFile as text)
        set destinationFile to quoted form of (POSIX path of chosenFolder & dateFolder & "/" & name of aFile)
        do shell script "ditto " & sourceFile & space & destinationFile
        do shell script "rm " & sourceFile
    end repeat
end run

This creates a new folder and moves the correct files. I need this to run automatically when files are dropped in to a specific folder. I then need to move the newly created folders to another directory.

2

Answers


  1. You can move the folder with

    do shell script "mv " & quoted form of (POSIX path of chosenFolder & dateFolder & "/") & space & "/path/to/destinationFolder
    

    And what is going to happen if two files are distributed in the same dateFolder? The syntax above will overwrite an existing folder.

    For a folder action you actually don’t need an automator workflow.

    Login or Signup to reply.
  2. You can do this with folder actions, without using Automator. Open the Script Editor app and copy in the following code:

    on adding folder items to this_folder after receiving these_items
        tell application "System Events"
            set chosenFolder to POSIX path of this_folder
            repeat with aFile in these_items
                set {Nm, Ex} to {name, name extension} of aFile
                try
                    if Ex is not in {missing value, ""} then
                        set dateFolder to text 1 thru 13 of Nm
                        set destinationFolder to my checkForFolder(chosenFolder, dateFolder)
                        move aFile to destinationFolder
                    end if
                on error
                    set malformedFileNameFolder to my checkForFolder(chosenFolder, "Malformed File Names")
                    move aFile to malformedFileNameFolder
                end try
            end repeat
        end tell
    end adding folder items to --ing folder items to
    
    on checkForFolder(baseFolderPath, folderName)
        tell application "System Events"
            try
                if not (exists folder folderName of folder baseFolderPath) then
                    make new folder at folder baseFolderPath with properties {name:folderName}
                end if
                return folder folderName of folder baseFolderPath
            on error errstr
                display dialog errstr
            end try
        end tell
    end checkForFolder
    

    Save it with whatever name you like in ‘~/Library/Scripts/Folder Action Scripts‘. Open the Folder Actions Setup app, click the ‘+’ button on the left to add your JPGs folder and the ‘+’ button on the right to attach the script to the folder. Then you should be done.

    P.s. If you need to set up the folder action programmatically — for instance, if you’re setting these up manually and not just cloning a user — you can use a script like this:

    tell application "System Events"
        set folderPath to "/path/to/target/folder"
        set scptPath to "/Users/whomever/Library/Scripts/Folder Action Scripts/script name"
    
        try
            set fa to first folder action whose path is folderPath
        on error errstr
            set fa to make new folder action with properties {path:folderPath, enabled:false}
        end try
    
        set {fileType, fileExt, fileName} to {file type, name extension, name} of file scptPath
        if fileType is "osas" or fileExt is "scpt" then
            tell fa
                try
                    make new script at end of scripts with properties {name:fileName}
                on error errstr
                    display dialog errstr
                end try
            end tell
        end if
    
        enable fa process new changes ask
    end tell
    

    You can change ‘ask’ in the last line to ‘yes’ or ‘no’, depending on whether you want to automatically process items already in the folder.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search