Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Applescript to find image and copy to new folder

  • I have a folder (with subfolders) that contain images of different extensions (.jpg, .tif, .psd, etc)
  • I have a list of numbers in excel (example: 12345) that correspond to the image files (example: 12345.jpg)


If my original folder or subfolders contains an image that is listed in my excel column, I want those images copied and put in a new folder.


Can someone help me create a simple applescript? Thanks.

Posted on Jul 21, 2015 7:48 AM

Reply
8 replies

Jul 17, 2017 1:11 AM in response to KreativeKore

Thanks. Got something where it takes images to be searched from a file and copies to dest folder.


set theNewFolder to choose folderwith prompt "Choose the destination folder."

set theOriginalFolder to choose folderwith prompt "Choose the folder containing your images."


set theDocument to choose filewith prompt "Please select a document to process:"

set theNumbers to paragraphs of (read (theDocument as alias))


tell application "Finder" to set theFiles to files of the entire contents of theOriginalFolder as alias list


set theFilesToDuplicate to {}

repeat with thisNumber in theNumbers

set thisNumber to contents of thisNumber

repeat with thisFile in theFiles

set thisFile to contents of thisFile

if (thisFile as text) contains (thisNumber as text) then

copy thisFile to the end of theFilesToDuplicate

exit repeat

end if

end repeat

end repeat


tell application "Finder" to duplicatetheFilesToDuplicatetotheNewFolder with replacing

Jul 21, 2015 3:57 PM in response to KreativeKore

A simple script? The following script is both simple and “pure” AppleScript. It has been tested under OS X 10.10.4 and seems to work pretty well when the number of subfolders and pictures is small. You might want to give it a try.


set theNewFolder to choose folderwith prompt "Choose the destination folder."

set theOriginalFolder to choose folderwith prompt "Choose the folder containing your images."


set theNumbers to {12345, 23456, 34567, 45678, 56789, 67890} -- to replace with your own list of numbers


tell application "Finder" to set theFiles to files of the entire contents of theOriginalFolder as alias list


set theFilesToDuplicate to {}

repeat with thisNumber in theNumbers

set thisNumber to contents of thisNumber

repeat with thisFile in theFiles

set thisFile to contents of thisFile

if (thisFile as text) contains (thisNumber as text) then

copy thisFile to the end of theFilesToDuplicate

exit repeat

end if

end repeat

end repeat


tell application "Finder" to duplicatetheFilesToDuplicatetotheNewFolder with replacing

Jul 21, 2015 4:50 PM in response to Pierre L.

Thanks but I'm new to applescript. Maybe this can help you formulate it better for me, so I can understand.


Original Images folder with subfolders: Desktop/Images

New folder were I want files duplicated to: Desktop/NewImages

List of numbers I want duplicated: images.txt


I only want numbers that match the ones in that list to be duplicated to the new folder.


I see the destination folder and containing folder, that helps. But maybe show where I can list the .txt or excel file for it to match numbers.


thanks for your time and help.

Jul 21, 2015 5:37 PM in response to KreativeKore

If you want, you can replace the first two lines of the script with

set theNewFolder to (path to desktop as text) & "NewImages:" as alias

set theOriginalFolder to (path to desktop as text) & "Images:" as alias

However, the only real issue is your list of numbers. First, I don't use Excel, but Numbers. Second, that list seems to be in a text file (images.txt). Without more details, I don't see how the script could get the correct list of numbers from that text file.

Jul 21, 2015 6:53 PM in response to KreativeKore

Let's give just an example. Suppose your list of numbers has been written into a TextEdit document saved to the Desktop as Plain Text under the name "My list.txt", as below:

User uploaded file


Then you would just have to replace

set theNumbers to {12345, 23456, 34567, 45678, 56789, 67890}

with

set theNumbers to paragraphs of (read ((path to desktop as text) & "My list.txt" as alias))

in the script.

Applescript to find image and copy to new folder

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