0
votes

I GET 2 ERRORS.

OBJECTIVE: In my downloads folder I have:

FOLDER: fileName 001 IMAGE: fileName 001 preview.jpg FOLDER: fileName 002 IMAGE: fileName 002 preview.jpg FOLDER: fileName 003 IMAGE: fileName 003 preview.jpg

I want Automator to move all previews inside their respective folders.

So, I followed this similar tutorial and I got this so far:

1st)

on run {input, parameters}
    return input
end run

2nd) Filter Finder Items: Kind is Folder 3rd) Set Value of Variable: FilePath ( path where image has to go ) 4th) IN THIS STEP I GET AN ERROR: Check the actions properties ( I'm new so don't know ).

on run {input, parameters}
    tell application "Finder"
        set FileName to name of file input
    end tell
end run

5th) Set Value of Variable: FilePath ( path where image has to go ) 6th) Filter Finder Items: Kind is Image AND Name contains {FilePath} ( same path as folder name ).

The problem occurs with the {FilePath}, Automator doesn't accept the newly created variable: FilePath, in the contains field, the Newly created variable called FilePath.

7th) Get Value of Variable: FileName 8th) Move Finder Items to: FilePath

Here is the workFlow file.

1

1 Answers

0
votes

The link to your workflow file didn't work, but it's ok—thanks for trying to share it anyway, but, in this instance, I can work without it, as I'm actually going to do it from scratch:

Automator workflow

Depending how your workflow will be setup to receive the input, you can remove the Get Specified Finder Items action and allow the workflow to run as a Service, for example, whose input will be the folder that contains images to be processed. As you said it was your Downloads folder that contains the images, I felt it was unlikely that you'd be setting up the workflow to work as a Folder Action.

For testing purposes, you can keep the Get Specified Finder Items action and replace the Example folder I've put in there with your Downloads folder.

Here's the code from the Run AppleScript action for you to copy/paste:

    on run {input, parameters}
        # input will be the folder containing the images, e.g. the Downloads folder
        set [here] to the input
        
        set suffix to "preview.jpg"
        set my text item delimiters to {pi, space & suffix}
        
        
        tell application "Finder"
            set jpgs to the name of every file in here whose name ends with the suffix
            
            repeat with jpg in jpgs
                set this to the text items of jpg as text
                set there to (a reference to the folder named this in here)
                
                if not (there exists) then set there ¬
                    to make new folder in here ¬
                    with properties {name:this}
    
                move the file named jpg in here to there
            end repeat
        end tell
    end run

It matters not whether the destination folders exist: if they do, the image will be moved into the appropriate one; for those that don't, the folder is created before the image is moved.

Update In Response To Comments

① Sometimes I have a string between "preview" and extension .jpg or .png

② I'd like to also move the completed folders to a new folder /Users/sebba/BLENDER/BLENDS

Given the variability of your filenames that you've now disclosed, I think it's easier to use a shell script instead of an AppleScript to process the files.

Delete the Run AppleScript action and insert a Run Shell Script action, with the following options: Shell: bin/bash, Pass input: as arguments.

Delete any sample code that appears in the editing field and enter this code:

    cd "$1"
    folder=()

    while read -r file
    do 
        folder+=("$(egrep -io '^.+ \d{3}' <<<"$file")")
        [[ -d "${folder[@]: -1}" ]] || mkdir "${folder[@]: -1}"
        mv -v "$file" "${folder[@]: -1}"
    done <<<"$( ls | egrep -i '^.+ \d{3} preview.*\.(jpg|png)' )"

    mv -v "${folder[@]}" "/Users/sebba/BLENDER/BLENDS"

This uses a regular expression match to pick out filenames that:

  1. Begin with absolutely anything (but not nothing); then
  2. Follow with a space, precisely three digits, a space, then the word "preview"; then
  3. Follow with absolutey anything (including nothing); then
  4. End with ".jpg" or ".png".

Therefore, it will move the following files to the given folder (the folder is created if it doesn't exist, and files with the same name are overwritten):

  • My image_file 001 preview.foo bar.jpgMy image_file 001/
  • 004 2 001 preview.png004 2 001/

But the following files won't be affected:

  • 001 preview.png
  • My image_file 01 preview.jpg