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
Respectfully, I disagree with this ethos. At the minimum, you ought to include an explanatory note accompanying the script explaining to a user (many of whom are new to AppleScript, and new to scripting in general) that this is the case. I feel that intentionally posting a script that you are stating here knowingly contains improper coding choices, and failing to highlight specific parts of the script that might require error-handling, is, at the best, careless, and at worst, extremely irresponsible.
I think this is a reason why posting a script by itself with no accompanying text is not a good example to set for others. Explaining what the script does at each step would be much more educational to a novice scripter than these supposed "exercises for the user".
But I appreciate that we all have different opinions on things like this, and maybe I have the opposite problem and write too much explanation.
Absolutely, yes. There's nothing intrinsically special about a file that is being operated upon that limits our ability to retrieve its properties, in order to get its filename. Moving a file is also not inherently illegal, and it relies on a conscientious developer to place a lock on the file as its operating on it. Otherwise, it's even possible to open multiple file handles to a single file for two programs to both write to it simultaneously.
A demonstrable example that illustrates a couple of these very issues is in the implementation of a folder action that acts upon a folder that also syncs to iCloud. When a new file is added to the cloud, a new placeholder file is added to the watched folder into which data is being downloaded from iCloud as it syncs (e.g. creates) a local copy. If the folder action runs a script that is designed to move new files (which will also be triggered by existing files that are updated by a sync) out of the watched folder to some other folder, the script targets the placeholder file being written to. The effect is interesting and deleterious in this scenario, because the iCloud placeholder is a temporary swap file that would ultimately be replaced by the fully-formed file once downloading is complete, however it gets moved out of the folder mid-way through writing. The moment it leaves the local iCloud folder, the placeholder file disappears, possibly because interrupting the data transfer is equivalent to cancelling the sync. This might ordinarily leave the swap file in place until sync continues at a later point, but in moving it out of the folder, it signifies the removal of the local copy, which then syncs back to iCloud and deletes the iCloud copy.
A more everyday example is when converting a video file from one format to another.
ffmpegdoesn't use a swap file—it just creates the new video file to which it writes data in a non-atomic fashion, all the while leaving it open to being moved, deleted, renamed. This wouldn't be intrinsically a problem ifffmpegused a file id reference, which would maintain a link to the file even after it gets moved or renamed. But it appears to just used a file path reference, which breaks as soon as the file path changes.