# Calling a Project Script by name

**URL:** <https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658>\
**Category:** Ignition\
**Created:** [June 6, 2023, 6:42pm UTC](https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658 "2023-06-06T18:42:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djones](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@djones](https://forum.inductiveautomation.com/u/djones)\
**Post date:** [June 6, 2023, 6:42pm UTC](https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658/1 "2023-06-06T18:42:54Z")

</div>

Is there any way to call a project script by string name?

I have a bit of equipment simulation logic and I'd like to be able to select a routine. The routine is tied to a different project script, so the main simulation script can vary what logic it uses based on the routine selected.

something like:

inputParams = {"input1" :1, "input2":2}  
scripting.callByName("RoutineName.FunctionName", inputParams)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 6, 2023, 6:49pm UTC](https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658/2 "2023-06-06T18:49:50Z")

</div>

Getattr probably does what you want: [2. Built-in Functions — Python 2.7.18 documentation](https://docs.python.org/2.7/library/functions.html#getattr)

---

<div class="post-metadata">

**Author:** ![djones](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@djones](https://forum.inductiveautomation.com/u/djones)\
**Post date:** [June 7, 2023, 1:16pm UTC](https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658/3 "2023-06-07T13:16:28Z")

</div>

After looking at your info, I ended up with this and it seems to be working. I can now pass the name of the routine (script) and pass and return parameters.

I understand that each script would need a function called MainLogic and take the same inputs and return the same outputs. I am fine with this as each script is a different set of logic for a simulation. so all simulations take the same inputs and return the same outputs. This allows me to dynamically change the logic.

```auto
def CallRoutine (Routine,params,CurrentLens, CurrentLensID, ColorTable,RunID, CurrentTime, LocationData,TotalProcessedLenses,ScheduleData):
	
	z = globals()[Routine]
	CurrentLens,TotalProcessedLenses,ScheduleData = z.MainLogic(params,CurrentLens, CurrentLensID, ColorTable,RunID,CurrentTime, LocationData,TotalProcessedLenses,ScheduleData)

	return CurrentLens,TotalProcessedLenses,ScheduleData

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 7, 2023, 1:32pm UTC](https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658/4 "2023-06-07T13:32:50Z")

</div>

You could also use a mapping. This allows a 'finer' control over what can be called.

```python

func_map = {
    'foo': path.to.foo,
    'bar': path.to.bar,
    'wuz': path.to.wuz
}
func = func_map[Routine]
func(args)

```

Unless you have A LOT of different functions that could be called, I'd use a mapping.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 7, 2023, 2:05pm UTC](https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658/5 "2023-06-07T14:05:43Z")

</div>

> [@pascal.fragnoud](#):
>
> You could also use a mapping.

Why would you create this mapping when a mapping (`globals()`) is automatically made for you within the script?

And when not inside the script, accessing via string is simply:

```auto
getattr(script, functionName)(*args, **kwargs)

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [June 7, 2023, 2:13pm UTC](https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658/6 "2023-06-07T14:13:01Z")

</div>

To allow only predefined functions to be called like this.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 7, 2023, 2:23pm UTC](https://forum.inductiveautomation.com/t/calling-a-project-script-by-name/74658/7 "2023-06-07T14:23:49Z")

</div>

Meh. I would just make a script that only has allowed callables.
