Menu

Get node ID

Kai
2018-05-30
2021-05-23
  • Kai

    Kai - 2018-05-30

    Dear Freemind Forum.
    I would like to link OneNote and Freemind and therefore plan to refer to Freemind nodes via their IDs. Is there a way to copy the selected node's ID to the clipboard via shortcut or groovey script to achieve this? I tried node.id or node.getId() in Groovey but they do not work. The "manual" way of creating a local link to the target node, open the link window via Ctrl+K and copy the ID from their, is too time consuming. I would love to reduce this to one shortcut.
    Eventually I would then like to add the second step, which is by using a different shortcut/script to navigate to the node that is referred to by its ID in the clipboard. If anyone can help me to achieve this, that would be great.
    Many thanks
    Kai

     
  • Kai

    Kai - 2018-06-03

    Dear all,
    if anyone else has a similar problem, please find the following code as example. It works fine for me when added as groovey script file and integrated via shortcut (1) to Freemind to work in all scripts.

    /* Script by Kai B., 2018, provided as-is without warranty of any kind.
     * This groovy script was designed for use with Freemind 1.0.1.
     * The script is copying the last selected node's id to the clipboard or an
     * empty string on any error.
     */
    
    java.lang.String curId = "";
    freemind.modes.ModeController mc = null;
    freemind.modes.MindMapNode n = null;
    
    
    try {
        mc = c.getModeController();
    
        if (mc.getSelecteds().size() != 1)
        {
            curId = "<>1"
            // println("<>1 selected nodes"); // DEBUG
        }
        else
        {
            n = mc.getSelected();
            if (n!=null)
            {
                curId = mc.getNodeID(n);
            }
        }
    
    }catch(Exception ex){
        curId = "";
        // println("Exception caught"); // DEBUG
    }
    
    try
    {
        c.setClipboardContents(new java.awt.datatransfer.StringSelection(curId));
    } catch(Exception ex) {
        // println("Exception caught setting CB content"); // DEBUG
    }
    
    mc = null;
    

    (1): integrate via (description for Windows) for general acccess via shortcut Ctrl+Shift+h:
    copying the above code as freemind script copySelectedNodeId.groovy in the .freemind folder in your users directory.
    AND adding the following segment to the file ScriptingEngine.xml in the "Plugins" subfolder of your Freemind installation.

    <plugin_action 
            name="copySelectedNodeId" 
            documentation="Copy the selected node's ID to the clipboard." 
            label="plugins/copySelectedNodeId" 
            key_stroke="control shift H"
            base="freemind.extensions.ModeControllerHookAdapter" 
            class_name="plugins.script.ScriptingEngine">
            <plugin_mode class_name="freemind.modes.mindmapmode"/>
            <plugin_menu location="menu_bar/extras/first/scripting/copySelectedNodeId"/>
            <!--  change the following entry to your user name vvvvvvvvv   here! -->
            <plugin_property name="ScriptLocation" value="/Users/Kai/.freemind/freemind script copySelectedNodeId.groovy" />
        </plugin_action>
    

    Regards,
    Kai

     
    👍
    1
  • Kai

    Kai - 2018-06-03

    And this is step 2 for the initially described use case, also as groovey script to the best way I could find in the Freemind sources without being involved in the development project:

    File: "freemind script goToClipboardRefNodeId.groovy":

    /* Script by Kai B., 2018, provided as-is without warranty of any kind.
     * This groovy script was designed for use with Freemind 1.0.1.
     * The script is acquiring a node ID from the clipboard and than navigates
     * the focus to this id.
     * It takes no action on any error.
     */
    
    
    java.awt.datatransfer.Transferable t = null;
    freemind.modes.ModeController mc = null;
    freemind.modes.NodeAdapter na = null;
    freemind.view.mindmapview.NodeView nv = null;
    freemind.modes.MindMapNode n = null;
    
    try
    {
    
        t = c.getClipboardContents();
    
        if(!t.isDataFlavorSupported(java.awt.datatransfer.DataFlavor.stringFlavor))
        {
            t = null;
            return;
        }
    
        java.lang.String cbId = t.getTransferData(java.awt.datatransfer.DataFlavor.stringFlavor).toString();
        cbId = cbId.trim();
    
        if(!cbId.startsWith("ID_"))
            cbId = "ID_" + cbId;
    
        mc = c.getModeController();
        na = mc.getNodeFromID(cbId);
    
        // First of all unfold all parents:
        n = na; // NodeAdapter implements MindMapNode
        while ((n!=null) && (n.getNodeLevel() >= 2) && (n.hasFoldedParents()))
        {
            n = n.getParentNode();
            c.setFolded(n, false); // works better than n.setFolded(false);
        }
        n = null; // As n does no longer hold the reference to the node itself, kill it right away
    
    
        // Now get the viewer and select+focus the node:
        if(na.getViewers().size() > 0)
        {
            nv = na.getViewers().peek();
            //nv.requestFocus(); // does not seem to have many impact
            mc.select(nv);
            c.obtainFocusForSelected();
        }
        else
        {
            // println("No viewer found."); // DEBUG
            t = null;
            mc = null;
            na = null;
            nv = null;
            n = null;
            return;
        }
    }
    catch(Exception ex)
    {
        // println("Exception caught"); // DEBUG
    }
    
    t = null;
    mc = null;
    na = null;
    nv = null;
    n = null;
    

    and the according ScriptingEngine.xml lines for adding as shortcut Ctrl+Shift+j:

    <plugin_action 
            name="goToClipboardRefNodeId" 
            documentation="Select and focus the node referred to by its ID in the clipboard." 
            label="plugins/goToClipboardRefNodeId" 
            key_stroke="control shift J"
            base="freemind.extensions.ModeControllerHookAdapter" 
            class_name="plugins.script.ScriptingEngine">
            <plugin_mode class_name="freemind.modes.mindmapmode"/>
            <plugin_menu location="menu_bar/extras/first/scripting/goToClipboardRefNodeId"/>
            <!--  change the following entry to your user name vvvvvvvvv   here! -->
            <plugin_property name="ScriptLocation" value="/Users/Kai/.freemind/freemind script goToClipboardRefNodeId.groovy" />
        </plugin_action>
    
     
  • Rodrigo

    Rodrigo - 2020-06-10

    [Solved]

    I just removed the

    "/Users/-----/.freemind/'freemind script' goToClipboardRefNodeId.groovy"

    and it worked.

    So if anyone is having issues this might be the solution.


    Hi Kay,

    When I came across your post it was the solution that I was looking for, but unfortunetly it did not work for me.

    I am using version 1.0.1, and I followed all your steps. I can find the scripts inside of Freemind but the IDs don't get copied to my clipboard. could you help me out??

    Thanks :)

     

    Last edit: Rodrigo 2020-06-10
  • Kai

    Kai - 2020-09-05

    Hey Rodrigo, glad you figured it out. As long as the real file name is the same as how you refer to it in the XML file you are fine. If you omitted the 'freemind script' part in the filename, then you had to do the same as described.

     
  • Kai

    Kai - 2021-05-23

    Sometimes Java blocks the Clipboard interaction between Freemind and the rest of my Win10 OS. Therefore I have added this little modification as separate macro to enter the target node ID via a small input-box rather than the Clipboard. You can load this file the same way as described above as separte macro (and recommended to assign a different shortcut):

    /* Script by Kai B., 2021, provided as-is without warranty of any kind.
     * This groovy script was designed for use with Freemind 1.0.1.
     * The script is acquiring a node ID from an inputbox and than navigates
     * the focus to this id. The node ID may be pre-fixed with the filename + #.
     * It takes no action on any error.
     */
    
    
    freemind.modes.ModeController mc = null;
    freemind.modes.NodeAdapter na = null;
    freemind.view.mindmapview.NodeView nv = null;
    freemind.modes.MindMapNode n = null;
    freemind.view.mindmapview.MapView mv = null;
    
    try
    {
    
        java.lang.String cbId = javax.swing.JOptionPane.showInputDialog(null, "Please enter the Node ID:", "Kai-Freemind", javax.swing.JOptionPane.QUESTION_MESSAGE)
        cbId = cbId.trim();
        if((cbId == null) || (cbId == ""))
            return;
    
        // Get the filename of the map:
        java.lang.String curFn = "";
        try {
            curFn = c.getMindMapMapModel().toString()
        }catch(Exception ex){
            curFn = null;
        }
        // Check on link contains filename pre-fix:
        if (curFn != null)
            if (cbId.startsWith(curFn + "#"))
                cbId = cbId.substring(curFn.length() + 1);
    
        if(!cbId.startsWith("ID_"))
            cbId = "ID_" + cbId;
    
        mc = c.getModeController();
        mv = mc.getView();
        na = mc.getNodeFromID(cbId);
    
        // First of all unfold all parents:
        n = na; // NodeAdapter implements MindMapNode
        while ((n!=null) && (n.getNodeLevel() >= 2) && (n.hasFoldedParents()))
        {
            n = n.getParentNode();
            c.setFolded(n, false); // works better than n.setFolded(false);
        }
        n = null; // As n does no longer hold the reference to the node itself, kill it right away
    
    
        // Now get the viewer and select+focus the node:
        if(na.getViewers().size() > 0)
        {
            nv = na.getViewers().peek();
            //nv.requestFocus(); // does not seem to have many impact
            mc.select(nv);
            c.obtainFocusForSelected();
            mv.centerNode(nv); // optional - this command centers the jumped to node
        }
        else
        {
            // println("No viewer found."); // DEBUG
            mc = null;
            mv = null;
            na = null;
            nv = null;
            n = null;
            return;
        }
    }
    catch(Exception ex)
    {
        javax.swing.JOptionPane.showMessageDialog(null, "Error / Node-Id not found.");
    }
    
    mc = null;
    na = null;
    nv = null;
    mv = null;
    n = null;
    
     

Log in to post a comment.