Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby kunkel321 » Mon Mar 10, 2014 4:52 pm

Hi Boercher (or other knowledgeable person),
You had previously helped me with the following working script
Code: Select all
def basedir  = "C:\\Users\\swkunkel\\MapTemp"
c.findAll().each {
  if (it.hasStyle('fileme')) {
    def title = it.plainText
    def note = it.note
    if (note) {
        def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
        def file = new File (basedir, filename)
        file.text = note.plain
    }
  }
}
//https://sourceforge.net/apps/phpbb/freeplane/viewtopic.php?f=1&t=421
//Basically Boercher wrote the whole thing...  :)

It saves my case notes to text files so I can sync them with my hand-held (a Palm back then, a Kindle FDH now). I have so many cases now though, that this is no longer a good solution. (I.e. having 100+ text files all in the same folder of my Kindle is overwhelming.)

My workflow in Freeplane is to have my cases categorized by completion-status, via having different parent nodes be the categories. Each child node is then a “case” with the case notes being the node notes.
What I’d like to do is have subfolders (dir paths) that correspond to the completion-status-category parent nodes. Please see image of map and file-manager
Image
I want Groovy to put the case notes in the corresponding folder on my PC, so I can then sync them to my Kindle…. Does that description make sense?
I tweaked the above code a bit, and it “seems” like it should work.
Code: Select all
/*
C:\Users\swkunkel\MapTemp\InProcess\
C:\Users\swkunkel\MapTemp\Completed\   
*/

def basedir  = "C:\\Users\\swkunkel\\MapTemp"
c.findAll().each {
 if (it.node.parent.text('In Process') {   //The node is a child of the "In Process" node.
    if (it.hasStyle('fileme')) {
      def basedir  = "C:\\Users\\swkunkel\\MapTemp\\InProcess"  //So put the TXT in this subfolder.
      def title = it.plainText
      def note = it.note
      if (note) {
          def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
          def file = new File (basedir, filename)
          file.text = note.plain
      }
    }
  }
 else if (it.node.parent.text('Completed') {   //The node is a child of the "Completed" node.
    if (it.hasStyle('fileme')) {
      def basedir  = "C:\\Users\\swkunkel\\MapTemp\\Completed" //So put the TXT in this subfolder instead.
      def title = it.plainText
      def note = it.note
      if (note) {
          def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
          def file = new File (basedir, filename)
          file.text = note.plain
      }
    }
  }
}

But it does not. I get the following error:
Image
I tried it with “If,” “Else,” and “Else If.” It says it’s expecting an open paren… Is that right?
Many thanks for any help you can offer.

EDIT: I found where the missing paren was... :oops:
Still getting errors though:
Image

I think maybe instead of completely reassigning the basedir variable inside the IF{ } statements, I need to append the subfolder name to it (?) IDK Will work on it more tomorrow. :D
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby kunkel321 » Tue Mar 11, 2014 6:19 pm

Update 3-11-2014:

I tried concatenating the "basedir" and a "subdir" to get a "wholedir."
Also I got rid of some of the redundant code by putting it after the if switches.
Code: Select all
//NOT WORKING CODE
def basedir  = "C:\\Users\\swkunkel\\MapTemp"
c.findAll().each {
 if (it.node.parent.text('In Process')) {
    if (it.hasStyle('fileme')) {
      def subdir  = "\\InProcess"
    }
  }
 else if (it.node.parent.text('Completed')) {
    if (it.hasStyle('fileme')) {
      def subdir  = "\\Completed"
   }
  }
 def title = it.plainText
      def note = it.note
      if (note) {
        def wholedir = basedir + subdir
          def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
          def file = new File (wholedir, filename)
          file.text = note.plain
      }
}
At any rate, I'm getting a different error message now.
Image
I saw a thing about "Slashy Strings" here http://groovy.codehaus.org/Strings+and+GString I tried that too, but to no avail.
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby kunkel321 » Wed Mar 12, 2014 8:24 pm

I'm getting closer! :lol:
I switched some more things around... Rather than trying to get the node.parent.core, I just will assign them their own Custom Styles in Freeplane. (Volker has already shown me how to evaluate styles correctly)
Code: Select all
def basedir
c.findAll().each {
 if (it.hasStyle('fileme')){
   if (it.parent.hasStyle('catInProcess')) {
         basedir  = "C:\\Users\\swkunkel\\MapTemp\\Inprocess"
    }
   else if (it.parent.hasStyle('catCompleted')) {
         basedir  = "C:\\Users\\swkunkel\\MapTemp\\Completed"
    }
  }
 def title = it.plainText
      def note = it.note
      if (note) {
        def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
          def file = new File (basedir, filename)
          file.text = note.plain
      }
}
It doesn't work correctly, but it does work!

Image
In the image, the "Completed" folder in my file manager should only have files for "Billy SMith" and "Jane Doe."
Will work on it more later.
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby jokro » Thu Mar 13, 2014 4:28 pm

1. it.node.parent should be it.parent: "it" represents the "node".
2. if (it.parent.text('In Process') is lacking a closing parenthesis: if (it.parent.text('In Process')).
The same is true for if (it.parent.text('Completed')

3. it.parent = null if it represents the root node. To prevent a null error, use:
if (it.parent!=null&&it.parent.text('In Process'))
if (it.parent!=null&&it.parent.text('Completed'))

Let know if it works now.

Regards
Jodi

Code: Select all
 def basedir  = "C:\\Users\\swkunkel\\MapTemp"
    c.findAll().each {
    if (it.parent!=null&&it.parent.text('In Process')) {   //The node is a child of the "In Process" node.
        if (it.hasStyle('fileme')) {
          basedir  = "C:\\Users\\swkunkel\\MapTemp\\InProcess"  //So put the TXT in this subfolder.
          def title = it.plainText
          def note = it.note
          if (note) {
              def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
              def file = new File (basedir, filename)
              file.text = note.plain
          }
        }
      }
    else if (it.parent!=null&&it.parent.text('Completed')) {   //The node is a child of the "Completed" node.
        if (it.hasStyle('fileme')) {
          basedir  = "C:\\Users\\swkunkel\\MapTemp\\Completed" //So put the TXT in this subfolder instead.
          def title = it.plainText
          def note = it.note
          if (note) {
              def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
              def file = new File (basedir, filename)
              file.text = note.plain
          }
        }
      }
    }
jokro
 
Posts: 486
Joined: Sun Jul 24, 2011 2:56 pm

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby kunkel321 » Thu Mar 13, 2014 5:12 pm

Hi Jodi, Thanks for the reply! :D

I'm still getting an error:
Image

Note that I changed the name of the one parent node to "InProcess" just to get rid of the white space...

Is it possible that .text() is not supposed to be used this way?
It seems like there should be an it.hasText() method similar to the it.hasStyle() method(?) I Googled "groovy hastext" and it doesn't appear to exist. I also thought FreePlane might have a .core() or .core.text(). I don't see such a thing in the ScriptingAPI map. "Core" is the correct terminology though I think... (see image).
Image

In one of my above posts I also changed the code so it looks at the it.parent.hasStyle--that way I don't even half to deal with the 'text' issue at all. I have to say though, looking for the text of the parent node would be easier.

Thoughts?
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby kunkel321 » Thu Mar 13, 2014 7:20 pm

Ha! I was rereading http://freeplane.sourceforge.net/wiki/i ... /Scripting
and noticed this line of code in the Filtering section:
Code: Select all
def matches = c.find{ it.text.contains('ok') }

Putting ".contains" in there did the trick :)
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby jokro » Thu Mar 13, 2014 8:17 pm

Could you add the code and a description to the wiki and/or develop it into an addon ?
Regards
Jodi
jokro
 
Posts: 486
Joined: Sun Jul 24, 2011 2:56 pm

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby kunkel321 » Thu Mar 13, 2014 9:14 pm

Yay! I think I have a final working script for what I need :lol:

Here is the actual map (with fake ppl of course)
Image
And the corresponding code:
Code: Select all
def basedir
c.findAll().each {
 if (it.hasStyle('Student-Boy') | it.hasStyle('Student-Girl')) {
      if (it.parent!=null&&it.parent.text.contains('cBIP')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\BIP"
      }
      else if (it.parent!=null&&it.parent.text.contains('cComplete')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Complete"
      }
      else if (it.parent!=null&&it.parent.text.contains('cEvaluations')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Evaluations"
      }
      else if (it.parent!=null&&it.parent.text.contains('cMisc')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Misc"
      }
      else if (it.parent!=null&&it.parent.text.contains('cReferrals')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Referrals"
      }
      else if (it.parent!=null&&it.parent.text.contains('cTransfers')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Transfers"
      }
      else if (it.parent!=null&&it.parent.text.contains('cUpcomming')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Upcomming"
      }
      else {
          basedir  = ""
      }
 def title = it.plainText
      def note = it.note
      if (note) {
        def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
          def file = new File (basedir, filename)
          file.text = note.plain
      }
    }
}


I don't know how to compile addons, but I'll post it to the 'Sample scripts' page of the wiki :D

Thanks again for your help!

EDIT 3-17-2014:
Okay I added a description to the wiki. Please see: http://freeplane.sourceforge.net/wiki/i ... Text_Files
If anyone sees this, and happens to have a suggestion to improve the code, please reply and I'll update the wiki page. :D
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby kunkel321 » Wed Mar 19, 2014 3:00 pm

Hi Again Jody, or Volker, or anybody...

After I had this script all "figured out" and posted it to the Sample Scripts Wiki, I am again getting an error! :oops: :evil: Argh!
Image

It appears to be telling me that there is an unexpected "=" in line one--but there is not.

It occured to me that I put the variable declaration at line one, though I don't even know if you need to do that with Groovy...
So then I commented-out the declaration. Here is the actual code:
Code: Select all
//def basedir
c.findAll().each {
 if (it.hasStyle('Student-Boy') | it.hasStyle('Student-Girl')) {
      if (it.parent!=null&&it.parent.text.contains('cBIP')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\BIP"
      }
      else if (it.parent!=null&&it.parent.text.contains('cComplete')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Complete"
      }
      else if (it.parent!=null&&it.parent.text.contains('cEvaluations')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Evaluations"
      }
      else if (it.parent!=null&&it.parent.text.contains('cMisc')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Misc"
      }
      else if (it.parent!=null&&it.parent.text.contains('cReferrals')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Referrals"
      }
      else if (it.parent!=null&&it.parent.text.contains('cTransfers')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Transfers"
      }
      else if (it.parent!=null&&it.parent.text.contains('cUpcomming')) { 
          basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Upcomming"
      }
      else {
          basedir  = ""
      }
 def title = it.plainText
      def note = it.note
      if (note) {
        def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
          def file = new File (basedir, filename)
          file.text = note.plain
      }
    }
}

I get the same error though...

Interestingly, the script *is* working (with or without the declaration). It puts the text files where it is supposed to.... So why the error? Does anyone know?

==========
EDIT On Wed, 03-19-2014 at 08:26am:
I just realized that yesterday I had imported styles from another map. This had overwritten the styles that were already in my map... The script looks for styles "Student-Boy" and "Student-Girl." They had been replaced with "student-boy" and "student-girl." Same spelling, but lowercase.... I'm not sure it that caused the problem or not (?) I have now gotten rid of the lowercase ones and put the proper case ones in there, but I'm still getting the error popup.
kunkel321
 
Posts: 131
Joined: Thu Aug 16, 2012 4:23 am

Re: Need Groovy help: If node.parent (‘X’) Save .TXT to C://X

Postby boercher » Thu Mar 20, 2014 12:00 am

A stray '=' may stem from a formula that has been converted to a "real" script ("line 1, column 1")

I tried your script. You probably should use logical '||' instead of bitwise '|' and you could save the 'if' but all in all it's fine (please note that it is note.plainText or note.to.plain instead of note.plain):
Code: Select all
c.find{ it.hasStyle('Student-Boy') || it.hasStyle('Student-Girl') }.each {
    if (it.parent!=null&&it.parent.text.contains('cBIP')) {
        basedir  = "C:\\Users\\swkunkel\\CaseNotes\\BIP"
    }
    else if (it.parent!=null&&it.parent.text.contains('cComplete')) {
        basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Complete"
    }
    else if (it.parent!=null&&it.parent.text.contains('cEvaluations')) {
        basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Evaluations"
    }
    else if (it.parent!=null&&it.parent.text.contains('cMisc')) {
        basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Misc"
    }
    else if (it.parent!=null&&it.parent.text.contains('cReferrals')) {
        basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Referrals"
    }
    else if (it.parent!=null&&it.parent.text.contains('cTransfers')) {
        basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Transfers"
    }
    else if (it.parent!=null&&it.parent.text.contains('cUpcomming')) {
        basedir  = "C:\\Users\\swkunkel\\CaseNotes\\Upcomming"
    }
    else {
        basedir  = "."
    }
    def title = it.plainText
    def note = it.note
    if (note) {
        def filename = title.substring(0, Math.min(30, title.length())) + '.txt'
        def file = new File (basedir, filename)
        file.text = note.plainText
    }
}

I'd propose that you decide on the directory by extracting it from parent.text, e.g. ''basedir = new File("C:/Users/swkunkel/CaseNotes", it.parent.text)''. (You may have to manipulate the text to make it work as a directory name.)

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

Next

Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron