I am writing a python macro that opens a file and I can not get the editor to use the file i open. I try the set focus and doCommands focusEditor. I also can not seem to find an example of how to do this. All the examples I find work with just the open document.
To open the file I am using
komodo.openURI("file:///"+path+controller)
Now how do I tell my current komodo.editor to use that file?
Thank you for any help,
Stephen Becker IV
I just did:
komodo.openURI("file:///Users/shanec/upload.py");
and that opened a new buffer for me. I'm figuring there is something I am missing in your question...unless, hmm...are you doing something like this?
komodo.openURI("...")
# we now have a new buffer
komodo.editor.somethingelsenow
If that is in the same macro, you have run into a limitation of the python macro's that we will need to fix. The "komodo api" you are using is quite limited. If you can give me some indication as to what you want to do, I can help with some code to work around this problem (though it *might* require using JavaScript instead).
Then we'll need to look at fixing that particular problem in the api.
Shane
I am trying to make a macro like RadRail's Controller-view switcher. I have this bit of code, but it still is using the view file not the controller i open
#get controller and open
komodo.openURI("file:///"+path+controller)
#go to end get max lines to look at
komodo.editor.documentEnd
maxlines = komodo.editor.lineFromPosition(komodo.editor.currentPos)
#go back to start
komodo.editor.documentStart
komodo.editor.lineEnd()
#highlight text from back (this how the example i found did it)
komodo.editor.vCHomeExtend()
#get the text that was just highlighted
text_line = komodo.editor.selText
#line text no prompts me with first line of the rhtml view file not the controller
prompt.alert("error text line",text_line)
The code is in my svn
http://svn.stephenbeckeriv.com/code/komodo/contoller_to_view_macro.py
I think it can be done in a much cleaner way, but its the best i could do with the docs and examples i found.
Thank you very much
Stephen Becker IV
The "komodo.view" variable is just a reference to the current view at the time the macro started, this does not get updated during the macro execution if the view happens to change. The "komodo" namespace is just a JS object containing references to commonly used parts of the view/editor.
You need to use the Komodo view manager to keep track of the current view if your changing between views.
var view = komodo.view;
// Open (or focus) this file
komodo.openURI("file:///"+path+controller);
// view variable above still references the view where the macro started
// to get the new current view, use the following:
var newview = gViewMgr.currentView;
var neweditor = view.scintilla;
// You could of course update the komodo JS object references, which
// would be fine as well...
komodo.view = newview;
komodo.editor = neweditor;
// Note: This is a javascript sample, but the same applies to Python macros as well var view = komodo.view; // Open (or focus) this file komodo.openURI("file:///"+path+controller); // view variable above still references the view where the macro started // to get the new current view, use the following: var newview = gViewMgr.currentView; var neweditor = view.scintilla; // You could of course update the komodo JS object references, which // would be fine as well... komodo.view = newview; komodo.editor = neweditor;Cheers,
Todd
Ok, a bug in a couple IDL files in Komodo 4.0 prevent a clean way of doing this, so here are two python scripts for opening a new file and getting a handle to the editor component for the new file.
For Komodo 4.0 and 4.1 beta 1:
wwatch = components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(components.interfaces.nsIWindowWatcher)
prompt = wwatch.getNewPrompter (wwatch.activeWindow)
from xpcom.server import UnwrapObject
viewSvc = components.classes["@activestate.com/koViewService;1"].getService(components.interfaces.koIViewService)
# hack for 4.0 to open file from python
pyViewSvc = UnwrapObject(viewSvc)
viewMgr = pyViewSvc._viewMgr
viewMgr.newViewFromURI("file:///Users/shanec/tmp/test.py", "editor")
# end 4.0 hack
komodo.view = viewSvc.currentView.QueryInterface(components.interfaces.koIScintillaView)
komodo.editor = komodo.view.scimoz
komodo.document = komodo.view.document
prompt.alert("Test", "current view uri is:"+komodo.view.document.file.URI)
from xpcom import components wwatch = components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(components.interfaces.nsIWindowWatcher) prompt = wwatch.getNewPrompter (wwatch.activeWindow) from xpcom.server import UnwrapObject viewSvc = components.classes["@activestate.com/koViewService;1"].getService(components.interfaces.koIViewService) # hack for 4.0 to open file from python pyViewSvc = UnwrapObject(viewSvc) viewMgr = pyViewSvc._viewMgr viewMgr.newViewFromURI("file:///Users/shanec/tmp/test.py", "editor") # end 4.0 hack komodo.view = viewSvc.currentView.QueryInterface(components.interfaces.koIScintillaView) komodo.editor = komodo.view.scimoz komodo.document = komodo.view.document prompt.alert("Test", "current view uri is:"+komodo.view.document.file.URI)After I fix the IDL files in 4.1, which will be available in beta 2:
wwatch = components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(components.interfaces.nsIWindowWatcher)
prompt = wwatch.getNewPrompter (wwatch.activeWindow)
uri = "file:///Users/shanec/tmp/test.py"
#first see if we're already open
docSvc = components.classes["@activestate.com/koDocumentService;1"].getService(components.interfaces.koIDocumentService)
viewSvc = components.classes["@activestate.com/koViewService;1"].getService(components.interfaces.koIViewService)
doc = docSvc.findDocumentByURI(uri)
if doc and doc.getView():
# the file is already open, make it the current buffer
doc.getView().makeCurrent(1)
else:
# 4.1 Beta 2 way of doing this
viewSvc.newViewFromURI(uri, "editor")
komodo.view = viewSvc.currentView.QueryInterface(components.interfaces.koIScintillaView)
komodo.editor = komodo.view.scimoz
komodo.document = komodo.view.document
prompt.alert("Test", "current view uri is:"+komodo.view.document.file.URI)
from xpcom import components wwatch = components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(components.interfaces.nsIWindowWatcher) prompt = wwatch.getNewPrompter (wwatch.activeWindow) uri = "file:///Users/shanec/tmp/test.py" #first see if we're already open docSvc = components.classes["@activestate.com/koDocumentService;1"].getService(components.interfaces.koIDocumentService) viewSvc = components.classes["@activestate.com/koViewService;1"].getService(components.interfaces.koIViewService) doc = docSvc.findDocumentByURI(uri) if doc and doc.getView(): # the file is already open, make it the current buffer doc.getView().makeCurrent(1) else: # 4.1 Beta 2 way of doing this viewSvc.newViewFromURI(uri, "editor") komodo.view = viewSvc.currentView.QueryInterface(components.interfaces.koIScintillaView) komodo.editor = komodo.view.scimoz komodo.document = komodo.view.document prompt.alert("Test", "current view uri is:"+komodo.view.document.file.URI)I am using
Komodo Edit, version 4.0.2, build 275451, platform linux-libcpp6-x86.
Built on Sat Feb 10 12:09:05 2007.
and
Komodo Edit, version 4.1.0-beta1, build 276433, platform linux-libcpp6-x86.
Built on Fri Mar 2 17:27:04 2007.
I am using the 4.0 version of the code with a different uri location.
Traceback (most recent call last):
File "/opt/Komodo-Edit-4.0/lib/mozilla/components/koProject.py", line 2570, in evalPythonMacro
return eval('_code()', macroGlobals, macroGlobals)
File "", line 0, in ?
File "", line 48, in _code
File "/opt/Komodo-Edit-4.0/lib/mozilla/python/xpcom/client/__init__.py", line 337, in __getattr__
raise AttributeError, "XPCOM component '%s' has no attribute '%s'" % (self._object_name_, attr)
AttributeError: XPCOM component '' has no attribute 'newViewFromURI'
It looks like I need to read up on XPCOM and Python. Thank you for your help.
It looks like that code relied on one part of the fix as well. You might have to wait for the next beta to try this out, but I'll put these fixes in the 4.0 branch as well.
From python code how does one check the current version of the program? Not every one will use the beta right away, and making two macros does not seem the correct way to handle this.
Thank you for your help.
There is an XPCOM service object for this "koIInfoService".
Example python usage:
infoService = components.classes['@activestate.com/koInfoService;1'].getService(components.interfaces.koIInfoService);
print infoService.version # String
print infoService.buildNumber # String