Testing scripts

You know, of course, about the Scripting Plugin for OmegaT. If you’re a member of OmegaT Yahoo user group, you may know Yu Tang who provided a bunch of nice scripts to open various files and folders of the currently active OmegaT project. His scripts have been included in the most recent version of the Scripting Plugin. The only problem with them was that one needed to edit them if they were used on OS’es other than MS Windows. Generally not a problem, as many OmegaT users are quite computer savvy, but it would be nicer if they could work everywhere “out of box”.

Yu Tang has updated the scripts so now they check which OS they are running on. If you’re willing to test them out and report any possible problems, please do.

The scripts are listed here and posted on the Pastebin.com with the link to each individual script right before the respective listing.

  1. open_current_file.groovy
    /*
    * Open current file
    *
    * @author Yu Tang
    * @date 2013-05-23
    * @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 = 'open current file'
    final def msg = 'Please try again after you open a project.'
    showMessageDialog null, msg, title, INFORMATION_MESSAGE
    return
    }
    
    // get command GString to open a file
    def file = prop.sourceRoot + editor.currentFile
    def command
    switch (osType) {
    case [OsType.WIN64, OsType.WIN32]:
    command = "cmd /c start \"\" \"$file\"" // for WinNT
    // command = "command /c start \"\" \"$file\"" // for Win9x or WinME
    break
    case [OsType.MAC64, OsType.MAC32]:
    command = "open \"$file\""
    break
    default: // for Linux or others
    command = ['xdg-open', file]
    break
    }
    
    // open it
    command.execute()
    
  2. open_folder.groovy
    /*
     *  Open project folder
     *
     * @author  Yu Tang
     * @date    2013-05-23
     * @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 = 'open project folder'
      final def msg   = 'Please try again after you open a project.'
      showMessageDialog null, msg, title, INFORMATION_MESSAGE
      return
    }
    
    // get command GString to open a folder
    def folder = prop.projectRoot
    def command
    switch (osType) {
      case [OsType.WIN64, OsType.WIN32]:
        command = "explorer.exe \"$folder\""
        break
      case [OsType.MAC64, OsType.MAC32]:
        command = "open \"$folder\""
        break
      default:  // for Linux or others
        command = ['xdg-open', folder]
        break
    }
    
    // open it
    command.execute()
    
  3. open_glossary.groovy
    /*
     *  Open the writeable glossary in an editor
     *
     * @author  Yu Tang
     * @date    2013-05-23
     * @version 0.2
     */
    
    import static javax.swing.JOptionPane.*
    import static org.omegat.util.Platform.*
    
    /**
     * Uncomment the next line if you want to set a default text editor
     * that will open glossary file
     */
    // def textEditor = /path to your editor/
    // E.g., /TextMate/
    // /C:\Program Files (x86)\editor\editor.exe/
    // ['x-terminal-emulator', '-e', 'vi']
    
    // make a Closure to show message dialog
    def showMessage = { msg -> showMessageDialog null, msg, 'Open glossary', INFORMATION_MESSAGE }
    
    // abort if a project is not opened yet
    def prop = project.projectProperties
    if (!prop) {
      showMessage 'Please try again after you open a project.'
      return
    }
    
    // exit if file not found
    def file = prop.writeableGlossary
    if (! new File(file).exists()) {
      showMessage 'Glossary file not found.'
      return
    }
    
    // get command GString list to open a file
    def command
    switch (osType) {
      case [OsType.WIN64, OsType.WIN32]:
        command = "cmd /c start \"\" \"$file\""  // default
        try { command = textEditor instanceof List ? [*textEditor, file] : "\"$textEditor\" \"$file\"" } catch (ignore) {}
        break
      case [OsType.MAC64, OsType.MAC32]:
        command = "open \"$file\""  // default
        try { command = textEditor instanceof List ? [*textEditor, file] : "open -a \"$textEditor\" \"$file\"" } catch (ignore) {}
        break
      default:  // for Linux or others
        command = ['xdg-open', file] // default
        try { command = textEditor instanceof List ? [*textEditor, file] : [textEditor, file] } catch (ignore) {}
        break
    }
    
    // open it
    console.println "command: $command"
    command.execute()
    
  4. open_project_save.groovy
    /*
     *  Open project_save.tmx in an editor
     *
     * @author  Yu Tang
     * @date    2013-05-23
     * @version 0.2
     */
    
    import static javax.swing.JOptionPane.*
    import static org.omegat.util.Platform.*
    
    /**
     * Uncomment the next line if you want to set a default text editor
     * that will open project_save.tmx
     */
    // def textEditor = /path to your editor/
    // E.g., /TextMate/
    // /C:\Program Files (x86)\editor\editor.exe/
    // ['x-terminal-emulator', '-e', 'vi']
    // "x-terminal-emulator -e vi".split()
    
    // abort if a project is not opened yet
    def prop = project.projectProperties
    if (!prop) {
      final def title = 'open project_save.tmx'
      final def msg   = 'Please try again after you open a project.'
      showMessageDialog null, msg, title, INFORMATION_MESSAGE
      return
    }
    
    // get command GString list to open a file
    def file = "${prop.projectInternal}project_save.tmx" 
    def command
    switch (osType) {
      case [OsType.WIN64, OsType.WIN32]:
        command = "cmd /c start \"\" \"$file\""  // default
        try { command = textEditor instanceof List ? [*textEditor, file] : "\"$textEditor\" \"$file\"" } catch (ignore) {}
        break
      case [OsType.MAC64, OsType.MAC32]:
        command = "open \"$file\""  // default
        try { command = textEditor instanceof List ? [*textEditor, file] : "open -a \"$textEditor\" \"$file\"" } catch (ignore) {}
        break
      default:  // for Linux or others
        command = ['xdg-open', file] // default
        try { command = textEditor instanceof List ? [*textEditor, file] : [textEditor, file] } catch (ignore) {}
        break
    }
    
    // open it
    console.println "command: $command"
    command.execute()
    
  5. open_tm_folder.groovy
    /*
     *  Open the /tm folder
     *
     * @author  Yu Tang
     * @date    2013-05-23
     * @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 = 'open TM folder'
      final def msg   = 'Please try again after you open a project.'
      showMessageDialog null, msg, title, INFORMATION_MESSAGE
      return
    }
    
    // get command GString to open a folder
    def folder = prop.TMRoot
    def command
    switch (osType) {
      case [OsType.WIN64, OsType.WIN32]:
        command = "explorer.exe \"$folder\""
        break
      case [OsType.MAC64, OsType.MAC32]:
        command = "open \"$folder\""
        break
      default:  // for Linux or others
        command = ['xdg-open', folder]
        break
    }
    
    // open it
    command.execute()
    

To test, put the scripts to other scripts that come with the Scripting Plugin and run them on a real or testing OmegaT projects. Among the things to check are: ability to run at all, ability to run when the filenames or path to the projects contain spaces and “special” characters.
You can post your findings in comments, or, if you’re so inclined, here (clickable)


UPDATE

Thanks to all who participated in testing, now shiny new scripts got included in a new release of the Scripting Plugin for OmegaT 2.6, and as an integral part of new OmegaT 3.0. Pretty cool, yeah?

One thought on “Testing scripts

  1. Hello! Sorry for writing here but I find no way to communicate with Yu Tang. The thing is that I’m on a Mac and none of this scripts are working for me now. I get an error message saying: javax.script.ScriptException: java.io.IOException: Cannot run program “open”: error=2, No such file or directory

    Could you please help me? Every scripts that implies opening something (folders, files, even the one you posted here for creating tables from Velior is saying the same :S).

    Regards,
    Víctor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s