Apple Event: May 7th at 7 am PT

Applescript: Copy files from a folder that contains jpegs using txt files that contains unique codes

I found this very helpful thread:

Automator/Applescript: Copy Files to Fold… - Apple Community


This really helped me! But my situation is a bit different.

My list only contains the unique codes.

So I need to copy all the files that contains the list of unique codes on their filenames.


This is the code that I am using: (I got from the mentioned thread)


---------------------------------------------------------------------------------------------------------------

property defloc : (path to desktop as text) as alias


property msg1 : "Select the input folder"


property msg2 : "Select the output folder"


property msg3 : "Select the input file with filenames"




use scripting additions




set name_list to {}


set match_list to {}




try

set infolder to (choose folder default location defloc with promptmsg1 without invisibles, multiple selections allowed and showing package contents)

set outfolder to (choose folder default location defloc with promptmsg2 without invisibles, multiple selections allowed and showing package contents)

set name_file to (choose file default location defloc with promptmsg3 without invisibles, multiple selections allowed and showing package contents)

display dialog infolder's POSIX path & return & outfolder's POSIX path & return & name_file's POSIX path

-- read names from file into quoted filename list

set name_list to read name_file as «class utf8» using delimiter linefeed

with timeout of 1800 seconds

tell application "Finder"

-- get every filename in infolder hierarchy whose filename matches names in name list

set match_list to (every item of entire contents of folder infolder whose name is in name_list) as alias list

-- copy the files to outfolder with exact permissions and overwrite if required

repeat with anItem in match_list

duplicate anItem to outfolder with replacing and exact copy

end repeat

end tell

end timeout

on error errmsgnumbererrnbr

my error_handler(errnbr, errmsg)

end try


return




on error_handler(nbr, msg)

return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

end error_handler


---------------------------------------------------------------------------------------------------------------


My list contains only the unique codes: (screenshot below)


But from the folder where I want my script to copy some files contains variants of the unique codes from my list.


I hope someone could help me. Thanks!


Posted on Jul 19, 2023 10:46 AM

Reply
Question marked as Best reply

Posted on Jul 19, 2023 11:05 AM

Your unique codes list would only allow to match on for example, the base 30F2G7PC5L-001 string, and you would need to determine which of the appended (_1, _2, _3, .etc) jpeg images you want the script to act on — or all of them.


How many files to process and are there sub-folders too?

Similar questions

10 replies

Jul 19, 2023 4:36 PM in response to Teftef

Here is an Automator application that you double-click on your Desktop to run. It will first prompt you for the source folder containing your images, and your codes.txt file. Then, it prompts you for the folder to move code matched files too. This will move all of the associated _n.jpg files for the given match.


I needed to switch from AppleScript to the Zsh shell in order to perform regular expression matches on partial image names.


Steps:


  1. Launch /Applications/Automator.
    1. Select Application in the interface, and then click Choose.
      1. A three panel window will open from left to right Library, Actions, Workflow.
    2. Select Files & Folders : Ask For Finder Items, click and drag to the right into the larger workflow window.
      1. Set Start at: Desktop
      2. Type: Folders (do not check Multiple selection.
      3. This prompt will be your folder containing your images and the codes.txt file.
    3. Repeat step 2. Same settings. This will be the folder where you move the images on matches.
    4. Utilities : Run Shell Script is then dragged to the right below the two previous actions.
      1. Shell: /bin/zsh
      2. Pass Input: as arguments. This will toss a for-loop into the window and you should remove all of that content.
      3. Copy and paste the entire code below into that Run Shell Script window.
    5. Click on the Run Shell Script Results button. Click the Automator Run ▶︎ button to test the Automator application before saving it to your Desktop. You will see the matching files printed in the Results window, but not moved.
      1. When you are satisfied, place a unquoted '#' character (comment) before the print statement, and remove the unquoted '#' character before the move (/bin/mv) command.
  2. Save to your Desktop with a useful name.
  3. Quit Automator


Code:


#!/bin/zsh

: << "COMMENT"
Provide a starting folder and a destination folder to copy the matched files.
Attempt to match the code string to the file basename without the "_n.jpg" 
suffix. On match, images will be moved to the destination folder.
COMMENT

STARTDIR="${1:a:s/\~\//}"
OUTDIR="${2:a:s/\~\//}"

# array declaration
typeset -a codes_list=()

# read the image codes into an array7
codes_list=( $(< ${STARTDIR}/codes.txt) )

# make filenames case-insensitive
setopt nocaseglob

for p in $codes_list;
do
    # get every image in the folder sorted ascending by name (on)
    # the ** is an indication of recursion into the folder tree
    for f in ${STARTDIR}/**/*.(jpg|jpeg)(.Non);
    do
        # codes match basename of file, otherwise get another entry
        # strip off the _n part of the filename here to get a match
        [[ "${p}" =~ "(${${f:r:t}[1,-3]})" ]] || continue

        # no overwrites allowed in the OUTDIR
        [[ "${f:t}" == ${OUTDIR}/${f:t} ]] && continue
        print ${f:t}
        # /bin/mv "${f:a}" "${OUTDIR}/${f:t}"
    done
done
unset parts_list
exit 0


The finished Automator application should look like this:


Jul 19, 2023 1:49 PM in response to Teftef

Doing some testing now on Ventura. Since we need to make partial code and filename matches, I have switched from AppleScript to Zsh shell where I can use the power of regular expressions and more speed for larger directory structures.


The way I have this written now is if it matches your codes in different sub-folders, it will not overwrite the previous matching file.

Jul 20, 2023 4:47 AM in response to Teftef

Verify that the name of your text file containing the codes is in your image folder, and that the same name is also used in the script.


When you first launch Automator, you select New Document, then click Application, then click Choose. When you are ready to save as an Application, you click Save, give it a meaningful name (e.g. copy_images), and quit Automator. There is no conversion to anything involved.

Applescript: Copy files from a folder that contains jpegs using txt files that contains unique codes

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