Script to insert formated text in a node

Script to insert formated text in a node

Postby pilominco » Sun Feb 02, 2014 7:50 pm

I want to be able to insert "[·some text·]" at the cursor position of the node i am editing. I use that a lot for coments about the text I am writing. I know there is JEditorPane and JTextPane. I can do some scripting but i don't get to understand how those tools work. There is a script to toggle two characters. The problem is that script bypasses all formatting of the text. ¿How can I modify that script to insert formated text? Something similar is certainly in the source code, because Ctrl-e changes the selected text to blue, so I know the developers have already been there. Also a serious tool to work with text, like freeplane, should have this kind of auto text iplemented. Has anybody seen this issue in the forum? Nobody has had the same problem? Thanks.
Pretty different script from what i want, but the closest I have found to my problem:


def focusOwner = KeyboardFocusManager.currentKeyboardFocusManager.focusOwner
JEditorPane editor = focusOwner
// next, find the cursor position and save it in a variable
cursor = editor.caretPosition - 1
// Get a copy of the node text with all the html markup stripped out
def oldstr = htmlUtils.htmlToPlain(editor.text)
// Make a new string with the letters around the cursor changed
def newstr = oldstr.substring(0, cursor-1) + oldstr[cursor] + oldstr[cursor-1] + oldstr.substring(cursor+1)
// And write it back into the node
editor.text = newstr
// finally, put the cursor back where it was
editor.caretPosition = cursor + 1
pilominco
 
Posts: 32
Joined: Tue Jan 10, 2012 8:21 am

Re: Script to insert formated text in a node

Postby boercher » Mon Feb 03, 2014 1:52 am

Have a look at the editGoodies add-on. Unfortunately it's not on the add-on wiki page but here:

viewtopic.php?f=1&t=99

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Script to insert formated text in a node

Postby pilominco » Mon Feb 03, 2014 10:05 am

Thank you for the sugestion, Volker, but I can't open that add on. I get a warning about bugs in it, and it fails to load. But, most importantly, those scripts work with plain text, not with formated text. In fact, the script i posted in my first mesage comes from that addon: It is about transposing two characters, which is one of the things the "edit" addon does. I need something different. By the way, do you know if there is any web site of freelance developpers where i can hire someone to do such a tiny job, maybe just a dozen lines?
pilominco
 
Posts: 32
Joined: Tue Jan 10, 2012 8:21 am

Re: Script to insert formated text in a node

Postby boercher » Mon Feb 03, 2014 11:31 pm

What do you mean by "formatted text"? Do you mean the SimpyHTML editor? Unfortunately it's not possible to execute scripts there. You could try to extend SimplyHTML for that though.

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Script to insert formated text in a node

Postby pilominco » Mon Feb 03, 2014 11:45 pm

No, i don't mean SimplyHTML. Just inline editing. You can introduce colored text there, and bold and italic. It is just that I want to do that with a script. The only similar script I found uses JEditorPane but removes all html formating. I wonder how I can attach a JTextPane to the node I am editing and maybe that is the right way to do it. I just want to press a keyboard shortcut and get [·some text·] added at the cursor position. I am not a developer. I can do some scripting because i am a heavy user of computers and i really need Freeplane. But I would never be able to extend SimplyHTML. Waaaay beyond my reach.
pilominco
 
Posts: 32
Joined: Tue Jan 10, 2012 8:21 am

Re: Script to insert formated text in a node

Postby boercher » Tue Feb 04, 2014 12:01 am

I have thought about that when editgoodies came out but it's obviously more work to find the right cursor position if it's no plain text. You could get an impression of the work if you change a script to write the text content to the log. I guess that you have to do some HTML parsing (which is supported in scripting by basic java means).

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Script to insert formated text in a node

Postby pilominco » Tue Feb 04, 2014 12:26 am

Volker:
Thanks for your awareness. I have been trying to do it. But all the info I got about Java html parsing seems to go through JTextPane. In the script you have in EditGoodies they use JEditorPane. And I can't transform it into JTextPane. Copying some hava I could be able to do html parsing, but I don't know how to connect those tools to the Freeplane node. It seems tricky. Should be easy for you guys. For me it is impossible. I give up. If I don't get anyone interested I will have to quit. Either that, or hire a couple of ours from somebody. But who? Is it just too tiny a project to hire someone on line? Where would I do that? Maybe it would be good to have a small pool of freelance developers ready to solve some users problems. Could be useful for the project. But I don't know. I can't find the solution by myself. Thanks again, though.
pilominco
 
Posts: 32
Joined: Tue Jan 10, 2012 8:21 am

Re: Script to insert formated text in a node

Postby pilominco » Wed Feb 05, 2014 9:42 am

Finally, i got it solved! The autotext function is useful, for example, to insert sort of a markup system to be read by humans. It is like comments inserted in the main text. It could also be useful for automating many other things.
The problem is you need color, or other formatting. In Freeplane, that means html. Now, the method node.text is protected for consistency. It is totally stubborn in many different ways. It escapes html tags when you try to pass them literally into the text of the node. You can insert plain text in an unformatted node. But not if you use formatted one. One solution would be to bypass the interface and go directly to the text variable that holds text in the node class. But it is not recommend. And without the caret method to know where you want to insert, you don't know the right position to insert into because of a lot of hidden html.
So, the solution was using the java class “robot”. Now it works perfectly. The triky part were special characters, preciselly the [ ] I use to mark my comments. Java virtual key presses depend on the keyboard one is using. Spanish in my case. The workaround for this is a reminder of our beginnings with computers. :)) If anyone is interested in this small script, just tell me.
Last edited by pilominco on Sun Feb 09, 2014 11:23 am, edited 1 time in total.
pilominco
 
Posts: 32
Joined: Tue Jan 10, 2012 8:21 am

Re: Script to insert formated text in a node

Postby boercher » Sun Feb 09, 2014 1:09 am

Could you post your script here?

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Script to insert formated text in a node

Postby pilominco » Sun Feb 09, 2014 11:17 am

Of course I can. But it is so simple that I am a bit ashamed. No big deal. The trick was to use the old “Alt+Number keyboard” combination to get the special symbols “[ ]”. The rest is just the java robot. For me it is very useful though. By the way, some of the imports may not be necessary, I didn't make the effort to test them. Also, the script has to be assigned to a keyboard shortcut if it has to be of any use. If you don't do that the keystrokes you save with the script would be wasted with the effort of launching it through the menu. Anyway, here it goes:
Code: Select all
import java.awt.FlowLayout;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

        Robot r = new Robot()
      
      r.keyPress(KeyEvent.VK_CONTROL)
      r.keyPress(KeyEvent.VK_E)
      r.keyRelease(KeyEvent.VK_E)
      r.keyRelease(KeyEvent.VK_CONTROL)
      
      r.keyPress(KeyEvent.VK_ALT)
      r.keyPress(KeyEvent.VK_NUMPAD0)
      r.keyRelease(KeyEvent.VK_NUMPAD0)
      r.keyPress(KeyEvent.VK_NUMPAD9)
      r.keyRelease(KeyEvent.VK_NUMPAD9)
      r.keyPress(KeyEvent.VK_NUMPAD1)
      r.keyRelease(KeyEvent.VK_NUMPAD1)
      r.keyRelease(KeyEvent.VK_ALT)
      
      r.keyPress(KeyEvent.VK_ALT)
      r.keyPress(KeyEvent.VK_NUMPAD0)
      r.keyRelease(KeyEvent.VK_NUMPAD0)
      r.keyPress(KeyEvent.VK_NUMPAD9)
      r.keyRelease(KeyEvent.VK_NUMPAD9)
      r.keyPress(KeyEvent.VK_NUMPAD3)
      r.keyRelease(KeyEvent.VK_NUMPAD3)      
      r.keyRelease(KeyEvent.VK_ALT)
   
      r.keyPress(KeyEvent.VK_CONTROL)
      r.keyPress(KeyEvent.VK_K)
      r.keyRelease(KeyEvent.VK_K)
      r.keyRelease(KeyEvent.VK_CONTROL)
      
      r.keyPress(KeyEvent.VK_SPACE)

      r.keyPress(KeyEvent.VK_LEFT);
      r.keyRelease(KeyEvent.VK_LEFT);
      
      r.keyPress(KeyEvent.VK_LEFT);
      r.keyRelease(KeyEvent.VK_LEFT);
pilominco
 
Posts: 32
Joined: Tue Jan 10, 2012 8:21 am


Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron