User-contributed example scripts: Difference between revisions

From FreeMind
Jump to navigationJump to search
No edit summary
No edit summary
Line 96: Line 96:
   def logger = c.getFrame().getLogger(this.getClass().getName());
   def logger = c.getFrame().getLogger(this.getClass().getName());
   logger.info(" ******* Logger set up for script");
   logger.info(" ******* Logger set up for script");
</groovy>
== Export to bibtex ==
This is an attempt to make a script that takes a mindmap formatted in a certain way and produces a bibtex file out of each node (each node has the correct fields to be parsed as a bibtex entry).
I have a problem, the script exits from the main switch(attr['Type']), after calling any print*() function. The function is executed, the file first entry is written but after the call I have a "java.lang.NullPointerException: Cannot invoke method call() on null object" error.
I'm a groovy programmer with 5hours experience, so I might be missing something easy. Can somebody help to make this script work?
<groovy>
import freemind.modes.MindMapNode;
def attrMap = { node, f ->
    def attrs = [:];
    for(i in 0..<node.getAttributes().getRowCount()) {
        attrs[node.getAttributes().getName(i)] = node.getAttributes().getValue(i);
    };
    return attrs;
};
def printConference (f, attr, index){
f << "@InProceedings{ ${index.toString()},\n";
attr.each{
f << "switch";
if(it.key != null){
switch(it.key){
case 'Authors': f << "author = \"${it.value}\",\n";
break
case 'Title': f << "title = \"${it.value}\",\n";
break
case 'Conference': f << "booktitle = \"${it.value}\",\n";
break
case 'Year': f << "year = \"${it.value}\",\n";
break
case 'URL': f <<  "URL = \"${it.value}\",\n";
break
default:
break
}
}
}
f << '}\n';
};
def printJournal (f, attr, index) {
f << "@Article{ ${index.toString()},\n";
attr.each{
switch(it.key){
case 'Authors': f << "author = \"${it.value}\",\n";
break
case 'Title': f << "title = \"${it.value}\",\n";
break
case 'Journal': f << "journal = \"${it.value}\",\n";
break
case 'Year': f << "year = \"${it.value}\",\n";
break
case 'URL': f <<  "URL = \"${it.value}\",\n";
break
default:
break
}
}
f << '}\n';
};
def printStandard (f, attr, index){
f << "@STANDARD{ ${index.toString()},\n";
attr.each{
switch(it.key){
case 'Authors': f << "author = \"${it.value}\",\n";
break
case 'Title': f << "title = \"${it.value}\",\n";
break
case 'Institution': f << "institution = \"${it.value}\",\n";
break
case 'Year': f << "year = \"${it.value}\",\n";
break
default:
break
}
}
f << '}\n';
};
def printWeblink (f, attr, index) {
f << "@online{ ${index.toString()},\n";
attr.each{
switch(it.key){
case 'Authors': f << "author = \"${it.value}\",\n";
break
case 'URL': f <<  "URL = \"${it.value}\",\n";
break
default:
break
}
}
f << '}\n';
};
def allChildren (node, f) {
    def kids = [];
    def stack = new Stack();
    stack.push(node.childrenUnfolded());
    while(!stack.isEmpty()) {
        nodes = stack.pop();
        while(nodes.hasNext()) {
k = nodes.next();
            kids.add(k);
stack.push(k.childrenUnfolded());
        };
    };
kids.each{
};
    return kids;
};
f = new File("/tmp/File.txt");
f.delete();
index = 1;
allChildren(node, f).each { kid ->
attr  = attrMap(kid, f);
switch(attr['Type']){
case 'Journalpaper':
printJournal(f, attr, index);
index = index + 1;
break
case 'Standard':
printStandard(f, attr, index);
index = index + 1;
break
case 'Weblink':
printWeblink(f, attr, index);
index = index + 1;
break
case 'Confpaper':
printConference(f, attr, index);
index = index + 1;
break
default:
break
}
}
</groovy>
</groovy>

Revision as of 09:54, 4 October 2007

A node with the current time

If you need a node that shows the last saving time, just add the following script and start it once each time, you are opening the map for editing.

<groovy> import freemind.modes.MindMapNode; import freemind.modes.mindmapmode.MindMapController; import java.util.Timer;

class DateTimer extends java.util.TimerTask { MindMapNode mNode; MindMapController mC; DateTimer(node, c) { mNode = node; mC = c; }

void run() { mNode.setText("Save Time: " + new java.util.Date()); mC.nodeRefresh(mNode); } }

t = new Timer(); t.schedule(new DateTimer(node, c),2000,2000); </groovy>

WARNING! I have now played with Groovy for 30 minutes and I am unsure of the side effects to FreeMind of using these examples, as they modify children of a node. - Dave Torok

Prepend the modified date at the beginning of the node text

This is an example of using some more Node methods.

<groovy>=(new java.text.SimpleDateFormat("M/d/yyyy")).format(node.getHistoryInformation().getLastModifiedAt())

  + " " + node.getText()</groovy>

Add up all subnodes and set them into the first row, second column.

Copy the script into subnodes if you want go more than one level. As far as I can tell you can't grab the values by attribute name, just by row / column, but I didn't spend forever looking over the source.. (Maybe someone could push some javadoc?)


<groovy> def i = node.childrenUnfolded(); def total=0; while (i.hasNext()) { d = i.next(); def val=d.getAttributes().getValueAt(0,1); if (val!=null) total=total+java.lang.Integer.parseInt(val); }; node.getAttributes().setValueAt(total, 0,1);</groovy>

The above code has a problem that it will let leaf node and its parent nodes with the script to yield 0 value. The following code would work recursively.

<groovy> if (!node.isLeaf()) {def i = node.childrenUnfolded(); def total=0; while (i.hasNext()) { d = i.next(); def val=d.getAttributes().getValueAt(0,1); if (val!=null) total=total+java.lang.Integer.parseInt(val); }; node.getAttributes().setValueAt(total, 0,1);};</groovy>

--yushen 15:35, 15 Jan 2007 (PST)



Set the color for all children

This is an example of iteration over child nodes. Also, a call to c.nodeStructureChanged(node) is necessary to force a refresh to paint the new color.

<groovy> def i = node.childrenUnfolded(); while (i.hasNext()) { d = i.next(); d.setColor(java.awt.Color.BLUE); }; c.nodeStructureChanged();</groovy>

Swap two CHILD nodes

This works. My goal is to get a foundation for a SORT script, to sort children by alphabet, by created date, etc. until the functionality is added to the software. WARNING WARNING WARNING this is just an example to swap the 2nd and 4th child nodes.

<groovy>def swap (a,b) {

   na = node.getChildAt(a);
   nb = node.getChildAt(b);
   node.remove(a); 
   node.remove(b - 1); 
   node.insert(nb,a);
   node.insert(na,b);
   c.nodeStructureChanged(node);

};

swap(1,3);</groovy>

Add/Replace today

<groovy> =nt=node.toString(); pos=nt.indexOf(" / "); return ((pos>=0)?nt.substring(0,pos):nt) + " / "

  + (new java.text.SimpleDateFormat("yyyy-M-d")).format(new Date())

</groovy>

Set up a logger

The following code, set up at the beginning of your script, will set up a logger that you can use to follow the progress of your script while you are working on it. You can find details on how to use the Java Logger at http://java.sun.com/javase/6/docs/api/

Be sure to read section 1.8 on how to set up your configuration file. If you don't get anything out of the logger, that's one of the likely sources of trouble.

<groovy>

 def logger = c.getFrame().getLogger(this.getClass().getName());
 logger.info(" ******* Logger set up for script");

</groovy>


Export to bibtex

This is an attempt to make a script that takes a mindmap formatted in a certain way and produces a bibtex file out of each node (each node has the correct fields to be parsed as a bibtex entry). I have a problem, the script exits from the main switch(attr['Type']), after calling any print*() function. The function is executed, the file first entry is written but after the call I have a "java.lang.NullPointerException: Cannot invoke method call() on null object" error. I'm a groovy programmer with 5hours experience, so I might be missing something easy. Can somebody help to make this script work?

<groovy> import freemind.modes.MindMapNode;

def attrMap = { node, f ->

   def attrs = [:];
   for(i in 0..<node.getAttributes().getRowCount()) {
       attrs[node.getAttributes().getName(i)] = node.getAttributes().getValue(i);
   };
   return attrs;

};

def printConference (f, attr, index){ f << "@InProceedings{ ${index.toString()},\n"; attr.each{ f << "switch"; if(it.key != null){ switch(it.key){ case 'Authors': f << "author = \"${it.value}\",\n"; break case 'Title': f << "title = \"${it.value}\",\n"; break case 'Conference': f << "booktitle = \"${it.value}\",\n"; break case 'Year': f << "year = \"${it.value}\",\n"; break case 'URL': f << "URL = \"${it.value}\",\n"; break default: break } } } f << '}\n'; };

def printJournal (f, attr, index) { f << "@Article{ ${index.toString()},\n"; attr.each{ switch(it.key){ case 'Authors': f << "author = \"${it.value}\",\n"; break case 'Title': f << "title = \"${it.value}\",\n"; break case 'Journal': f << "journal = \"${it.value}\",\n"; break case 'Year': f << "year = \"${it.value}\",\n"; break case 'URL': f << "URL = \"${it.value}\",\n"; break default: break } }

f << '}\n'; };

def printStandard (f, attr, index){ f << "@STANDARD{ ${index.toString()},\n"; attr.each{ switch(it.key){ case 'Authors': f << "author = \"${it.value}\",\n"; break case 'Title': f << "title = \"${it.value}\",\n"; break case 'Institution': f << "institution = \"${it.value}\",\n"; break case 'Year': f << "year = \"${it.value}\",\n"; break default: break } }

f << '}\n';

}; def printWeblink (f, attr, index) { f << "@online{ ${index.toString()},\n"; attr.each{ switch(it.key){ case 'Authors': f << "author = \"${it.value}\",\n"; break case 'URL': f << "URL = \"${it.value}\",\n"; break default: break } }

f << '}\n'; };


def allChildren (node, f) {

   def kids = [];
   def stack = new Stack();
   stack.push(node.childrenUnfolded());
   while(!stack.isEmpty()) {
       nodes = stack.pop();
       while(nodes.hasNext()) {

k = nodes.next();

           kids.add(k);

stack.push(k.childrenUnfolded());

       };
   };

kids.each{ };

   return kids;

};


f = new File("/tmp/File.txt"); f.delete(); index = 1;


allChildren(node, f).each { kid -> attr = attrMap(kid, f); switch(attr['Type']){ case 'Journalpaper': printJournal(f, attr, index); index = index + 1; break case 'Standard': printStandard(f, attr, index); index = index + 1; break case 'Weblink': printWeblink(f, attr, index); index = index + 1; break case 'Confpaper': printConference(f, attr, index); index = index + 1; break default: break } }


</groovy>