Community Forums Archive

Go Back

Subject: Pasting Files from a textfile?
Posted by: Kit
Date: 3/21/2014 5:33:56 AM

The following code compiles but when I run it I get an error:

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

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

ISfFileHost openFile = app.CurrentFile;
ISfDataWnd wnd = app.ActiveWindow;
long currentPos = wnd.Cursor;
long startingPos = wnd.Cursor;

string Path = @"K:\_Settings\Sony Sound Forge\importList.txt";
if (File.Exists(Path))
{
string line;

// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader(Path);
while((line = file.ReadLine()) != null)
{
string strPaste = line;
//MessageBox.Show(strPaste,"File Names");
ISfFileHost fileToPasteFrom = app.OpenFile(strPaste, false, false);
fileToPasteFrom.WaitForDoneOrCancel();
fileToPasteFrom.Close(CloseOptions.DiscardChanges);
long mPos = fileToPasteFrom.Length;
openFile.ReplaceAudio(new SfAudioSelection(currentPos, 0), fileToPasteFrom, new SfAudioSelection(fileToPasteFrom));
currentPos = currentPos + mPos;
openFile.WaitForDoneOrCancel();
}
file.Close();

wnd.SetCursorAndScroll(startingPos, DataWndScrollTo.NoMove);
}
else
{
MessageBox.Show("No File Selected","Paste Aborted!");
}



}


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

} //EntryPoint


Below is the error. If I uncomment the message box it shows me the path of the files and I've checked that they exist. Can anyone tell me why I'm getting an error and how to fix this. Thanks.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at SoundForge.ISfFileHost.get_Length()
at EntryPoint.Begin(IScriptableApp app)
at EntryPoint.FromSoundForge(IScriptableApp app)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at SoundForge.ScriptHost.ScriptManager.Run(Assembly asm, String className, String methodName)
at SoundForge.ScriptHost.CodeDomScriptManager.Run(Assembly assy)
at SoundForge.ScriptHost.RunScript(String szFile, String szSourceText, EngineType eType, Boolean fCompileOnly, Boolean fDebug)

Message last edited on 3/21/2014 5:35:47 AM by Kit.
Subject: RE: Pasting Files from a textfile?
Reply by: roblesinge
Date: 3/21/2014 7:46:12 AM

You open the fileToPasteFrom but then immediately close it before doing anything with it. That WaitForDoneOrCancel() line can just be removed. Then move your Close() call to after the operations just before the end of the while loop. Otherwise, you're calling methods on an object that doesn't exist anymore.

Rob.

Subject: RE: Pasting Files from a textfile?
Reply by: Kit
Date: 3/21/2014 10:47:05 AM

Thanks very much, I had misunderstood what t WaitForDoneOrCancel() did. Your suggestions worked and the script now functions as it should.

Subject: Bundling Undos
Reply by: Kit
Date: 3/21/2014 9:13:04 PM

I wonder if you can help me with two more problems. The first is critical. I'm getting an error if there is no open file to paste into. I tried:

   if (null == openFile)
return;


Just before checking for the existence of the file containing the list to import. It compiled OK but still throws an error when there's no open file.

The second problem is whether it7s possible to create something like ActiveDocument.BeginCommandGroup in vba? The aim is to be able to undo all the pastes with one undo rather than multiple ones. Perhaps that is not posible with Sound Forge?

I hope you can help, especially with the first error. Thanks.

EDIT: Solved the first problem by moving the null code nearer the top of the file. Still wonder about bundling undos.

Message last edited on 3/21/2014 9:16:39 PM by Kit.
Subject: RE: Bundling Undos
Reply by: roblesinge
Date: 3/22/2014 12:47:33 AM

It's late here and I just saw this. I wish the forum would e-mail when a thread is replied to, but c'est la vie.

I'll get back to you in the morning (later in the morning) on the undos, I need to check out the documentation a bit before answering.

While we're waiting, I feel like I'm only seeing bits and pieces of the bigger picture of what you're trying to do with this script. Are you trying to paste a giant list of files into a specific spot in other files? Is it always the same list of files, or does that change?

Rob.

Subject: RE: Bundling Undos
Reply by: Kit
Date: 3/22/2014 1:00:39 AM

Thanks, I looked at the documentation in the SDK but didn't find anything I thought would do. I'll probably end up with several scripts for different specific purposes. It won't be a giant list of files and the list will change. I'll use it to make changes to existing files - a bit like making a jigsaw but reordering the positioning of the pieces.

Subject: RE: Bundling Undos
Reply by: roblesinge
Date: 3/25/2014 7:29:36 AM

Sorry for the delay. Take a look at BeginUndo() and EndUndo(). BeginUndo() allows you to set up a starting point for a multi-step undo. Part of the EndUndo call is a boolean switch that you can flip to discard all changes since the BeginUndo() call.

Rob.

Subject: RE: Bundling Undos
Reply by: Kit
Date: 3/28/2014 8:38:53 PM

Thanks, didn't notice this for a while. I found a thread that has a good example and got it working.

Go Back