0

I have the following code that I want to run each time a Arcmap is open:

Windows 7 ArcMap 10.3.1

I have followed the instructions here: http://resources.arcgis.com/en/help/main/10.1/index.html#//014p00000018000000

It's suppose to run on an OpenDocument event. However the code does not run. If I copy paste into the python window in Arcmap it works fine, any ideas?

import arcpy
import pythonaddins
import getpass

class OnOpen(object):
    """Implementation for RunOnOpen_addin.OnStart (Extension)"""
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True
        OnOpen.openDocument(self)
    def openDocument(self):
        mxd = arcpy.mapping.MapDocument("CURRENT")
        text_elements = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")
        username = getpass.getuser()
        initial = username[0]
        lastname = username[1:]
        name = initial.title() + ". " + lastname.title()

        for element in text_elements:
            if element.name == 'Producer':
                element.text = "Producer: " + name

OnOpen()
  • Did you create the add-in and install it? – Branco Aug 7 '15 at 17:37
  • @Branco Yes, created the add-in and installed it. – Tristan Forward Aug 7 '15 at 17:38
  • Ok, was hoping it was an easier thing. I am not familiar with executing scripts on load but I'll check it out and see what may be missing or weird about the tutorial. – Branco Aug 7 '15 at 17:40
1

I played around with this a little bit and could not get the openDocument or newDocument events to fire. I was able to get the startup event to fire and tested by creating a text file. I am on ArcMap 10.3.1 and Win7 x64.

Here is my code:

import arcpy
import pythonaddins

class Py2(object):
    """Implementation for ArcGISAddIns_addin.extension2 (Extension)"""
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True
    def startup(self):
        open('C:\\Test.txt', 'w')

Instead of creating a text file, I tried editing the MXD properties. It seemed to be hit or miss sometimes. I got it to work once but couldn't repeat it. There may be a chance that the MapDocument instance isn't fully initialized by then? You could try to cross-post this to GeoNet to get an expanded user base to answer. If it comes down to it, contact ESRI support to see if they could be of further assistance.

  • Can you try adding in a delay? For example maybe not everything is actually loaded when the open document event is fired. Delay the code by 15 secs and see if works that way. I'll attempt as well. Also did the startup function run without an explicit startup() run call? – Tristan Forward Aug 7 '15 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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