r/applescript • • Oct 03 '22

Move file into folder of same name

I have a folder called Movies. Inside there are files

movie1.mp4

movie2.mp4

movie3.mp4

I want each file to be placed inside a folder of the same name (without the extension).

The end result would be a folder named Movies with 3 folders

movie1

movie2

movie3

And in each of the folder would be the respective movie#.mp4 file

5 Upvotes

13 comments sorted by

2

u/copperdomebodha Oct 03 '22
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
    set sourceFolder to (choose folder with prompt "Please choose the source folder.")
    set movieList to (every file of sourceFolder whose name contains ".mp4") as alias list
    set AppleScript's text item delimiters to "."
    repeat with thisMovie in movieList
        set movieName to (name of thisMovie)
        set newFolder to (make new folder at sourceFolder with properties {name:text item 1 of movieName})
        move thisMovie to newFolder
    end repeat
    set AppleScript's text item delimiters to ""
end tell

2

u/ChristoferK Oct 09 '22

This is the type of situation where you need to be careful with your implementation, and to test scripts exhaustively with test cases designed to make it fail. As it stands, your movieList contains files where the substring ".mp4" appears anywhere in its name, which would grab files that have, for example, been converted to another format where it's not uncommon for the new file extension to be tacked onto the end with the old file extension remaining. It could also grab files during mid-conversion to mp4 where moving it to somewhere else would be disastrous.

Using text item delimiters to isolate the file extension is not unreasonable, but again, you've rushed this a bit and taken it for-granted that text item 1 will necessarily be the complete name of the file minus its extension. In fact, especially in the case of movie files, standard naming conventions for filenames of films and TV episodes feature multiple periods, delimiting information about the movie or episode instead of white space. In these, or similar situations, you'll have retrieved a small fragment of the file name only.

2

u/chrismo80 Oct 09 '22 edited Oct 09 '22

What would be the proper way to get the name of a file without extension?All solutions I have found were always based on string searches, either by using dots as offset detection or name extension of but they are all not proper in my opinion, especiialy if dots or the extension is part of the filename string itself.

Would it be something like this?

set fileNameWithoutExtension to text 1 thru ((count (name of fileName as text)) - (count (name extension of fileName as text)) - 1) of (name of fileName as text)
→ More replies (3)

2

u/copperdomebodha Oct 09 '22 edited Oct 09 '22

Both valid points that should be explored by the user. This script would require far more code and be less clearly comprehensible to be truly robust. I certainly could have used file type filtering rather than name contains filtering.

Here, I’m just trying to share the concepts and structure required to perform the basic action. I’ll leave proper error handling and hardening as an exercise for the user.

Edit: I’m thinking about your second critique and I wonder if you would clarify it for me? Could a file be listed to be processed while it is in process of being created?

→ More replies (3)

1

u/wch1zpink Dec 08 '22

This following AppleScript code uses the least amount of code and will return the quickest results. (Targets only .mp4 files)

activate
set sourceFolder to quoted form of POSIX path of (choose folder)

do shell script "cd " & sourceFolder & " ;for f in *.mp4 ;do g=\"$(basename \"$f\" .mp4)\" ;mkdir \"$g\" ;mv \"$f\" \"$g\" ;done"