MacNN Forums (
-   Mac OS X (
-   -   Script to do an action when a movie file is finished saving? (

FireWire Sep 16, 2013 03:55 AM
Script to do an action when a movie file is finished saving?
Hi! I'm frequently converting iMovie files to .mkv and then upload them via FTP. This is time consuming and as I convert the file during the night, I often have to set an alarm in the middle of the night to begin the uploading process. Is there a way to do a script to detect when the file is finished converting? There's a folder action that triggers when a file is added to a folder but I'm not sure if it works if it try to upload it while it's only partially there.

Also, would there be a way to save directly from iMovie to .mkv without using DivX converter? That would save me a lot of time. thank you!
P Sep 16, 2013 04:24 AM
You don't mention what video format you want to use inside the MKV, but if it can be done with Handbrake, you could use the terminal and something like

HandbrakeCLI -i /source/file -o /output/file && ftp -u /output/file
The && thingy means "only proceed with the second command if the first succeeds".

EDIT: I should mention that if it is a multiuser machine, your ftp password is visible to any other user by simply using ps. This is obviously not a problem if you are the only user, though.
ghporter Sep 16, 2013 07:36 AM
I'd never seen the "&&" operator before. Very useful bit there.

Can the Terminal command you provided be included in a script so that a list of source files could be converted and uploaded until the list is exhausted?
P Sep 16, 2013 08:10 AM
Absolutely! The easiest way is to use the for command (I'm assuming bash or zsh here - csh/tcsh scripting is still considered harmful)


for FILE in file1 file2 file3
  HandbrakeCLI -i /source/$FILE -o /output/$FILE && ftp -u /output/$FILE

The && thing is originally a hack, but it's so useful that it has become standard to make the shell behave like that. A single ; means execute command 2 once command 1 finishes, no matter the result. || is the opposite of && - execute command 2 if command 1 fails. & means start executing command 2 as soon as command 1 has started (it executes independently and can finish before it), and | means execute command 2 using the output of command 1 as input.
shifuimam Sep 16, 2013 11:11 AM
That's pretty awesome. Do you have a link to a site or document explaining this (and possibly other, similarly useful syntax for Bash scripting)? I'll be adding that to my list of "things you should know by now".
P Sep 16, 2013 12:49 PM
As I said, it's an old hack. What happens is this: && is logical AND. AND is true only if both the ingoing variables are true, so if the first process returns false, there is no reason to run the second, because you know that the result will be false. In the same way || is logical OR, and if the first process returns true, you don't have to run the second because you know that the result is true.

(Note that for process exit codes, 0 is true and anything else is false, completely the opposite of all other situations. There is a reason for this - if it's OK, then that's all you need, but if the process fails you want to know why it failed, so you need multiple error codes - but it's confusing enough that bash eventually implemented new special logical operators meant for if tests, reserving the && and || for the hacky use above).

As for documentation: This is where I look up things when I have to:

Advanced Bash-Scripting Guide
All times are GMT -4. The time now is 05:34 PM.

Copyright © 2005-2007 MacNN. All rights reserved.
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2014, vBulletin Solutions, Inc.

Content Relevant URLs by vBSEO 3.3.2