Batch Mencoder Script

Okay, so I’m very new to Applescript (as in…yesterday). But I’m very eager to start digging my teeth in. I thought I would ask some advice on a specific problem that I can’t find any other thread addressing directly (or it ther was I didn’t understand it). It’s probably dead simple for somebody who knows what they’re doing, but here we go:

I’m wanting a script that will run each file in a given folder (the folder will always be the same) through a specific (again, unchanging) mencoder shell command.

So, for example, if I’ve got a movie at ~/Movies/Rips/Test.mkv

And I want to run the following command:
mencoder ~/Movies/Rips/Test.mkv -oac copy -ovc copy ~/Movies/Converted/Test.mpg

Then delete the original file and move to the next one in the folder.

Any suggestions? If something similar has already been addressed and I couldn’t find it, please direct me!

Well, this script is quick & dirty…
source and destination folders are not quoted
the script always assumes that the suffix is 3 characters long
the script does not give a shit if there is already such a file in the destination folder…
.
.
.
But I think you get an idea and can make a better script later on :stuck_out_tongue:

set source_folder to "~/Movies/Rips/"
set destination_folder to "~/Movies/Converted/"

set thefiles to every paragraph of (do shell script "ls " & source_folder)
repeat with thefile in thefiles
	try
		with timeout of 10000000 seconds
			do shell script "mencoder " & source_folder & quoted form of thefile & " -oac copy -ovc copy " & destination_folder & quoted form of ((characters 1 through -4 of thefile) & "mpg" as text)
			do shell script "rm " & source_folder & quoted form of thefile
		end timeout
	end try
end repeat


Awesome! Thanks for a great starting point. I’ll start playing with it and refine as I learn more. I appreciate your time!

Ah, and I see what you did. As a newbee I was unsure of how to get the filename without the extension, but I see how you do it now. Cool stuff. I’m excited.

Since you mentioned his way of getting the file name without the extension I thought I’d point something out to you… as a learning experience. Run this script and look at the result. You will notice it’s a list of letters.

set someFileName to "test.mov"
characters 1 through -4 of someFileName

Now run this one and notice it’s a string…

set someFileName to "test.mov"
text 1 through -4 of someFileName

All I did was change the word “characters” to “text”. So you can see that if you use the word “characters” you get a list and thus have to later coerce it to text so you can use it. I suggest you just use the word “text” to begin with. As such I would change that one part of your code from this…

((characters 1 through -4 of thefile) & "mpg" as text)

to

((text 1 through -4 of thefile) & "mpg")

I hope you learned something and good luck. :smiley:

Here’s one more learning experience for you. Many people use the command “with timeout of” incorrectly. I don’t mean to pick on hubionmac’s script but “with timeout of” only works with applications, it doesn’t work with a “do shell script” command. As a test try this. You will notice the script using the application Finder will timeout in 5 seconds but the script using “do shell script” will not timeout in 5 seconds. So as a learning experience I wanted to show you that. :slight_smile:

tell application "Finder"
	activate
	with timeout of 5 seconds
		display dialog "This is a test... do not close this dialog box until the script times out! You will see it times out in 5 seconds because *with timeout of* works with applications and we are using the application Finder."
	end timeout
end tell
with timeout of 5 seconds
	do shell script "osascript -e 'tell application \"Finder\"' -e 'activate' -e 'display dialog \"This is a test. Notice that your script will not timeout in 5 seconds because *with timeout of* only works with applications and *do shell script* is not an application. So wait longer than 5 seconds before dismissing this dialog box.\"' -e 'end tell'"
end timeout