How to move files into folders that contains same name?

I guys,
This forum is amazing!
I’m new to applescript (and to scripting in general - made a game on scratch :rolleyes: and now im trying to learn new stuff).

I promise I looked all over the web but cant get the result I need.

I have some some folders inside a container folder.
And I have images in the same container folder.
(the image names and folder names are not exact but they are very similar)

What I want to do:

If image name contains folder name then duplicate image to that folder.
repeat process until all images are duplicated to their folders.

I have tried a billion different scripts and dug all over the internet and forum.

But didn’t even come close.

Go ahead and laugh, last script I was trying was this one:


set targetFolder to (choose folder with prompt "Where are the files and folders?")

tell application "Finder"
	set folderList to every folder of folder targetFolder
	duplicate (every file of targetFolder whose name contains folderList)
end tell

Could anyone give me a headsup ?

Thanks in advance

Model: macbook pro
AppleScript: Version 2.6 (152)
Browser: Safari 537.36
Operating System: Mac OS X (10.8)

Welcome to MacScripter :slight_smile:

You can try something like this:


set mainFolder to (choose folder)

tell application "Finder"
	set myFiles to files of mainFolder
	repeat with aFile in myFiles
		set {fileName, fileExt} to {name, name extension} of aFile
		set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
		set targetFolder to (folders of mainFolder whose name contains baseName)
		if targetFolder ≠ {} then duplicate aFile to (first item of targetFolder)
	end repeat
end tell

Hello.

This is another take. which should be just a bit more efficient, since it performs just one query, but it isn’t timed!

set theFol to "Macintosh HD:Users:You:Your:folder:with:Pictures" as alias
tell application id "MACS"
	set fileList to name of every document file of folder theFol whose name extension is "jpg" or name extension is "JPG"
	set folderList to name of every folder of folder theFol
	repeat with aFile in fileList
		repeat with aFolder in folderList
			if aFolder is in aFile then
				-- the file won't be copied if it is there from before
				try
					duplicate file aFile of theFol ¬
						to folder aFolder of theFol replacing no
				end try
			end if
		end repeat
	end repeat
end tell

This is amazing!

both scripts work very well!!

McUsrII script is cool because it is very fast.

And adayzdone is cool because it uses the “contains” that I really needed.

Mc Usrll how would I use your script with a “contain” parameter?

Thnk you so much guys!

Hello.

The script uses contains, insofar that is in, can be read as is contained by, and is the inverse operation of contains, since, as I understood it, the folder name was to be part of a filename, I test if the folderName is in → is contained by the filename

Anyways here is an updated version that uses contains, I have inverted the structure of the repeats to keep it in sync.

set theFol to "Macintosh HD:Users:You:Your:folder:with:Pictures" as alias
tell application id "MACS"
	set fileList to name of every document file of folder theFol whose name extension is in {"jpg", "JPG"}
	set folderList to name of every folder of folder theFol
	repeat with aFolder in folderList
		repeat with aFile in fileList
			if aFile contains aFolder then
				-- the file won't be copied if it is there from before
				try
					duplicate file aFile of theFol ¬
						to folder aFolder of theFol replacing no
				end try
			end if
		end repeat
	end repeat
end tell

Hey McUsrII thanks for the reply. Very helpfull! (more than that, I am even ashamed of how you guys help out. Hope to contribute in the future)

I think I may have expressed myself wrong.

Indeed it was:
If filename is contained by folderName

So should I change:

 if aFile contains aFolder then

to

 if aFolder contains aFile then

?

Thanks!

Hello.

Say we are having a file named acmeWidgets, and a folder named acme:

The folder acme is contained by acmeWidgets, so the test should be:

if aFile contains aFolder

the same expressed differently:

if aFolder is in aFile

You can ofcourse also be circumlocational and write:

if aFolder is contained by aFile

By the way, thinking a little about your script, I have a suggestion that you start it off by something below, if you are looking at the folder when you run it anyway:

tell application id "MACS"
	set theFol to target of its front window as alias
	activate
	display dialog "This script will move image files in the Folder: " & name of theFol & " to subfolders that are contained in the image names." with title "Move picture files" with icon caution
end tell

Ok That makes it clear!

So I really got it all messed up with the basics.

After all what I really needed was:

if aFile is in aFolder then

I updated that line but still didn’t get it to work out! I’ll figure it I think ;).
thank you so much

I clearly cant figure it.

I did some try outs but this should work shouldnt it?

set theFol to "Crucial SSD:Users:abarro:Desktop:teste" as alias
tell application id "MACS"
	set fileList to name of every document file of folder theFol whose name extension is "jpg" or name extension is "JPG"
	set folderList to name of every folder of folder theFol
	repeat with aFile in fileList
		repeat with aFolder in folderList
			if aFile is in aFolder then
				-- the file won't be copied if it is there from before
				try
					move file aFile of theFol ¬
						to folder aFolder of theFol replacing yes
				end try
			end if
		end repeat
	end repeat
end tell

Hi McUsr. I am clearly out of the loop, where did tell application id “MACS” come from ?

Is that just another way of telling Finder or is there any advantage to using it?

Hello. There is no advantage, here, and four letter creator types are at least on its way to be depreceated.

The advantage here, as I was using AppleScript Editor, without any configured word expansions, was less typing. :slight_smile:

At least using the bundle identifer for accessing an app instead of using its name, should speed up the lookup of the app for launch services, its of course more gainful if you call that app from a handler or something from within a loop.

Edit

A swift counting reveals that it isn’t any typing to be saved when dealing with Finder, the payoff comes with System Event’s which creator type is “sevs”. :slight_smile:

Not really, the speed difference of the lookup functions for name and bundle identifier is negligible.
And for the run speed of any script is completely irrelevant, because the terminology is resolved at compile time :wink:

Yes, I know that the speed difference is like when you optimize the invariants in the if tests for speed. :slight_smile: But on the other hand, launch services may be pretty occupied at times, so at least it doesn’t hurt to relieve it from some work. This goes at least for standard applications that Apple has delievered with the system, and not for any third party apps, since I believe the usage of the bundle identifier/creator code, may then be stuck with an older version of an app.

So, what I really do, is short circuiting a nice scheme for getting the latest app, as launch serviceses will resolve your script to use the latest version when you use the name of the app, and not its creator type/bundle identifier.

And the optimization of the lookup was what I was after really. But, what I should have used was the bundle identifier instead of the creator type. com.apple.finder will certainly work in the future. One day, the creator type will be dropped.

You shouldn’t need to worry about a folder having the same name as an image file. the image file should have an extension. I just scanned through this post, but if I’m way off course, then disregard. :slight_smile: