mfeldman
mfeldman

Reputation: 3

How can I move specific files from within a folder when that folder is added to a directory?

I'm trying to create an applescript that will essentially pull a movie file from a folder when that folder is added to a directory. For instance, when I add a folder containing a movie and other files to my "Movies" directory, I want to be left with the movie file itself, deleting the other unneccesary files.

I've been able to make this work with a manual script but I'd like it to run automatically on add. I think part of the issue is handling the folder coming in. I'm not sure how "these_items" is processed. I've tried handling it as if it were a file and if it was a list of files in the folder.

Here's my attempt so far. I have lines commented out from different trials and I'm using ~/Desktop as my destination folder right now for simplicity.

on adding folder items to this_folder after receiving these_items
tell application "Finder"
    --if kind of these_items is "Folder" then
    --set this_folder to (path to these_items)
    set this_list to every file of these_items
    --repeat with i in this_list
    --set this_list2 to every file of i
    repeat with i from 1 to number of items in this_list
        if (name of i) ends with ".mp4" then
            move i to (path to desktop folder)
            move i to trash
        end if
    end repeat
    --end repeat
    --end if
end tell

end adding folder items to

Upvotes: 0

Views: 922

Answers (1)

adayzdone
adayzdone

Reputation: 11238

Attach this folder action to your "Movies" directory:

on adding folder items to theFolder after receiving theItems
    set destFolder to path to desktop

    repeat with anItem in theItems
        tell application "Finder"
            if kind of anItem is "Folder" then
                move (files of (entire contents of anItem) whose name extension = "mp4") to destFolder
                delete anItem
            else if name extension of anItem = "mp4" then
                move anItem to destFolder
            else
                delete anItem
            end if
        end tell
    end repeat
end adding folder items to

Upvotes: 1

Related Questions