Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a script that I want to run within ArcMap when a button in a toolbar is clicked, and so far I have only been able to make the script into a script tool. I need it to run as a command, not as a geoprocessing tool. Running it as a geoprocessing tool takes much longer than when the same code is executed from the command line window. I have just started looking into ArcObjects, but I want to get started using it if it is what I'll need to use. If anyone has any sample code, or resources for creating a button, that would be great.

share|improve this question
    
Are you trying to do this in ArcMap or in a ArcGIS Engine standalone application? –  MathiasWestin Sep 3 '10 at 12:59
    
I want to do this in ArcMap 10. –  Tanner Sep 3 '10 at 16:51
1  
the 9.3 version of this question is Arcmap: attach python script to button –  matt wilkie Oct 4 '11 at 22:12

3 Answers 3

up vote 7 down vote accepted

If you don't need any input or outputparameters, this sample should be possible to use to run a script in a custom command Leveraging ArcPy in a .NET application, C# example:

// Executes a shell command synchronously.
// Example of command parameter value is
// "python " + @"C:\scripts\geom_input.py".
//
public static void ExecuteCommand(object command)
{
    try
    {
        // Create the ProcessStartInfo using "cmd" as the program to be run,
        // and "/c " as the parameters.
        // "/c" tells cmd that you want it to execute the command that follows,
        // then exit.
        System.Diagnostics.ProcessStartInfo procStartInfo = new
            System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

        // The following commands are needed to redirect the standard output.
        // This means that it will be redirected to the Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;

        // Do not create the black window.
        procStartInfo.CreateNoWindow = true;

        // Now you create a process, assign its ProcessStartInfo, and start it.
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = procStartInfo;
        proc.Start();

        // Get the output into a string.
        string result = proc.StandardOutput.ReadToEnd();

        // Display the command output.
        Console.WriteLine(result);
    }
    catch (Exception objException)
    {
        Console.WriteLine(objException.Message);
        // Log the exception and errors.
    }
}
share|improve this answer
    
Thanks! This looks very promising. –  Tanner Sep 4 '10 at 10:02
1  
Also note that this is not python specific. It can be used to run any external command. –  matt wilkie Oct 4 '11 at 22:26
    
But did it work, @Tanner? –  Richard Apr 19 '12 at 17:54
    
To be honest I never got it to work, but back then my only programming experience was some limited Python. I feel like I could give it a fair shot now if I had the time and still needed to make a button. –  Tanner Apr 20 '12 at 21:40

Right click a blank area next to your toolbars > open the customize window > click on the commands tab > scroll to the bottom of the list and click [UI Control] > Select new UI Control > select the type of control that you want and then click Create and Edit. This will open the VBA editor for the new control and you can write your code inside hear and define the event that calls the code. VBA is included with 10 for free but you will need to request a license file for it and then register that license. Call ESRI for this and they should give you a license free of charge. After 10 this will go away...

share|improve this answer

Use [Add Tools...] in Toolbar / Customize to add the script to a category. Then pull the script to the toolbar of choice.

share|improve this answer
    
That's what I did. Using that method, the script runs as a script tool and brings up a geoprocessing window, making the script take much longer to execute. –  Tanner Sep 3 '10 at 19:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.