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)
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
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
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
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
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
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.
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”.
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
Yes, I know that the speed difference is like when you optimize the invariants in the if tests for speed. 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.