# Script Program in a URCap

**URL:** <https://forum.universal-robots.com/t/script-program-in-a-urcap/6737>\
**Category:** URCaps SDK\
**Created:** [March 20, 2020, 9:54am UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737 "2020-03-20T09:54:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineeth1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/vineeth1/32/1774_2.png) [@vineeth1](https://forum.universal-robots.com/u/vineeth1)\
**Post date:** [March 20, 2020, 9:54am UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/1 "2020-03-20T09:54:46Z")

</div>

Hello everyone,

I am currently working developing my first URCap. So far it’s going good, but i have a question from my side.

I have written some script commands in a robot program which is saved in .urp file, but i would like to create a simple URCap which just executes the program in a single node, when i select the URCap node in the program. Also adding to this question, i would like to add user data which has to be done through installation node and then the script program uses the data from the installation node and executes the program.

I looked into the forum to get examples as such, but i could not find one. Could someone help me with such examples?

For eg:

Robot program  
xxxx  
xxxx  
xxx  
xxx

All the xxx are the script commands and i would save them in a script file. After i develop a URCap the robot program should look like

Robot program  
URCap Node. (And this URCap node executes all the xxxx commands).

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![anon65613222](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@anon65613222](https://forum.universal-robots.com/u/anon65613222)\
**Post date:** [March 20, 2020, 12:12pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/2 "2020-03-20T12:12:48Z")

</div>

What you seem to be trying to do is to add to the script to the program nodes [generateScript](https://plus.universal-robots.com/apidoc/63131/com/ur/urcap/api/contribution/ProgramNodeContribution.html#generateScript-com.ur.urcap.api.domain.script.ScriptWriter-) function?

---

<div class="post-metadata">

**Author:** ![vineeth1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/vineeth1/32/1774_2.png) [@vineeth1](https://forum.universal-robots.com/u/vineeth1)\
**Post date:** [March 20, 2020, 12:47pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/3 "2020-03-20T12:47:09Z")

</div>

No. I have a .script file with all my functions in it. I need to obtain the data from installation node and then this data must be used in .script file. After the data is obtained, in the program node, i need to run this .script file/function i created as a URCap node.

I looked into the example of the tutorial for adding script functions in generateScript function in java, but i cannot write all the commands in the generateScript function. So i would like to use it as a function command to call my .script file/function and then the program runs. I would like to receive some examples regarding this.

I hope this answers your question.

---

<div class="post-metadata">

**Author:** ![anon65613222](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@anon65613222](https://forum.universal-robots.com/u/anon65613222)\
**Post date:** [March 20, 2020, 3:52pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/4 "2020-03-20T15:52:04Z")

</div>

So you mean the scriptfile will be changed at runtime?

If so you could use a socket connection and from Java read each line and send one at a time to a socket that you listen to in generateScript.

Otherwise, anything that you can do in a scriptfile you can add with writer.appendLine(yourScriptLine), even things that aren’t in the ScriptManual, but is valid URScript language.  
Ofc if you use appendLine you need to be more careful with syntax etc.

---

<div class="post-metadata">

**Author:** ![anon65613222](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@anon65613222](https://forum.universal-robots.com/u/anon65613222)\
**Post date:** [March 20, 2020, 3:54pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/5 "2020-03-20T15:54:40Z")

</div>

Also, if you have all the script lines in your installation node contribution, you can add them there as a defined function, then in the program node just call that function you created

---

<div class="post-metadata">

**Author:** ![vineeth1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/vineeth1/32/1774_2.png) [@vineeth1](https://forum.universal-robots.com/u/vineeth1)\
**Post date:** [March 20, 2020, 4:06pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/6 "2020-03-20T16:06:09Z")

</div>

> [@anon65613222](#):
>
> So you mean the scriptfile will be changed at runtime?

No, the script file does not change in runtime.

Let me make this clear, I have a script file with  
def Program\_1():  
var\_1  
var\_2  
xxxxxx  
xxx  
xxx  
xxx  
etc.

I can do this program in a UR Program right now, but i would like to create a URCap node to make it easier. At the start of the program a user gives var\_1 and var\_2 input. And based on that input the program runs. So my idea of this URCap node is to put  
var\_1 and var\_2 in the installation contribution node, where user gives the input before the start of the program.  
def Program1(): in the program contribution node, where selection of the URCap node in the program view executes this program.

Ofcourse writing each line of my Program1 in the generateScript takes a lot of time. So i would like to know whether i can just call def Program1(): in the program contribution node and also can i append the var\_1 and var\_2 into this Program1() node?

Instead of writing all the lines of the Program1 in the generateScript is there a way i can call this function def Program1():? Is there any example for this?

---

<div class="post-metadata">

**Author:** ![anon65613222](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@anon65613222](https://forum.universal-robots.com/u/anon65613222)\
**Post date:** [March 20, 2020, 4:20pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/7 "2020-03-20T16:20:53Z")

</div>

I don’t know of any way of appending a variable not created by your URCap, but you can create variables in your scriptcode in generateScript to get user input.  
You don’t find them in the script manual, but I think the commands are request\_float\_from\_primary\_client(“message string”), request\_integer\_from\_primary\_client(“message string”), request\_boolean\_from\_primary\_client(“message string”) or request\_string\_from\_primary\_client(“message string”).

To be sure, you can create operator input variables in a URP, save it and read the .script file that has been created with your .urp file, then you can see exactly what the command is.

So your generateScript in the InstallationNode would be:

```
@Override
	public void generateScript(ScriptWriter writer) {
		writer.appendLine("def Program_1():");
		writer.appendLine("request_integer_from_primary_client("messageString")");
		// Rest of your code in writer.appendLine()
		writer.appendLine("end");
	}

```

and your programNode generateScript:

```
@Override
	public void generateScript(ScriptWriter writer) {
		writer.appendLine("Program_1()");

	}
```

---

<div class="post-metadata">

**Author:** ![vineeth1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/vineeth1/32/1774_2.png) [@vineeth1](https://forum.universal-robots.com/u/vineeth1)\
**Post date:** [March 20, 2020, 5:09pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/8 "2020-03-20T17:09:06Z")

</div>

Thank you for the reply. I will try this and get back if it works.

---

<div class="post-metadata">

**Author:** ![vineeth1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/vineeth1/32/1774_2.png) [@vineeth1](https://forum.universal-robots.com/u/vineeth1)\
**Post date:** [March 20, 2020, 5:09pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/9 "2020-03-20T17:09:54Z")

</div>

And one more question, where do i have to save my .script file when the code is executed? It must be saved somewhere right?

---

<div class="post-metadata">

**Author:** ![anon65613222](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@anon65613222](https://forum.universal-robots.com/u/anon65613222)\
**Post date:** [March 20, 2020, 5:21pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/10 "2020-03-20T17:21:24Z")

</div>

Same folder as the URP is saved, so in the on a real robot on /programs/  
On a simulator, ursim-current/programs.UR10/ or which simulator model you’re running.

---

<div class="post-metadata">

**Author:** ![vineeth1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/vineeth1/32/1774_2.png) [@vineeth1](https://forum.universal-robots.com/u/vineeth1)\
**Post date:** [March 20, 2020, 5:24pm UTC](https://forum.universal-robots.com/t/script-program-in-a-urcap/6737/11 "2020-03-20T17:24:36Z")

</div>

I will try this. Thank you.
