Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * #Purpose: Write selection to a file to create a list of terms
- * #Files: Writes 'terms_list.txt' in the current project's root
- * the file contains selection text, segment number, segment text
- * and filename of the selection, if selection is in the current segment,
- * or just the text of selection and the filename, if selection
- * is outside the current segment.
- * #Note: When invoked without selection, it opens the file
- * in the default text editor
- * #Details: http://wp.me/p3fHEs-4L
- *
- * @author Kos Ivantsov
- * @based on scripts by Yu Tang
- * @date 2013-06-25
- * @version 0.2
- */
- import static javax.swing.JOptionPane.*
- import static org.omegat.util.Platform.*
- // abort if a project is not opened yet
- def prop = project.projectProperties
- if (!prop) {
- final def title = 'Selection to List'
- final def msg = 'Please try again after you open a project.'
- showMessageDialog null, msg, title, INFORMATION_MESSAGE
- return
- }
- // get segment #, source filename and the whole current segment
- def srcfile = editor.currentFile
- def ste = editor.currentEntry
- cur_text = ste.getSrcText()
- cur_seg = ste.entryNum()
- // define list file
- def folder = prop.projectRoot
- def fileloc = folder+'/terms_list.txt'
- list_file = new File(fileloc)
- // create file if it doesn't exist
- if (! list_file.exists()) {
- list_file.write("",'UTF-8')
- }
- /*
- * command to open the file if there's no active selection
- * if a custom (not OS default) text editor should be used,
- * it needs to be defined in the next line (edit as needed and uncomment)
- */
- // def textEditor = /path to your editor/
- def command
- switch (osType) {
- case [OsType.WIN64, OsType.WIN32]:
- command = "cmd /c start \"\" \"$list_file\"" // default
- try { command = textEditor instanceof List ? [*textEditor, list_file] : "\"$textEditor\" \"$list_file\"" } catch (ignore) {}
- break
- case [OsType.MAC64, OsType.MAC32]:
- command = ['open', list_file] // default
- try { command = textEditor instanceof List ? [*textEditor, list_file] : ['open', '-a', textEditor, list_file] } catch (ignore) {}
- break
- default: // for Linux or others
- command = ['xdg-open', list_file] // default
- try { command = textEditor instanceof List ? [*textEditor, list_file] : [textEditor, list_file] } catch (ignore) {}
- break
- }
- def sel_txt = editor.selectedText
- if (sel_txt) {
- list_file.append "${'='*10}\n $sel_txt\n",'UTF-8'
- if (cur_text =~ sel_txt) {
- list_file.append "${'-'*5}\n\
- filename: $srcfile\n\
- segment: $cur_seg\n\
- segment text: $cur_text \n\n",'UTF-8'
- }else{
- list_file.append "${'-'*5}\n\
- filename: $srcfile\n\
- ***Selection outside of current segment***\n",'UTF-8'
- }
- console.println "\"$sel_txt\" written to $list_file"
- } else {
- console.println "[No selection]"
- console.println "***Opening the file in text editor***"
- console.println "Command: $command"
- command.execute()
- return // exit
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement