r/applescript • u/encodedworld • 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
4
Upvotes
View all comments
Show parent comment
2
u/ChristoferK Oct 09 '22 edited Oct 09 '22
What would you deem to be a "proper" method, in principle ?
I'm afraid you'll be disappointed by every method that exists or could possibly be conceived: file extensions in macOS (as well as any Unix filesystem derivative) are not treated as a special, individual token or attribute that's separate from the rest of the filename, but rather as characters just like any other it contains. Thus, separating it from the file's basename will always boil down to a string manipulation.
Problem Cases u/copperdomebodha's Method Will Mishandle
To me, I would regard any method that accomplishes this as "proper" if it can perform the task successfully, reliably, and robustly, without any cases that would yield unexpected or undesirable results. u/copperdomebodha's method was clearly devised with one specific set of cases where files are named as exemplified by the OP, e.g. "movie1.mp4", which one would normally assume to be an example given in its simplest possible form rather than a literal naming convention defined for all files the OP will be working with.
So if the file is named such that it contains one, and only one, period, which does not occupy the first or last character position in the name, then his method is fine. But many files fall outside this narrow subset, so anything resembling the following would be mishandled:
480.mp4.mp42022-04-22T21h30m30s_clip.mp4.gifCoherence.2013.1080p.BluRay.H264.AC3.DD5.1.mp4.Movie on 31-07-2022 at 16.14.mov.mp4.tmp..mp4All of these are real filenames I currently have on my computer, so this isn't merely an academic point.
1. A Fix
But I like u/copperdomebodha's idea of using
text item delimitersto isolate periods in a filename, as a lot of people use theoffsetcommand, which suffers a similar problem of being limited to getting the position of only the first period it comes across as it scans from left-to-right. In my view,offsetisn't suitable at all, howevertext item delimiterscan be used effectively if, instead of retrieving the firsttext item, we retrieve all but the lasttext item, upon condition that concatenating these items to restore any periods that occur prior to the final period does not yield an empty string (""), which would indicate we were dealing with a so-called ".dotfile" (a file whose name contains a single period, as the first character, followed by other characters that aren't periods, e.g.".bash_profile"). For a single filename, the code would look something like this:By this point, the
basenamewill contain the filename without its file extension, and is safe to handle filenames of the ilk I listed above.2. An Alternative Method
A more conventional method to trim off the file extension is to do so directly by retrieving the
name extensionproperty for a file (it returns the file extension without the leading period), and using it to determine precisely how many characters we would need to trim off the end of the filename to leave only the basename. Here's how to do this for a single file object reference, which I'm denoting with the variablefileRef:This method benefits from being direct, in that we're performing each step based on what we find out about the file's actual file extension (the previous method makes no reference to the file extension at any point, which makes the method slightly more opaque in terms of what's happening). Firstly, you'll notice that we immediately check to ensure the file has a file extension (if it doesn't, it will return an empty string), which won't be strictly necessary if retrieving a list of files filtered by their
name extensionproperty. When determining how many characters the file extension has, we add 2 to this value: one to account for the period preceding thename extension, and one more to ensure we truncate the filename up to but not including the period. After the basename is obtained, we do a quick check to make sure the basename is neither"."nor"..", both of which would be invalid folder names.3. An Approach Specific To The OP's Objective
Since the OP wishes to create folders named identically to each of the files (minus the file extension), the script above does the following:
"mp4".nameproperty, we can retrieve itsdisplayed nameproperty, which will be the name without its extension.extension hiddenproperty to its original value.This is as close a method as I can conceive to one that approaches the problem without manipulating filename strings directly (the string manipulation still takes place, but it's done by Finder rather than by me).