Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help with AppleScript/ Automator Code

I am an intern at an architecture firm, and I would like to know if there is any way to code a program (using AppleScript or Automator) to complete this task:


Combine files that have similar file names into a folder and rename the file as the first file automatically. There are hundreds of files, and an expedient and efficient way of completing this task would be greatly appreciated.


For example:


I have files:

07-480-1.jpg

07-480-2.jpg

07-480-3.jpg

07-480-4.jpg

07-481-1.jpg

07-481-2.jpg

07-481-3.jpg

07-481-4.jpg

07-481-5.jpg


I want to select files names with similar characters (07-480 and 07-481) and place them into respective folders.


So the folders would be called

07-480-1

07-481-1


Any advice would be helpful.

Thank You!



OSX 10.9.5

MacBook Pro, OS X Mavericks (10.9.5)

Posted on Apr 28, 2015 2:42 PM

Reply
5 replies

Apr 28, 2015 3:58 PM in response to WMCRP

Almost anything is possible... the issue is the work-effort required to achieve the desired result.


In this case there are a lot of variables that you haven't covered, which may make the job easier or harder.


For example, you give a series of example file names, and appear to want to group them by the same initial 6 characters (although your example seems to create folder names based on the first 8 characters of the first match... is that true? Without knowing the background, I would expect '07-480-1.jpg' to go into the '07-480' folder, not the '07-481-1' folder). Those kinds of details will make a difference.


So if my theory is correct ('07-480' should be the folder name), do you want to create a folder every time? or only when there are two or more files (e.g. do you want an '07-490' folder when there's only one relevant file?


Here's a starting point. I'm assuming that you want the '07-480' form of the folder. When the script runs it will ask for the folder to check, then (blindly) iterate through the files in that folder, moving them into folders based on the first 6 characters of the file name, making new folders as needed:


tell application "Finder"

set theFolder to (choose folder)


repeat with eachfile in (get files of theFolder)



-- assume the standard filename format


-- may need error checking here to make sure the name matches what we expect


-- for now we just skip over any errors through the use of a try statement

try


-- get the first 6 characters of the file name

set basename to characters 1 through 6 of (get name of eachfile) as text



-- check if we already have a folder of the desired name

if (not (exists folder basename of theFolder)) then


-- we don't, so make one


makenewfolderattheFolderwith properties {name:basename}

end if



-- now move the file into the folder


moveeachfiletofolderbasename of theFolder

end try

end repeat

end tell

May 1, 2015 11:34 AM in response to WMCRP

Hello


You might try something like the following shell script. It will process *.jpg files in current directory.



#!/bin/bash # # move A-B-N.jpg -> A-B-M/A-B-N.jpg, # where # - A, B, N do not contain '-'; and # - N is integer index; and # - M is the smallest index N in file names matching A-B-N.jpg. # # E.g., given - # # ./07-480-1.jpg # ./07-480-2.jpg # ./07-481-9.jpg # ./07-481-10.jpg # # result - # # ./07-480-1/07-480-1.jpg # ./07-480-1/07-480-2.jpg # ./07-481-9/07-481-9.jpg # ./07-481-9/07-481-10.jpg # m1='' find . -type f -iname '*.jpg' -depth 1 -print0 | sort -z -t- -k1,2 -k3n | while read -d $'\0' f do [[ $f =~ ^([^-]+-[^-]+)-[^-/]+$ ]] || continue m=${BASH_REMATCH[1]} if [[ $m1 != $m ]] then m1=$m d=${f%.*} mkdir -p "$d" fi mv "$f" "$d" done




If you want to use it in an Automator workflow, try this -


1) Ask for Finder Items action

- choose a folder


2) Run Shell Script action

- shell = /bin/bash

- pass input = as arguments

- code = as follows



#!/bin/bash # # move A-B-N.jpg -> A-B-M/A-B-N.jpg, # where # - A, B, N do not contain '-'; and # - N is integer index; and # - M is the smallest index N in file names matching A-B-N.jpg. # # E.g., given - # # ./07-480-1.jpg # ./07-480-2.jpg # ./07-481-9.jpg # ./07-481-10.jpg # # result - # # ./07-480-1/07-480-1.jpg # ./07-480-1/07-480-2.jpg # ./07-481-9/07-481-9.jpg # ./07-481-9/07-481-10.jpg # m1='' find "$1" -type f -iname '*.jpg' -depth 1 -print0 | sort -z -t- -k1,2 -k3n | while read -d $'\0' f do [[ $f =~ ^([^-]+-[^-]+)-[^-/]+$ ]] || continue m=${BASH_REMATCH[1]} if [[ $m1 != $m ]] then m1=$m d=${f%.*} mkdir -p "$d" fi mv "$f" "$d" done




Automator workflow will look something like this:


User uploaded file




Briefly tested under OS X 10.6.8 but no warranties of any kind.


Please make sure you have complete backup of files and directories before running this sort of script.



Regards,

H

Help with AppleScript/ Automator Code

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.