Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

An extremely Mac specific question

Rune
2007-01-31
2013-03-15
  • Rune
    Rune
    2007-01-31

    pydev scripting. Is it possible to get the POSIX path of the current document in Eclipse, and then the do a shell command using this?
    (and of course binding this command to a key stroke)

    To clarify a bit, the emacs/lisp equivalent for the operation I want to perform is:
    (defun save-with-transmit () (interactive)
        (save-buffer)
        (start-process-shell-command "save-with-transmit" nil
                                  "osascript" "~/bin/TransmitFile.scpt"
                                    (shell-quote-argument (buffer-file-name))))
    (global-set-key "\C-x\C-t" 'save-with-transmit)

    This code calls the osascript binary on OSX which in turn executes a AppleScript that communicates with Transmit.app to do a sftp upload of a file.

    regards
    /rune

     
    • Fabio Zadrozny
      Fabio Zadrozny
      2007-01-31

      Yes, should be possible...

      for details on how to bind an action to a keybinding and using the scripting structure, take a look at:
      http://www.fabioz.com/pydev/manual_articles_scripting.html

      To get the path from a string in pydev scripting you would do:

      #save editor:
      from org.eclipse.core.runtime import NullProgressMonitor
      editor.doSave(NullProgressMonitor())

      #get file complete path:
      from org.python.pydev.core import REF
      f = editor.getEditorFile() <-- returns a java.io.File (jython)
      print REF.getFileAbsolutePath(f) <-- return full path in filesystem

      and the other things are not actually pydev-dependent (but you should be able to do it in the scripting structure relying on jython without any problems).

      Cheers,

      Fabio

       
    • Rune
      Rune
      2007-01-31

      Hmm..Do I need a jython instalation for this to work?

      - I've added the jython.jar (with Libs) found in org.python.pydev.jython_1.2.6 to Preferences::interpreter jython.
      - I've created a jython2.1 project called pydev_scripts and added its src folder to additional jython scripts.
      - I've created a file in the src folder "pydev_transmit.py" and pasted the code into the file.

      When saving I get this output in the console:
      --->  reloading /Users/roderik/Documents/workspace/pydev_scripts/src/pyedit_transmit.py
      Traceback (innermost last):
        File "<string>", line 1, in ?
        File "/Users/roderik/Documents/workspace/pydev_scripts/src/pyedit_transmit.py", line 4, in ?
      org.eclipse.swt.SWTException: Invalid thread access
          at org.eclipse.swt.SWT.error(SWT.java:3374)
          at org.eclipse.swt.SWT.error(SWT.java:3297)
          at org.eclipse.swt.SWT.error(SWT.java:3268)
          at org.eclipse.swt.widgets.Widget.error(Widget.java:653)
          at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:331)
          at org.eclipse.swt.custom.StyledText.setEditable(StyledText.java:6757)
          at org.eclipse.jface.text.TextViewer.setEditable(TextViewer.java:1824)
          at org.eclipse.ui.texteditor.AbstractTextEditor.updateState(AbstractTextEditor.java:3766)
          at org.eclipse.ui.texteditor.AbstractTextEditor.doSave(AbstractTextEditor.java:3583)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:585)
          at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java)
          at org.python.core.PyMethod.__call__(PyMethod.java)
          at org.python.core.PyObject.__call__(PyObject.java)
          at org.python.core.PyInstance.invoke(PyInstance.java)
          at org.python.pycode._pyx422.f$0(/Users/roderik/Documents/workspace/pydev_scripts/src/pyedit_transmit.py:4)
          at org.python.pycode._pyx422.call_function(/Users/roderik/Documents/workspace/pydev_scripts/src/pyedit_transmit.py)
          at org.python.core.PyTableCode.call(PyTableCode.java)
          at org.python.core.PyCode.call(PyCode.java)
          at org.python.core.Py.runCode(Py.java)
          at org.python.core.Py.exec(Py.java)
          at org.python.pycode._pyx423.f$0(<string>:1)
          at org.python.pycode._pyx423.call_function(<string>)
          at org.python.core.PyTableCode.call(PyTableCode.java)
          at org.python.core.PyCode.call(PyCode.java)
          at org.python.core.Py.runCode(Py.java)
          at org.python.core.Py.exec(Py.java)
          at org.python.util.PythonInterpreter.exec(PythonInterpreter.java)
          at org.python.pydev.jython.JythonPlugin.exec(JythonPlugin.java:423)
          at org.python.pydev.jython.JythonPlugin.execAll(JythonPlugin.java:302)
          at org.python.pydev.jython.JythonPlugin.execAll(JythonPlugin.java:280)
          at org.python.pydev.editor.scripting.PyEditScripting.doExec(PyEditScripting.java:34)
          at org.python.pydev.editor.scripting.PyEditScripting.onSave(PyEditScripting.java:42)
          at org.python.pydev.editor.PyEditNotifier$2.run(PyEditNotifier.java:57)
          at org.python.pydev.editor.PyEditNotifier$3.run(PyEditNotifier.java:75)
          at org.eclipse.core.internal.jobs.Worker.run(Worker.java:58)

      org.eclipse.swt.SWTException: org.eclipse.swt.SWTException: Invalid thread access

       
      • Fabio Zadrozny
        Fabio Zadrozny
        2007-01-31

        No, you don't need a jython installation (it will use the jython that comes bundled with pydev in the scripting engine).

        The problem in your case is that your code is executed in a thread... what you want to do is register some action and then put your code in that action (which will be executed in an ui thread)... take a look at the pyedit_example2.py to see how you do that.

        Still, if you wanted to execute something that needed ui access in this thread, you'd can use the code below (you can use it for testing, but it is not the way you should do it in the end...)

        from org.eclipse.swt.widgets import Display #@UnresolvedImport
        from java.lang import Runnable

        def myMethod():
            print 'executing in ui thread'

        class RunInUi(Runnable):
            '''Helper class that implements a Runnable (just so that we
            can pass it to the Java side). It simply calls some callable.
            '''
          
            def __init__(self, c):
                self.callable = c
            def run(self):
                self.callable ()
              
        def runInUi(callable):
            '''
            @param callable: the callable that will be run in the UI
            '''
            Display.getDefault().asyncExec(RunInUi(callable))
          
        runInUi(myMethod)

         
      • Fabio Zadrozny
        Fabio Zadrozny
        2007-01-31

    • Rune
      Rune
      2007-01-31

      Brilliant! Thanks!

      regards
      /rune