Community Forums Archive

Go Back

Subject:Need a Simple User Interface
Posted by: sdigre
Date:1/9/2007 12:46:06 PM

I have a working cs (C#) script which combines multiple audio files into one large audio file.

I need to prompt users for variable values, ie. itteration count. Literally, users would be prompted to enter int values.

int iterations = 'user input';

Is there a quick way to implement this without delving heavily into Visual Studio aspects?

Thanks, Steve

Subject:RE: Need a Simple User Interface
Reply by: _TJ
Date:1/9/2007 2:23:26 PM

Writing a form to prompt the user to enter a single text value, (and then parsing it to get an integer) can be done using the System.Windows.Forms.Control class and the System.Windows.Forms.Form class.

You don' t really have to use the Visual Studio forms designer if you don't want to. It would take about 100 lines of code to write just using the Form() class directly, Visual Studio would make that more interactive, but it would write a LOT more code to solve the equivalent problem, which doesn't work so well in a script environment.

here's a script that creates a form and asks for a single line of input, then parses
the input to an integer.


using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using SoundForge;

public class EntryPoint {
public void Begin(IScriptableApp app) {

string sInput;
DialogResult res = GetInput(app, "Input Please?", out sInput);
DPF("GetInput returns {0} value = '{1}'", res.ToString(), sInput);

if (DialogResult.OK != res)
return;

int iInput = 0;
try { iInput = Int32.Parse(sInput); } catch {}

DPF("parses to {0} as integer", iInput);
}


public DialogResult GetInput(IScriptableApp app, string sPrompt, out string sInput)
{
IWin32Window hOwner = app.Win32Window;

Form dlg = new Form();
Size sForm = new Size(520, 250);

dlg.Text = Script.Name + " Input";
dlg.FormBorderStyle = FormBorderStyle.FixedDialog;
dlg.MaximizeBox = false;
dlg.MinimizeBox = false;
dlg.StartPosition = FormStartPosition.CenterScreen;
dlg.ClientSize = sForm;

Point pt = new Point(10,10);
Size sOff = new Size(10,10);

if (sPrompt == null || sPrompt == "")
sPrompt = "Input Value";

Label lbl = new Label();
lbl.Text = sPrompt;
lbl.Width = sForm.Width - pt.X - sOff.Width;
lbl.Height = 14;
lbl.Location = pt;
dlg.Controls.Add(lbl);
pt.Y += lbl.Height;

TextBox edt = new TextBox();
edt.Size = sForm - new Size(20, 70);
edt.Location = pt;
dlg.Controls.Add(edt);

// we position the buttons relative to the bottom and left of the form.
//
pt = (Point)dlg.ClientSize;
pt -= sOff;

Button btn = new Button();
pt -= btn.Size;
btn.Text = "Cancel";
btn.Location = pt;
btn.Click += new EventHandler(OnCancel_Click);
dlg.Controls.Add(btn);
dlg.CancelButton = btn;
pt.X -= (btn.Width + 10);

btn = new Button();
btn.Text = "OK";
btn.Location = pt;
btn.Click += new EventHandler(OnOK_Click);
dlg.Controls.Add(btn);
dlg.AcceptButton = btn;
pt.X -= (btn.Width + 10);

DialogResult res = dlg.ShowDialog(app.Win32Window);
sInput = dlg.Tag as string;
return res;
}

// generic OK button click (sets dialog result and dismisses the form)
private static void OnOK_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
Form form = (Form)btn.Parent;
TextBox edt = (TextBox)form.Controls[1];
form.DialogResult = DialogResult.OK;
form.Tag = edt.Text;
form.Close();
}

// generic Cancel button click (sets dialog result and dismisses the form)
private static void OnCancel_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
Form form = (Form)btn.Parent;
form.DialogResult = DialogResult.Cancel;
form.Tag = null;
form.Close();
}


public void FromSoundForge(IScriptableApp app) {
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
Begin(app);
app.SetStatusText(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


Subject:RE: Need a Simple User Interface
Reply by: sdigre
Date:1/12/2007 4:05:17 PM

Excellent. And I assume I can alter this to (a class perhaps) to call whenever I need int input. Also, may alter my script to include string returns (for directories) and can remove the conversion for that.

Thank you much.

Subject:RE: Need a Simple User Interface
Reply by: _TJ
Date:1/12/2007 4:15:22 PM

It should be easy to convert to a class. But you don't need to use
this to enter a folder name. Sound Forge already has a helper method
for choosing a folder.

string ChooseDirectory(string szDescription, string szDirIn)

This method shows a standard Windows' folder browser and returns
the users choice as a string.

tj

Message last edited on1/12/2007 4:16:02 PM by_TJ.
Subject:RE: Need a Simple User Interface
Reply by: sdigre
Date:1/12/2007 5:34:08 PM

Once again, thanks for the quick feedback and helpful feedback. While I know this is rather basic for programmers, I have the resulting inputs and responses I need.

I could move the int conversion to a method and I may explore having all user promts in one dialog box.

Here is the resulting code. Maybe it will help someone else down the line.


using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using SoundForge;

public class EntryPoint {
public void Begin(IScriptableApp app) {

string sInput1;
string sInput2;
string sInput3;
string sInput4;
string sInput5;

// get input. leave as a string
DialogResult res = GetInput(app, "Primary language, first directory? (eg. englishMod5, sp5, fr1)", out sInput1);

// get input. leave as a string
GetInput(app, "Secondary language, second directory? (eg. englishMod2, spanish, frenchMod6)", out sInput2);

// get input. convert to int
GetInput(app, "Start at file number? (eg. 21)", out sInput3);
// convert to an integer
DPF("GetInput returns {0} value = '{1}'", res.ToString(), sInput3);
if (DialogResult.OK != res)
return;
int iInput3 = 0;
try { iInput3 = Int32.Parse(sInput3); } catch {}
DPF("parses to {0} as integer", iInput3);

// get input. convert to int
GetInput(app, "End at file number? (eg. 102)", out sInput4);
// convert to an integer
DPF("GetInput returns {0} value = '{1}'", res.ToString(), sInput4);
if (DialogResult.OK != res)
return;
int iInput4 = 0;
try { iInput4 = Int32.Parse(sInput4); } catch {}
DPF("parses to {0} as integer", iInput4);

// get input. convert to int
GetInput(app, "Number of second language repeats? (eg. 3)", out sInput5);
// convert to an integer
DPF("GetInput returns {0} value = '{1}'", res.ToString(), sInput5);
if (DialogResult.OK != res)
return;
int iInput5 = 0;
try { iInput5 = Int32.Parse(sInput5); } catch {}
DPF("parses to {0} as integer", iInput5);


// program functionality ..........

}




public DialogResult GetInput(IScriptableApp app, string sPrompt, out string sInput)
{
IWin32Window hOwner = app.Win32Window;

Form dlg = new Form();
Size sForm = new Size(520, 250);

dlg.Text = Script.Name + " Input";
dlg.FormBorderStyle = FormBorderStyle.FixedDialog;
dlg.MaximizeBox = false;
dlg.MinimizeBox = false;
dlg.StartPosition = FormStartPosition.CenterScreen;
dlg.ClientSize = sForm;

Point pt = new Point(10,10);
Size sOff = new Size(10,10);

if (sPrompt == null || sPrompt == "")
sPrompt = "Input Value";

Label lbl = new Label();
lbl.Text = sPrompt;
lbl.Width = sForm.Width - pt.X - sOff.Width;
lbl.Height = 14;
lbl.Location = pt;
dlg.Controls.Add(lbl);
pt.Y += lbl.Height;

TextBox edt = new TextBox();
edt.Size = sForm - new Size(20, 70);
edt.Location = pt;
dlg.Controls.Add(edt);

// we position the buttons relative to the bottom and left of the form.
//
pt = (Point)dlg.ClientSize;
pt -= sOff;

Button btn = new Button();
pt -= btn.Size;
btn.Text = "Cancel";
btn.Location = pt;
btn.Click += new EventHandler(OnCancel_Click);
dlg.Controls.Add(btn);
dlg.CancelButton = btn;
pt.X -= (btn.Width + 10);

btn = new Button();
btn.Text = "OK";
btn.Location = pt;
btn.Click += new EventHandler(OnOK_Click);
dlg.Controls.Add(btn);
dlg.AcceptButton = btn;
pt.X -= (btn.Width + 10);

DialogResult res = dlg.ShowDialog(app.Win32Window);
sInput = dlg.Tag as string;
return res;
}

// generic OK button click (sets dialog result and dismisses the form)
private static void OnOK_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
Form form = (Form)btn.Parent;
TextBox edt = (TextBox)form.Controls[1];
form.DialogResult = DialogResult.OK;
form.Tag = edt.Text;
form.Close();
}

// generic Cancel button click (sets dialog result and dismisses the form)
private static void OnCancel_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
Form form = (Form)btn.Parent;
form.DialogResult = DialogResult.Cancel;
form.Tag = null;
form.Close();
}


public void FromSoundForge(IScriptableApp app) {
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
Begin(app);
app.SetStatusText(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


Subject:RE: Need a Simple User Interface
Reply by: sdigre
Date:1/12/2007 5:35:33 PM

Wow, thanks again. I will definetly replace this functionality.

string ChooseDirectory(string szDescription, string szDirIn)

Subject:RE: Need a Simple User Interface
Reply by: sdigre
Date:1/12/2007 6:37:25 PM

I receive a compile error when attempting to use ChooseDirectory. Am I missing a Namespace or include?

string ChooseDirectory(string szDescription, string szDirIn)

Subject:RE: Need a Simple User Interface
Reply by: sdigre
Date:1/13/2007 12:09:50 PM

I found the error. The following code works generating a string based on user selected directory.

// choose directory
string szDir = @"c:\"; // sets start directory for dialog box
szDir = SfHelpers.ChooseDirectory("Choose the first folder", szDir);

Subject:File Merger
Reply by: roblesinge
Date:7/5/2010 12:11:07 PM

Sorry to bump this old of a thread, but I'm attempting to put together a similar script to the one the OP posted about (i.e. a file merger, which I'm not sure why this isn't built into SF in the first place, but oh well). I need to paste several wavs into one master mp3 file.

A twist on this is that ideally, I'd like to be able to look at a folder of several hundred files and merge them 50 at a time into mp3s. So, the first 50 wavs would go into one mp3, then the next 50 and so on. This might be pushing it, so a simple file merger would be a starting point.

I've altered the "access all files in a folder" script to do a batch paste before (meaning, I had one file that I wanted pasted onto the beginning of all of the files in a folder). But this is a bit different.

BTW, be gentle. I'm teaching myself c# with Visual C# 2008 Express. It has been extremely helpful, and I've put together at least one Windows Form app. I understand the concepts but don't have a complete grasp of everything yet.

Can someone possibly point me to a concept I should investigate for pulling off the merger?

Thanks!
R.

Subject:RE: File Merger
Reply by: roblesinge
Date:7/5/2010 5:14:25 PM

I found a previous post that seems to address the merging part. "http://www.sonycreativesoftware.com/forums/ShowMessage.asp?ForumID=27&MessageID=451668". I'm going to work on altering it to accept all files in a folder, and then, if possible, try and make it work in chunks of 50 per mp3 file.

Here is the code from that post, JIC.



using System;

using System.IO;

using System.Windows.Forms;

using SoundForge;



public class EntryPoint {

public void Begin(IScriptableApp app) {



// the list of filenames to string together

//

string[] astrFilenames = {

@"c:\filename1.wav",

@"c:\filename2.wav",

@"c:\filename3.wav"

};



// open the first file (so we know what sample rate and channels to use)

//

string strFirstFile = astrFilenames[0];

ISfFileHost fileSrc = app.OpenFile(strFirstFile, true, true);

if (null == fileSrc)

{

DPF("Could not open {0}", strFirstFile);

return;

}



// create an output file in the same format as the first file.

//

ISfFileHost fileOut = app.NewFile(fileSrc.DataFormat, true);

if (null == fileOut)

{

DPF("Could not create output file");

}



// now open each file in the list and paste each onto the end of the output file

//

foreach (string strFilename in astrFilenames)

{

fileSrc = app.OpenFile(strFilename, true, true);

fileOut.OverwriteAudio(fileOut.Length, 0, fileSrc, new SfAudioSelection(fileSrc));

fileSrc.Close(CloseOptions.DiscardChanges);

}



// now ask the user where to save the output file.

//

fileOut.Save(SaveOptions.PromptIfNoFilename);



}



public void FromSoundForge(IScriptableApp app) {

ForgeApp = app; //execution begins here

app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));

Begin(app);

app.SetStatusText(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)); }

public static string GETARG(string k, string d) { string val = Script.Args.ValueOf(k); if (val == null || val.Length == 0) val = d; return val; }

public static int GETARG(string k, int d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsInt(k); }

public static bool GETARG(string k, bool d) { string s = Script.Args.ValueOf(k); if (s == null || s.Length == 0) return d; else return Script.Args.AsBool(k); }

} //EntryPoint



R.

Message last edited on7/5/2010 5:23:29 PM byroblesinge.

Go Back