I’m pretty new to applescript and I’m trying to create a simple script that looks for mkv files in a folder (and it’s subfolders) and moves it to another folder. Here is what I have so far, but it only pulls files from the main folder. Any thoughts?
property extension_list : {"mkv"}
on adding folder items to this_folder after receiving added_items
repeat with this_item in added_items
tell application "Finder"
if (the name extension of this_item is in the extension_list) then
move this_item to "{destination folder}"
end if
end tell
end repeat
end adding folder items to
Plenty stuff turns up when you search this forum for ‘recursive’.
This is what I’m using.
Whatever you use, it’s only going to be triggered by files added to the main folder. That’s just how folder actions work.
Yeah, I did a pretty extensive search and nothing came up with recursive searching in folder actions. This seems like a huge limitation for something that should be very common and simple.
Dropped files go into the container”not its subfolder(s). Why, then, if you want to affect a subfolder, would you iterate through a list of dropped files (added-items)? Skip the folder action and just move the filtered entire contents.
If you are looking for files that already exist in the folder :
set theFolder to "~/Desktop/test"
try
set theFiles to every paragraph of (do shell script "ls -R " & theFolder & " | grep -e .mkv$")
end try
I would like the script to activate anytime a file is dropped in the main folder or it’s subsequent folders. The reason is I’m using a download program that creates a folder for each file downloaded and moves it to that main folder. So i want to script to look for any new folder created and search it to see if a specific file is in it, then move it.
Regardless of how it is activated, my script should find all of the .mkv files in the folder.
This is not possible unless you assign the folder action script to each subsequent folder
I’m using a demo of Hazel right now to accomplish this task. I’d like to find another way to do it without purchasing software. Any other options?
Hey Stefan,
Is there any way of setting up a smart folder that would filter the contents of the main folder, and then using that for the script?
In this case that would be breaking a fly on the wheel, you can filter and move the files with an one-liner.
tell application "Finder" to move (files of this_folder whose name extension is in extension_list) to folder destinationfolder
Anyway consider that a smart folder is a virtual folder, you can’t “touch” it
tell application "Finder" to move (files of this_folder whose name extension is in extension_list) to folder destinationfolder
Does that work recursively?
No, this works recursively, but it’s pretty slow
tell application "Finder" to move (files of entire contents of this_folder whose name extension is in extension_list) to folder destinationfolder
I did not know that, thanks!
This still only works in the root folder.
That’s how folder actions work. Imagine if they didn’t, and you attached one to your home folder. Between constantly checking every folder for changes, you probably wouldn’t get a lot of CPU time.