# Script Module Editor kills scripts added through module hook

**URL:** <https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723>\
**Category:** Module Development\
**Created:** [December 9, 2011, 9:06am UTC](https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723 "2011-12-09T09:06:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cgz](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@cgz](https://forum.inductiveautomation.com/u/cgz)\
**Post date:** [December 9, 2011, 9:06am UTC](https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723/1 "2011-12-09T09:06:25Z")

</div>

When exposing python scripts to a project through module’s hook like this:

```auto
    public static void exposeJythonBindings(ClientContext context) throws IOException
    {
        ScriptManager scriptMan = context.getScriptManager();
        String moduleCode = "# some python script here, I load each from a set of files bundled within the module's .jar";
            
        scriptMan.addScriptModule(moduleName, moduleCode);
    }
```

The problem is, every time I save changes in Script Module Editor of the Designer (editing scripts not exposed  
with the above mechanism), the above python scripts disappear and stop working. Only way to resume  
is by restarting the Designer.

Could you fix this?

Thanks!

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [December 9, 2011, 3:43pm UTC](https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723/2 "2011-12-09T15:43:46Z")

</div>

This isn’t the correct way to expose scripts.

What you want to do override #initializeScriptManager(ScriptManager) in the appropriate module hooks and add your script module every time this is called. The lifecycle of a ScriptManager is sometimes short-lived. Saving changes to your project is one way to restart the lifecycle, killing off the ScriptManager you added your scripts to and creating a new one.

Never hang on to the ScriptManager parameter of #initializeScriptManager(ScriptManager), just remember to add your scripts every time it gets called.

---

<div class="post-metadata">

**Author:** ![cgz](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@cgz](https://forum.inductiveautomation.com/u/cgz)\
**Post date:** [December 12, 2011, 7:13am UTC](https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723/3 "2011-12-12T07:13:40Z")

</div>

My designer hook’s #initializeScriptManager(ScriptManager) only executes once at startup, but  
not after project save like you suggested. The automatically added scripts still get dropped.

What am I missing?

---

<div class="post-metadata">

**Author:** ![cgz](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@cgz](https://forum.inductiveautomation.com/u/cgz)\
**Post date:** [December 15, 2011, 2:06pm UTC](https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723/4 "2011-12-15T14:06:26Z")

</div>

My conclusion is it’s broken, but I found out an alternative solution:

In designer hook:

```auto
    @Override
    public void notifyProjectSaveDone()
    {
        super.notifyProjectSaveDone();
        
        // reload scripts. the "initializeScriptManager" doesn't seem to work as defined
        ScriptManager scriptMan = AClassWhereIStoredTheContextObjectsAtStartup
                .getClientContext().getScriptManager();
        exposeJythonScripts(scriptMan);
    }
```

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [December 21, 2011, 11:26pm UTC](https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723/5 "2011-12-21T23:26:19Z")

</div>

What path are you adding them under? i.e. the 1st String argument to ScriptManager.addScriptModule

---

<div class="post-metadata">

**Author:** ![cgz](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@cgz](https://forum.inductiveautomation.com/u/cgz)\
**Post date:** [December 22, 2011, 7:43am UTC](https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723/6 "2011-12-22T07:43:30Z")

</div>

The scripts go to paths for example:

```auto
app.psc.project.SomeClass1.py
app.psc.project.SomeClass2.py
```

---

<div class="post-metadata">

**Author:** ![systemparadox](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@systemparadox](https://forum.inductiveautomation.com/u/systemparadox)\
**Post date:** [August 8, 2012, 11:07am UTC](https://forum.inductiveautomation.com/t/script-module-editor-kills-scripts-added-through-module-hook/3723/7 "2012-08-08T11:07:39Z")

</div>

notifyProjectSaveDone() doesn’t work on the client, but addProjectChangeListener() can be used instead.
