Community Forums Archive

Go Back

Subject:Simple batch script
Posted by: ForumAdmin
Date:11/11/2009 10:31:03 AM

The topic of manually processing all files in a folder comes up every now and then, so I've posted a sample script demonstrating one method. It has proven useful for simple conversions.


// convert_folder.cs
using System;
using System.IO;
using System.Collections;
using SoundForge;

public class EntryPoint {

public string Begin(IScriptableApp app)
{
// get folders
string srcDir = SfHelpers.ChooseDirectory("Select source folder", @"c:\");
if (srcDir == null || srcDir.Length <= 0)
return ("No source directory.");

string dstDir = SfHelpers.ChooseDirectory("Select destination folder:", @"c:\");
if (dstDir == null || dstDir.Length <= 0)
return ("No destination directory.");

// get renderer and template
int cRend = app.Renderers.Count;
string [] renderers = new string[cRend];
for (int ix = 0; ix < cRend; ix++)
renderers[ix] = app.Renderers[ix].Name;
object item = SfHelpers.ChooseItemFromList("Select destination file type:", renderers);

ISfRenderer rend = app.FindRenderer(item.ToString(), null);
if (null == rend)
return ("No renderer.");

ISfGenericPreset tpl = rend.ChooseTemplate(IntPtr.Zero, 0);
if (null == tpl)
return ("No template.");

// process until done or cancel
Script.Forge.ErrorMode = ForgeErrorMode.SuppressErrorBoxes;

foreach (string srcFile in Directory.GetFiles(srcDir, "*.*"))
{
SfStatus result = SfStatus.Success;

try
{
string dstBase = Path.GetFileNameWithoutExtension(srcFile);
string dstFile = Path.Combine(dstDir, dstBase + "." + rend.Extension);

ISfFileHost file = app.OpenFile(srcFile, false, true);
file.RenderAs(dstFile, null, tpl, null, RenderOptions.RenderOnly);
result = file.WaitForDoneOrCancel();
file.Close(CloseOptions.DiscardChanges);
}
catch (Exception e)
{
DPF("Failed to open {0}, {1}", srcFile, e.Message);
result = SfStatus.Fail;
}

DPF("{0} : {1}", srcFile, result.ToString());
if (result == SfStatus.Cancel)
break;
}

Script.Forge.ErrorMode = ForgeErrorMode.Normal;

return null;
}

public void FromSoundForge(IScriptableApp app)
{
ForgeApp = app;
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
string msg = Begin(app);
app.SetStatusText(msg != null ? msg : String.Format("Script '{0}' is done.", Script.Name));
}

public static IScriptableApp ForgeApp = null;
public static void DPF(string sz) { ForgeApp.OutputText(sz); }
public static void DPF(string fmt, object o) { ForgeApp.OutputText(String.Format(fmt,o)); }
public static void DPF(string fmt, object o, object o2) { ForgeApp.OutputText(String.Format(fmt,o,o2)); }
public static void DPF(string fmt, object o, object o2, object o3) { ForgeApp.OutputText(String.Format(fmt,o,o2,o3)); }
} //EntryPoint


J.

Subject:RE: Simple batch script
Reply by: ExSonyTJ
Date:11/20/2009 9:00:48 AM

When you SuppressErrorBoxes, it's important to make sure that you also clear any error text before your script exits or the suppressed error text will show up (erroneously) as an error on the next command you execute.


In your catch, you should call GetErrorReport(hr, true), like this


catch (Exception ex)
{
COMException exc = ex as COMException;
if (exc != null)
{
DPF("com exception: {0}", exc.Message);
string strReport = Script.Forge.GetErrorReport(exc.ErrorCode, true);
DPF("report: {0}", strReport);
}
else
{
DPF("non com exception: {0}", ex.Message);
}
}


Subject:RE: Simple batch script
Reply by: jijotomy
Date:11/20/2009 1:01:21 PM

This is the first time I'm running any script. I tested your script and got his error: Compiler error 0x80004005 on Line 25,18 : 'SoundForge.SfHelpers' does not contain a definition for 'ChooseItemFromList'

Can you please tell me what I'm doing wrong?

Subject:RE: Simple batch script
Reply by: ForumAdmin
Date:11/24/2009 9:25:29 AM

I assume you are using Forge 8.0. Those helpers were added in Forge 9.0. You can specify the renderer directly by its extension or the full name as displayed in the Save As dialog instead.

Replace this:

int cRend = app.Renderers.Count;
string [] renderers = new string[cRend];
for (int ix = 0; ix < cRend; ix++)
renderers[ix] = app.Renderers[ix].Name;
object item = SfHelpers.ChooseItemFromList("Select destination file type:", renderers);

ISfRenderer rend = app.FindRenderer(item.ToString(), null);
if (null == rend)
return ("No renderer.");

with this:

string fmt = ".mp3"; // or the extension of whatever you want to render to
ISfRenderer rend = app.FindRenderer(null, fmt);
if (null == rend)
return("No renderer.");

or this:

string fmt = "MP3 Audio"; // or a different full name you see in the Save As dialog
ISfRenderer rend = app.FindRenderer(fmt, null);
if (null == rend)
return("No renderer.");


J.


Message last edited on11/24/2009 9:31:02 AM byForumAdmin.

Go Back