Community Forums Archive

Go Back

Subject:Using form with buttons for SAVE
Posted by: SonicTools
Date:8/24/2012 8:47:29 PM

Hi.

I seek to craft a script and need help, please. I have no code/scripting training, but have tooled the code below from other people's examples. Amazingly to me, it works. (No doubt there is some refinement possible). The script simply saves a file in a predetermined format/template - but provides a form with an input for filename. It eliminates a hundred mouseclicks at every session of my Sound Forge work!!

What I would like MUCH MORE is: a form with several buttons replacing the single form "OK" button, each of which would add a bit of text to the file's saved name, and then proceed just as the "OK" button would.

Specificly; I get my dialouge form, input a file name, and then press one of three buttons to confirm and proceed with the save job - but button
#1 would add "(a)" to the end of the filename,
#2 would add "(b)" to the end of the filename,
#3 would add "(c)" to the end of the filename.

I must have the added text on every file name I create to indicate critical information, so that would be just brilliant. One script and one click to streamline all file saving!

Anybody offer any help??


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, "need a UNIQUE file name!", out sInput);
DPF("GetInput returns {0} value = '{1}'", res.ToString(), sInput);

if (DialogResult.OK != res)
return;
string szType = ".flac";
object vPreset = "44,100 Hz, 24 Bit, Stereo";
string szDir = @"c:\SOUNDFORGE";
string szDir2 = @"c:\SOUNDFORGE\safetynet"; //this folder is to help with dup filenames, as I dont knowhow to add serialized digits to the name

ISfFileHost file = app.CurrentFile;
if (null == file)
{
app.SetStatusText("this script didnt know what file to use and has STOPPED.");
return;
}

Directory.CreateDirectory(szDir);

ISfRenderer rend = null;
if (szType.StartsWith("."))
rend = app.FindRenderer(null, szType);
else
rend = app.FindRenderer(szType, null);

if (null == rend)
{
app.SetStatusText("this script doesnt know what file type to save as. Script has stopped.");
DPF("renderer not found.", szType);
return;
}

try {
int iPreset = int.Parse((string)vPreset);
vPreset = iPreset;
} catch (FormatException) {}

ISfGenericPreset template = null;
if ((string)vPreset != "")
template = rend.GetTemplate(vPreset);
else
template = rend.ChooseTemplate((IntPtr)null, vPreset);
if (null == template)
{
app.SetStatusText("script doesnt understand what saveas TEMPLATE to use. Stopped.");
return;
}

string szBase = file.Window.Title;

string szName = String.Format(sInput+szType, szBase, rend.Extension);
szName = SfHelpers.CleanForFilename(szName);


string szFullName = Path.Combine(szDir, szName);
if (File.Exists(szFullName)) //this used to DELETE a dup file, so tried this if-else.
{
string szFullName2 = Path.Combine(szDir2, szName);
file.RenderAs(szFullName2, rend.Guid, template, null, RenderOptions.AndClose);
}
else
file.RenderAs(szFullName, rend.Guid, template, null, RenderOptions.AndClose);


SfStatus status = app.WaitForDoneOrCancel();
DPF("seen this yet?", status);
}


public DialogResult GetInput(IScriptableApp app, string sPrompt, out string sInput)
{
IWin32Window hOwner = app.Win32Window;
Form dlg = new Form();
Size sForm = new Size(820, 350);
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 = 48;
lbl.Location = pt;
dlg.Controls.Add(lbl);
pt.Y += lbl.Height;
TextBox edt = new TextBox();
edt.Size = sForm - new Size(150, 350);
edt.Location = pt;
dlg.Controls.Add(edt);
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 + 50);
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 + 50);
DialogResult res = dlg.ShowDialog(app.Win32Window);
sInput = dlg.Tag as string;
return res;
}

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

Go Back