Community Forums Archive

Go Back

Subject:TJ's Tricks: Drag Current File To Vegas
Posted by: _TJ
Date:5/20/2005 1:42:23 AM

Over in the general Sound Forge forum, Angel suggested that we should be able to drag a file from Sound Forge and drop it on Vegas. This seemed like a good idea, and It occurred to me that this could probably be done with a script.

So I decided to try it, and it works. The result is a script that will save the
current file if necessary, and then show a form with the filename and a cancel button. You can grab the filename and drop it on Vegas, just like you would grab a filename from the Windows' Explorer.

search for lbl.DoDragDrop() to find the code that does the key bit.
search for filehost.Save if you want to modify the code to save to a different filename or folder.

------------ snip and save as DragToVegas.cs ------------

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

public class EntryPoint {

public void Begin(IScriptableApp app)
{
if (null == app.CurrentFile)
return;
DragToVegasForm form = new DragToVegasForm(app.CurrentFile);
form.ShowDialog(app.Win32Window);
}

public class DragToVegasForm : System.Windows.Forms.Form
{
protected ISfFileHost filehost = null;
protected Label lblFile = null;
protected ProgressBar barProg = null;

public DragToVegasForm (ISfFileHost file)
{
Size sForm = new Size(420, 100);

filehost = file;

this.Text = "Drag the filename and drop it on Vegas";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.ClientSize = sForm;
this.Activated += new EventHandler(this.OnActivated);
this.Closing += new CancelEventHandler(this.OnClosing);

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

Label lbl = new Label();
lbl.Text = file.Filename;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Width = sForm.Width - pt.X - sOff.Width;
lbl.Height = sForm.Height - pt.Y - 36;
lbl.Location = pt;
lbl.MouseDown += new MouseEventHandler(Drag_MouseDown);
lbl.MouseUp += new MouseEventHandler(Drag_MouseUp);
lbl.MouseMove += new MouseEventHandler(Drag_MouseMove);
this.Controls.Add(lbl);
pt.Y += lbl.Height;

lblFile = lbl;

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

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

if (true && file.IsModified)
{
pt.X = sOff.Width;

ProgressBar bar = new ProgressBar();
bar.Location = pt;
bar.Height = btn.Height;
bar.Width = btn.Location.X - pt.X - 10;
this.Controls.Add(bar);
barProg = bar;
}
}

private bool fSaving = false;
private bool fShouldCancel = false;

private void OnActivated(object sender, System.EventArgs e)
{
if ( ! fSaving && filehost.IsModified)
{
lblFile.Text = "Saving: " + filehost.Window.Title;
this.Text = "Please wait for the file to be saved.";

fSaving = true;
filehost.Save(SaveOptions.PromptIfNoFilename);
filehost.WaitWithProgress(new SfProgressCallback(this.OnProgress_Save));

this.Controls.Remove(barProg);
barProg = null;
lblFile.Text = filehost.Filename;
this.Text = "Drag the filename and drop it on Vegas";

fSaving = false;
}
}

private int OnProgress_Save(double dPercentDone, string strTask)
{
if (null != barProg)
{
barProg.Value = (int)Math.Round(dPercentDone);
if (strTask != null && strTask != "")
lblFile.Text = strTask;
}

if (fShouldCancel)
return -1;
return 0;
}

// generic Cancel button click (sets dialog result and dismisses the form)
private void OnCancel_Click(object sender, System.EventArgs e)
{
if (fSaving)
{
fShouldCancel = true;
return;
}
this.DialogResult = DialogResult.Cancel;
this.Close();
}

private void OnClosing(object sender, CancelEventArgs e)
{
if (fSaving)
e.Cancel = true;
}

private static Point ptDown = new Point();
private static MouseButtons eDown = MouseButtons.None;
private void Drag_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (filehost.IsModified || fSaving || fShouldCancel)
return;

if (eDown == MouseButtons.None &&
(e.Button == MouseButtons.Left || e.Button == MouseButtons.Right))
{
eDown = e.Button;
ptDown.X = e.X;
ptDown.Y = e.Y;
}
}

private void Drag_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Label lbl = (Label)sender;
if (eDown != MouseButtons.None)
{
if (e.X < ptDown.X-4 || e.X > ptDown.X+4 || e.Y < ptDown.Y-4 || e.Y > ptDown.Y+4)
{
eDown = MouseButtons.None;

string[] files = new string[1] {lbl.Text};
DataObject dobj = new DataObject(DataFormats.FileDrop, files);
DragDropEffects op = lbl.DoDragDrop(dobj, DragDropEffects.Copy);
this.Close(); // close the form after the drag finishes.
}
}
}

private void Drag_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (eDown == e.Button)
eDown = MouseButtons.None;
}
}

public static IScriptableApp ForgeApp = null;
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));
}

} //EntryPoint


---------------- end of script ------------

tj

Message last edited on11/8/2007 9:55:28 PM by_TJ.
Subject:RE: TJ's Tricks: Drag Current File To Vegas
Reply by: Angels
Date:5/22/2005 10:12:54 AM


Thank you TJ! This is just fantastic!

I'm going to try customizing it to generate a file name (random or incremental) at a default location, so that the save will be invisible. That way I can skip the save part.

On reflection, it should also be possible to auto create a copy, auto save it, allow the drag and drop transfer, and close the file in Forge. That way the original file in Forge can keep it's edit history, and I can undo, modify and re-transfer.

After clearing unused files, I always close my Vegas projects with "copy all media" to make sure all project files are in one location .

I think you may have created a monster ;)

More thanks in the Forge forum...

Angel

Subject:RE: TJ's Tricks: Drag Current File To Vegas
Reply by: _TJ
Date:5/22/2005 4:08:14 PM

Sounds good. Be aware that if you use file.SaveAs(), you get the same behavior as Save As on the File menu. (i.e. the default save name and path is changed as well).

Use file.RenderAs() to save all (or part) of the file to a new file without changing the default save name and file type.

tj

Subject:RE: TJ's Tricks: Drag Current File To Vegas
Reply by: Ben 
Date:11/8/2007 7:44:15 PM

Edit: oops! Please ignore! I re-pasted the script and it's working fine now - not sure what I did wrong.

Resurrecting an old thread...

I can't get this script to work with Sound Forge 9c. On running the script I get the following error:

System.ApplicationException: 10 compiler errors.
at SoundForge.ScriptHost.CodeDomScriptManager.Compile(String szFile, String szSourceText)
at SoundForge.ScriptHost.RunScript(String szFile, String szSourceText, EngineType eType, Boolean fCompileOnly, Boolean fDebug)

Any ideas what's going wrong?

Thanks!

Ben

Message last edited on11/8/2007 7:47:45 PM byBen .

Go Back