« [News] Servoy World 2005 US Tour | Main | [Tip] PopupMenu Plugin example code »
October 24, 2005
[Tip] Update Servoy data using AppleScript
by Jan Aleman
Servoy
Every time when we show Servoy at new customers they will ask the wildest questions:
- Can it print to a printer?
- Can I use barcodes?
- Can I pull pages from the web?
- Can it make coffee too?
- etc.
The answer is obviously always yes, and the smart customers then ask: but how much work is it?
My default answer is very little after which the even smarter customers challenge me to actually show that. In this particular case a customer wants to update data from within AppleScript. In graphical oriented companies this is commonly used to link data to publishing tools such as Quark and InDesign to automate creation of documents. As usual there are many ways to solve this in Servoy and here's one way to do it. I can already feel the comments below this article appearing that will show an even easier way to do it. In my case it took me 2 minutes of asking questions to some applescript aware people (thanks David), about 4 minutes of coding, 3 minutes of testing and 2 minutes of bugfixing. The example is pretty hardcoded; it can only change data in one table but it is quite simple to take this example and make it work on any table, if you do so you might want to implement some security in it as well. Obviously you can also use the example to insert data, delete data and perform other actions (send email, generate pdf, make coffee, etc)
In my approach I use curl to retrieve a URL from Servoy headless client and pass it the arguments through the URL.
The solution consists of three bits:
1. A servoy solution with one form, one method. The method receives the primary key of the record you wish to update and the new value, it then searches for that record and updates it, if all ok it returns OK otherwise an error.
var v_pk = arguments[0]
var v_val = arguments[1]
controller.find()
applescriptdemoid = v_pk
controller.search()
if(controller.getMaxRecordIndex()==1)
{
thevalue = v_val
controller.saveData()
//the method returns OK, you can test in your applescript for this
return "OK"
}
else
{
return "error, record not found"
}
2. A JSP page, put this page in server/webapps/ROOT, the JSP page receives the parameters and calls the Servoy method that is described above.
< @ page import = "java.util.*" >
< @ page import = "com.servoy.j2db.server.headlessclient.*" >
< @ page import = "com.servoy.j2db.dataprocessing.IDataSet" >
< @ page errorPage="errorpage.jsp" >
<
ISessionBean servoy_hc = (ISessionBean)application.getAttribute("servoyapplescript");
if (servoy_hc == null)
{
servoy_hc = HeadlessClientFactory.createSessionBean(request,"applescriptdemo");
application.setAttribute("servoyapplescript",servoy_hc);
}
boolean ok = servoy_hc.setMainForm("applescriptdemo");
String pkid = request.getParameter("pkid");
String val = request.getParameter("val");
>
<
String result = "";
if(null!=pkid && null != val)
{
result= (String)servoy_hc.executeMethod(null,"changeValue",new Object[]{pkid,val});
}
else
{
result = "pkid and val must be specified";
}
out.println(result);
>
3. The actual applescript that retrieves the URL, this is the bit you would stick into your existing workflow applescripts, some error handling is recommended.
set x to do shell script "curl http://localhost:8080/applescript.jsp?pkid=1\\&val=test"
| Posted by David Workman on October 24, 2005 at 02:58 PM in Tips | Permalink