[SOLVED] Saving File by script whitout ask user

Hello everyone, I have this code:

I want to save the file whitout asking to user, just save it directly when a tag change

table =event.source.parent.getComponent("tblEstadisticasPedidos")
table2=event.source.parent.getComponent("tblEstadisticasLineas")
tag='[default]app_tags/Componentes/ArranquePedidos/Counter_Ready'

from com.inductiveautomation.ignition.common import BasicDataset
dataset= BasicDataset(table.data)
dataset2= BasicDataset(table2.data)
dataset.setColumnNames(["Fecha","Confirmacion","No. ","No. 2","No. 3"])
dataset2.setColumnNames(["No. ","Nombre ","Codigo "])

if tag.value==10:
     sp= system.dataset.dataSetToExcel(1,[dataset,dataset2])
     filePath = system.file.writeFile('C:\\Users\\username\\Desktop\\document.xls',sp)

You will need to use system.tag.readblocking to get the tag value. Otherwise, I don't see anything else in your code that would prevent a file from being created without a user prompt. Here is your code, modified in a way that would work:

table =event.source.parent.getComponent("tblEstadisticasPedidos")
table2=event.source.parent.getComponent("tblEstadisticasLineas")
tagPath ='[default]app_tags/Componentes/ArranquePedidos/Counter_Ready'
tag = system.tag.readBlocking(tagPath)

from com.inductiveautomation.ignition.common import BasicDataset
dataset= BasicDataset(table.data)
dataset2= BasicDataset(table2.data)
dataset.setColumnNames(["Fecha","Confirmacion","No. ","No. 2","No. 3"])
dataset2.setColumnNames(["No. ","Nombre ","Codigo "])

if tag[0].value==10:
	sp = system.dataset.dataSetToExcel(True,[dataset,dataset2])
	filepath = system.file.writeFile('C:/Users/username/Desktop/document.xls', sp)

Edit: It occurs to me that the other part of this question could be where to fire the script from. Since this is relying on data from components within a client window, then the tag value would probably need to be bound to some component in the window itself. Then, a propertyChange event handler could be used to get this done. Here is an example with the tag value being bound to the Value (integer) property of a numeric text field:

if event.propertyName == 'value':
	table =event.source.parent.getComponent("tblEstadisticasPedidos")
	table2=event.source.parent.getComponent("tblEstadisticasLineas")
	tagValue = event.source.intValue
	
	from com.inductiveautomation.ignition.common import BasicDataset
	dataset= BasicDataset(table.data)
	dataset2= BasicDataset(table2.data)
	dataset.setColumnNames(["Fecha","Confirmacion","No. ","No. 2","No. 3"])
	dataset2.setColumnNames(["No. ","Nombre ","Codigo "])
	
	if tagValue == 10:
		sp = system.dataset.dataSetToExcel(True,[dataset,dataset2])
		filepath = system.file.writeFile('C:/Users/username/Desktop/document.xls', sp)

With this approach, it isn't necessary to read the tag, but rather just the component property itself.

The other thing that is perhaps missing from the code is a way to dynamically obtain the correct path to the current user's desktop. I typically do this with getPass.

Simply add this to the beginning of you code:

import getpass
username = getpass.getuser()

Then, change your filepath to this:

filepath = 'C:/Users/' + username + '/Desktop/document.xls'
system.file.writeFile(filepath, sp)
1 Like

Hi, thank you so much, this worked for me and thank you for your time

1 Like