Community Forums Archive

Go Back

Subject:New to Scripting, simple question
Posted by: realhelp
Date:3/22/2011 11:52:17 PM

Hi --

I'm trying to create a script that simply inserts a preset amount of silence at the cursor position of the currently selected open file. I have come up with the following code, which works, except it always inserts the silence at the BEGINNING of the file, not at the current cursor position. What am I missing?

Here's the code I'm using:

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

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

app.CurrentFile.DoEffect("Insert Silence",
"20ms at Cursor", // this is an existing preset.
app.ActiveWindow.EditRegion,
EffectOptions.EffectOnly);
app.CurrentFile.WaitForDoneOrCancel();
}

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;
} //EntryPoint

Subject:RE: New to Scripting, simple question
Reply by: roblesinge
Date:3/28/2011 7:05:43 AM

I'm not sure why you wouldn't just use the "Insert Silence" functionality inside SF, but there is a way to script this.

in your script you try to affect "app.ActiveWindow.EditRegion," which is the entire file. Thus the Insert Silence effect will happen at the beginning of the active file.

Try replacing your code with this:



{
long curPos = app.ActiveWindow.Cursor;
long silLength = app.CurrentFile.SecondsToPosition(.020);
app.CurrentFile.InsertSilence(curPos, silLength);

}


That should work. Obviously to alter the amt of silence, you change the SecondsToPosition amount. If you want it variable, you'll have to get a value from the user and convert that string to a number and then that number into a sample value.

Rob.

Subject:RE: New to Scripting, simple question
Reply by: realhelp
Date:3/28/2011 10:27:30 PM

Thanks, Rob. To answer your question, the reason is because we use SF for voiceover editing and we use specific presets of the Insert Silence command (and other commands as well) so often that we need to assign hotkeys to them. Since SF 10 does not allow keys to be assigned to individual presets, but does allow keys to be assigned to scripts, the only way we can have hotkeys assigned (without resorting to external hotkey programs) is to create a script for each Insert Silence preset we need, so I can then assign it to a hotkey.

It's a pain, and it's a kludge at best, but without this we are still using SF 8.0 and would have no way to switch to SF10 (in SF 8, you could select presets by pressing a unique first letter in the preset name, but in SF10 the preset name field is editable so typing in that field types a character instead of selecting from the list).

Hopefully the SF developers will allow key commands to map to individual presets soon, but in the meantime using scripting seems to be the only way to do this.


Subject:RE: New to Scripting, simple question
Reply by: realhelp
Date:3/28/2011 10:47:44 PM


Hey Rob --

I've discovered one slight problem with your solution above - once it completes, it zooms the file out to its farthest zoom setting. I assume I need to execute some sort of a method like "app.CurrentFile.ZoomLevel('1:4');" or something like that, but I can't find any documentation. What do I need to get back to the zoom level that I was at previously before executing this code?

Subject:RE: New to Scripting, simple question
Reply by: roblesinge
Date:3/29/2011 5:58:27 AM

I know what you're describing. This was a big complaint that I had with SF10 initially. I too use SF for voiceover editing. It was baffling to me how they removed that dropdown menu functionality that seems inherent in Windows. However, I got around it rather simply and now don't even miss it. For Insert Silence functions, I just use my usual shortcut to get to the dialog and then type in the silence I want with the number pad. The focus of that dialog box is on the length of silence you want. I typically always want the silence to go to the cursor position (as opposed to the front or back of the file). So, ALT + P + I then for this particular silence, you would just type .020 on your numpad and hit ENTER. This way, any amount of silence is literally at your fingertips.

Of course, I've long used my own hotkey device for all of my SF editing. I repurposed a Belkin Nostromo N50 (now an N52) gaming pad for editing. It allows you to map the keys to other keys or macros of keys. Speeds my editing up probably three-fold in some instances.

I'll see if I can figure out how to keep the script from zooming out on you. However, I still think using the number pad might be quicker for now.

Rob.

Subject:RE: New to Scripting, simple question
Reply by: roblesinge
Date:3/29/2011 6:16:25 AM

This should fix the zooming problem:


{
long curPos = app.ActiveWindow.Cursor;
long silLength = app.CurrentFile.SecondsToPosition(.020);
app.CurrentFile.InsertSilence(curPos, silLength);
app.ActiveWindow.ZoomToShow(app.ActiveWindow.ViewStart, app.ActiveWindow.ViewLength);
}


You mentioned not having documentation. There is an SDK available, in a sticky on this forum. It should help answer most of your questions about SF scripting.

Rob.

Subject:RE: New to Scripting, simple question
Reply by: realhelp
Date:3/31/2011 10:00:30 PM


Thanks, Rob. I'll give this new code a try a bit later tonight. As for typing in the values, that's not going to work as well in our case, because I hire entry-level editors to do the sound editing work. It's a lot easier to have a few presets assigned to hotkeys and teach them how to use those than to get into a lot of detail about how many milliseconds need to be inserted here or there.

As for the SDK, I downloaded it a couple of weeks ago, but the documentation there is extremely scant. I saw classes, etc., but nothing like the commands you are using are in there. Where do I find those?

Subject:RE: New to Scripting, simple question
Reply by: roblesinge
Date:4/4/2011 5:53:32 AM

I use the the "ForgeAPI.chm" file mostly. It's a help file that lets you search through available methods. I also know a tiny bit about programming in general, including a bit about C#, which helps. Searching this forum isn't a bad way to go either, especially if you come across a method you aren't quite sure how to use.

Rob.

Go Back