Advertisement
KosIvantsov

write_selection2list.groovy

Jun 22nd, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.93 KB | None | 0 0
  1. /*
  2.  * #Purpose: Write selection to a file to create a list of terms
  3.  * #Files:   Writes 'terms_list.txt' in the current project's root
  4.  *     the file contains selection text, segment number, segment text
  5.  *     and filename of the selection, if selection is in the current segment,
  6.  *     or just the text of selection and the filename, if selection
  7.  *     is outside the current segment.
  8.  * #Note:    When invoked without selection, it opens the file
  9.  *     in the default text editor
  10.  * #Details: http://wp.me/p3fHEs-4L
  11.  *
  12.  * @author   Kos Ivantsov
  13.  * @based on scripts by Yu Tang
  14.  * @date     2013-06-25
  15.  * @version  0.2
  16.  */
  17.  
  18. import static javax.swing.JOptionPane.*
  19. import static org.omegat.util.Platform.*
  20.  
  21. // abort if a project is not opened yet
  22. def prop = project.projectProperties
  23. if (!prop) {
  24.   final def title = 'Selection to List'
  25.   final def msg   = 'Please try again after you open a project.'
  26.   showMessageDialog null, msg, title, INFORMATION_MESSAGE
  27.   return
  28. }
  29. // get segment #, source filename and the whole current segment
  30. def srcfile = editor.currentFile
  31. def ste = editor.currentEntry
  32. cur_text = ste.getSrcText()
  33. cur_seg = ste.entryNum()
  34.  
  35. // define list file
  36.  
  37. def folder = prop.projectRoot
  38. def fileloc = folder+'/terms_list.txt'
  39. list_file = new File(fileloc)
  40.  
  41. // create file if it doesn't exist
  42. if (! list_file.exists()) {
  43.     list_file.write("",'UTF-8')
  44.     }
  45.  
  46. /*
  47.  * command to open the file if there's no active selection
  48.  * if a custom (not OS default) text editor should be used,
  49.  * it needs to be defined in the next line (edit as needed and uncomment)
  50.  */
  51.  
  52. // def textEditor = /path to your editor/
  53. def command
  54. switch (osType) {
  55.   case [OsType.WIN64, OsType.WIN32]:
  56.     command = "cmd /c start \"\" \"$list_file\""  // default
  57.     try { command = textEditor instanceof List ? [*textEditor, list_file] : "\"$textEditor\" \"$list_file\"" } catch (ignore) {}
  58.     break
  59.   case [OsType.MAC64, OsType.MAC32]:
  60.     command = ['open', list_file]  // default
  61.     try { command = textEditor instanceof List ? [*textEditor, list_file] : ['open', '-a', textEditor, list_file] } catch (ignore) {}
  62.     break
  63.   default:  // for Linux or others
  64.     command = ['xdg-open', list_file] // default
  65.     try { command = textEditor instanceof List ? [*textEditor, list_file] : [textEditor, list_file] } catch (ignore) {}
  66.     break
  67. }
  68.  
  69. def sel_txt = editor.selectedText
  70. if (sel_txt) {
  71.     list_file.append "${'='*10}\n $sel_txt\n",'UTF-8'
  72.     if (cur_text =~ sel_txt) {
  73.         list_file.append "${'-'*5}\n\
  74. filename: $srcfile\n\
  75. segment: $cur_seg\n\
  76. segment text: $cur_text \n\n",'UTF-8'
  77.     }else{
  78.         list_file.append "${'-'*5}\n\
  79. filename: $srcfile\n\
  80. ***Selection outside of current segment***\n",'UTF-8'
  81.     }
  82.     console.println "\"$sel_txt\" written to $list_file"
  83. } else {
  84. console.println "[No selection]"
  85. console.println "***Opening the file in text editor***"
  86. console.println "Command: $command"
  87. command.execute()
  88. return // exit
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement