JEdit Macro - Open and Save File
Question
I have a JEdit (BeanShell) macro which opens a specific file then immediately saves the file to my c:\temp folder (so that I don't accidentally update the real file).
Here is the bean shell code:
logFilePath = "c:\\temp\\aj.txt";
jEdit.openFile( view , logFilePath );
_buffer = jEdit.getBuffer(logFilePath);
_buffer.save(view,"c:\\temp\\backup.txt",true);
This gives me the following error:
I/O Error
Each buffer can only execute one input/output operation at a time.
Please wait until the current operation finishes
(or abort it in the I/O progress monitor) before starting another one.
I have tried adding a while loop to wait until buffer.isLoaded()
is true, but that just goes into an infinite loop.
What does seem to work is popping up a message box ( Macros.message
). However, I really don't want to have this unnecessary dialogue.
I don't know much java, so please tell me if I'm making a rookie mistake.
Update:
Added my own answer to show the code pointed to from Serhii's answer.
Solution
You can try this solution, calling VFSManager.waitForRequests();
.
OTHER TIPS
This Works
This is the code pointed to by Serhii's answer, above.
Add VFSManager.waitForRequests();
after the jEdit.openFile()
command.
Full Code
logFilePath = "c:\\temp\\aj.txt";
jEdit.openFile( view , logFilePath );
VFSManager.waitForRequests();
/*
VFSManager.waitForRequests();
jEdit waits then for the file to be completely loaded before continuing
... It's designed for waiting on all 'pending I/O requests'".
*/
_buffer = jEdit.getBuffer(logFilePath);
_buffer.save(view,"c:\\temp\\backup.txt",true);
You can also do it less bold.
- use the return value of jEdit.openFile(), this is already the Buffer, no need for getBuffer()
- Do not call VFSManager.waitForRequests() which waits for ALL requests to be done, but simply add a BufferListener to the Buffer you got from jEdit.openFile() and do your save call in the bufferLoaded method of this listener :-)