Jump to content

My First ScriptMaster Module


This topic is 3612 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I'm interested in learning how to create a ScriptMaster module from scratch and have read the documentation at http://360works.com/plugins/SCRIPTMASTERPLUGIN/documentation.html and ran through the tutorial at http://groovy.codehaus.org/Beginners+Tutorial.  360Works.com told me what I already know about the usefulness of ScriptMaster and how to use existing modules, codehaus.org gave me a (very)basic understanding of groovy code.

 

I'm looking for pointers on how to write a simple ScriptMaster module from scratch, any do's and dont's and tips on taking sample java code and editing it to make a groovy ScriptMaster module.  I learned by trial and error, for example, that a "println" in groovy will not give a result in Scriptmaster, instead replace "println" with "return" and get the expected result.  I can guess why that is with ScriptMaster rather than an IDE like Eclipse but would love not to guess.  Are there any tutorials published that deal with using Groovy with ScriptMaster specifically where I could follow along?

 

Ultimately I'm looking to create modules that will allow me to query web services that, almost all, require oauth before being able to use a REST API.  A good example that my google searches keep finding is to simply send a tweet.  That said for right now a simple walkthrough of how to create a ScriptModule from scratch, preferably one that uses a jar file is what I'm looking for but have been unable to find.

 

Thanks,

 

Danny

Link to comment
Share on other sites

its a small market so no one has (to my knowledge) done that.... Maybe you could document your experience?

 

Google (or other search engines are available) is you friend though-out along with answers onStackOverflow. And start by understanding that you will have to learn a new language.

 

Trying to learn what each of the 360Works sample functions does is a great place to start, with the tutorials from the Groovy website.

Then it is a case of logging away simple lessons one at a time, and trying little bits of snippets of code to se what they do. Then look over all the posts in this forum where people have shared code. There are a couple of useful eBooks too - (google Groovy+book) which help a lot at a philosophical level. SO understanding terminology like 'method' is important if you do not come from a Java background (like me) helps too.

Then you will discover sites like RoseIndia which has lots of helpful stuff, and some articles buried deep on the IBM website. Then you will realise it is slowly taking over your life!!!

 

The most difficult skill to master is how to interpret someone else's APi document into functions you can use, and expect weeks of pain peppered with moments of elation but more times of being close but one crucial step away from the answer.

 

So tip #1

Just because the answer presented to you appears to be text, does not mean it is. It might still be an object, that you can manipulate further. Get in the habit of doing >>   return answer.class or answer.getClass() to see what you are dealing with.

Link to comment
Share on other sites

Thanks for taking the time to reply in detail.

 

I had actually considered documenting my experience and will certainly do that once I'm confident I have information that will be helpful to others in my position.

 

Google has given me lots of information on Groovy in the context of Grails and web development which is not making it easy for me to figure out how that could apply to ScriptMaster.  I've run though a few tutorials, what is confusing me is when i learn that unlike Java, in Groovy "return" isn't needed, yet Groovy in ScriptMaster requires you to "return" a result.

 

With Java there is plenty of sample code that is pretty easy for a novice to study and figure out what makes it work and how to manipulate it to suit a need.  I haven't had as much success in finding usable examples of Groovy code and where I have and it runs as expected and gives result in GroovyConsole, it does not run in ScriptMaster.

 

At the moment I'm having a better time understanding how to get what I need from LinkedIn and Twitter's API than i am learning how Groovy will help me send those commands to the API.

Link to comment
Share on other sites

Well not strictly accurate, but surely the point of a function it to take some inputs, process them, and return a result, Not actually that different from a FileMaker function. Do something and return an answer. Works in the console too.

 

At the very least returning true if your function worked or false if it didn't would be helpful wouldn't it?

 

Consider the following example

def a = 4
def b = 2

divi = a/b
mult = a*b

return divi

This will give you back 2 of course, but you could also set a variable immediately to SMGetVariable("divi") which will also grab the result 8 as it is held in memory till another function is run. (The same is also true for the Groovy Console btw.)

ANY object you create along the way is available to grab this way, so you can for example simply return an array of data (lets just say grabbed from a WebService) but at the same time return the number of values that are contained. Now clearly you can do ValueCount() on the result, but as this is an array you also grab a second variable which is the the number of columns in the data for example.

 

Another example

def this = 1
def that = 2

mult = this * that

//return mult

divi = that / this

divi

In the simple example you do not even need the explicit 'return' at the end, but its good practise, as if you uncomment the middle return it will stop at that point and return the mult value of 2

 

 

If you really want some advice stay away from workbench till you have learned how to measure things first. What do I mean by that? The point about what Groovy stands for is an elegant way to write simpler code which will run under a JVM. To get knee deep into REST you need to first understand JSON and XML building and parsing or slurping, but before that you need to have a thorough grasp of arrays, maps and lists among other things, which require a thorough grasp of looping and casting.

I would have said it took at least two years to become used to this great language and only another two years later on do I feel confident enough to say I know enough to be fluent. Along the way I have learned more than most people need to know about PDF creation and manipulation and developed really fast methods for creation and reading of XML which are abstracted from the source inputs though. 

 

Tip #2

Double characters in a test

== means equals    ( = means make the thing on the left to be the result of the thing on the right )

!= means not equals   ( but also !variable is equivalent to variable == null )

&& means and     ( + means add numbers, but also 'text' + 'text' will result in texttext, 2 + 'text' will not throw an error but will assume you want to return 2text by automatically casting the 2 to a string, 2i + 2f i.e. adding an integer to a float will give you 4.0 which is the most logical answer in the circumstances - equally sum(2,2) will give you 4 as there are often more than one way of doing things in Groovy)

|| means or

Link to comment
Share on other sites

  • 1 year later...

A year has passed and I figured I'd post an update to my original post to help any others new to programming that might attempt to create their own scriptmaster functions.

 

I have learned that many of my initial problems were due to a couple of Filemaker/Scriptmaster limitations that are important to understand:

 

Filemaker is a 32bit Application and requires a 32bit Java Virtual Machine, this means Java 1.6.  Any jars must be compiled using JDK 1.6.

 

Scriptmaster uses version 2.0.6 of Groovy, so code that works in a recent download of groovyConsole may not work  and may need some edits in Scriptmaster.

 

I dont understand why, but "println" does not return a result in Scriptmaster and it does in groovyConsole but replacing it with "return" does the trick.

 

I have made a function that creates a perceptual hash of two images and compares them for similarity, the code is below.  It may be ugly, but it works.  I'm no programmer.

RegisterGroovy( "getPHash( imageurl1 ; imageurl2 )" ; "import com.aquariusinteractive.util.ImagePHash¶
        ¶
ImagePHash p = new ImagePHash()¶
¶
String image1 = p.getHash(new URL(imageurl1).openStream())¶
String image2 = p.getHash(new URL(imageurl2).openStream())¶
¶
return (image1 + "n" + image2 + "n" +p.distance(image1, image2))" )

and 

RegisterGroovy( "generatePHash( imageurl )" ; "import com.aquariusinteractive.util.ImagePHash¶
        ¶
ImagePHash p = new ImagePHash()¶
¶
String image1 = p.getHash(new URL(imageurl).openStream())¶
¶
return (image1)" )

and

RegisterGroovy( "comparePHash( imageurl1 ; imageurl2 )" ; "import com.aquariusinteractive.util.ImagePHash¶
        ¶
ImagePHash p = new ImagePHash()¶
¶
String image1 = p.getHash(new URL(imageurl1).openStream())¶
String image2 = p.getHash(new URL(imageurl2).openStream())¶
¶
return (p.distance(image1, image2))" )

You'll need  a 32bit jar of https://github.com/mdbishop/ImagePHash

Link to comment
Share on other sites

println sends something to an output device (can be then directed to screen or printer for example) so you can use Groovy console to do repeated println statements but the purpose in the plug-in is to do some calculation with inputs and return a result to you which can then be used in a FileMaker context... so having multiple things returned will not really help... which one will you want to use?? Instead each object you create can be accessed after using the SMGetVariable()

You don't actually need an explicit return either, although I would recommend it for best practise

 

Also Groovy does automatic type casting so you can remove some of the excess (thats exactly why Groovy is groovy) and if you use the single speech marks here it is easier to understand is FMP does not need to escape them...

 

like

RegisterGroovy( "getPHash( imageurl1 ; imageurl2 )" ; "import com.aquariusinteractive.util.ImagePHash¶
¶
p = new ImagePHash()¶
¶
image1 = p.getHash(new URL(imageurl1).openStream())¶
image2 = p.getHash(new URL(imageurl2).openStream())¶
¶
return (image1 + 'n' + image2 + 'n' + p.distance(image1, image2))" )
Link to comment
Share on other sites

Thanks for the tips.

 

I hadn't considered SMGetVariable(), I was thinking that i'd return both hashes and the value of difference in a value list, then use Filemaker to GetValue from the result.  Those single quotes will sure make life easier too.

 

I should also have mentioned if not already obvious that I made 3 separate functions so that I can generate hashes as I go and compare later.

Link to comment
Share on other sites

I got that

 

I would create a single function with a third parameter to choose between compare and get hash, coping with an empty second file parameter...

conceptual outline

 

image 1 = hash

if imageurl2  == null return image 1

 

image 2 = hash

if param = 1

return distance image 1, image 2

else

return

image 1 + image 2  + distance image 1, image 2

Link to comment
Share on other sites

This topic is 3612 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.