Unable to get visible texteditor in other viewcolumn - visual-studio-code

I'm trying to open the active file in a new viewcolumn, and fold a tag. The folding commands work fine in activeTextEditor:
// Fold based on linenumber
let range = editor.document.lineAt(lineNumber).range;
editor.selection = new vscode.Selection(range.start, range.end);
editor.revealRange(range);
commands.executeCommand('editor.fold');
Now I would like to do the same in a newly opened file:
// Open the same file in a new column
// at this time editor.ViewColum is One
commands.executeCommand('vscode.open', Uri.file(editor.document.fileName), ViewColumn.Two);
// Try to get that editor
let newEditor = vscode.window.visibleTextEditors.find(x=> x.viewColumn===viewColumn.Two && x.document.fileName===fileName)
The problem is that newEditor is not found, becouse the newly opened document has ViewColumn undefined.
Any idea how to solve this?
Thanks

The vs commands returns a promise. Needed to await that and everything works fine.

Related

How to properly close Word documents after Documents.Open

I have the following code for a C# console app. It parses a Word document for textboxes and inserts the same text into the document at the textbox anchor point with markup. This is so I can convert to Markdown using pandoc, including textbox content which is not available due to https://github.com/jgm/pandoc/issues/3086. I can then replace my custom markup with markdown after conversion.
The console app is called in a PowerShell loop for all documents in a target list.
When I first run the Powershell script, all documents are opened and saved (with a new name) without error. But the next time I run it, I get an occasional popup error:
The last time you opened '' it caused a serious error. Do you still want to open it?
I can get through this by selecting yes on every popup, but this requires intervention and is tedious and slow. I want to know why this code results in this problem?
string path = args[0];
Console.WriteLine($"Parsing {path}");
Application word = new Application();
Document doc = word.Documents.Open(path);
try
{
foreach (Shape shp in doc.Shapes)
{
if (shp.TextFrame.HasText != 0)
{
string text = shp.TextFrame.TextRange.Text;
int page = shp.Anchor.Information[WdInformation.wdActiveEndPageNumber];
string summary = Regex.Replace(text, #"\r\n?|\n", " ");
Console.WriteLine($"++++textbox++++ Page {page}: {summary.Substring(0, Math.Min(summary.Length, 40))}");
string newtext = #$"{Environment.NewLine}TEXTBOX START%%%{text}%%%TEXTBOX END{Environment.NewLine}";
var range = shp.Anchor;
range.InsertBefore(Environment.NewLine);
range.Collapse();
range.Text = newtext;
range.set_Style(WdBuiltinStyle.wdStyleNormal);
}
}
string newFile = Path.GetFullPath(path) + ".notb.docx";
doc.SaveAs2(newFile);
}
finally
{
doc.Close();
word.Quit();
}
The console app is called in a PowerShell loop for all documents in a target list.
You can automate Word from your PowerShell script directly without involving any other dependencies. At least that will allow you to keep a single Word instance without creating each time a new Word Application instance for each document:
Application word = new Application();
Document doc = word.Documents.Open(path);
In the loop you could just open documents for processing and then closing them. It should improve the overall performance of your solution.
When you are done processing a document you need to close it by using the Close method which closes the specified document.
Also when a new Word Application instance is created, don't forget to close it as well by calling the Quit method which quits Microsoft Word and optionally saves or routes the open documents.
Application.Quit SaveChanges:=wdSaveChanges, OriginalFormat:=wdWordDocument

VSCode create unsaved file and add content

In VS Code I would like to create a new document in a new editor (same window), but it need to remain unsaved. I cannot find a way to programmatically set the content of this document while it is in a unsaved state.
I have used:
commands.executeCommand("workbench.action.files.newUntitledFile")
but there seems to be no way to then add content to the file.
When I create a new temporary file and open it with:
workspace.openTextDocument(path)
The file is already saved.
Any thoughts?
Try using openTextDocument with an untitled document to create a unsaved file at a given path, and then use WorkspaceEdit to add some text:
import * as vscode from 'vscode';
import * as path from 'path';
const newFile = vscode.Uri.parse('untitled:' + path.join(vscode.workspace.rootPath, 'safsa.txt'));
vscode.workspace.openTextDocument(newFile).then(document => {
const edit = new vscode.WorkspaceEdit();
edit.insert(newFile, new vscode.Position(0, 0), "Hello world!");
return vscode.workspace.applyEdit(edit).then(success => {
if (success) {
vscode.window.showTextDocument(document);
} else {
vscode.window.showInformationMessage('Error!');
}
});
});
The new file will be unsaved when first opened, but saved to the given path when a user saves it.
Hope that provides a good starting point.
I don't know how to open it in the editor, but you can create an unsaved file with content like the following:
vscode.workspace.openTextDocument({
content: "your content",
language: "text"
});
That should be supported in VSCode 1.54 (Feb. 2021), meaning the vscode.workspace.openTextDocument script from Matt is implemented by default:
Open Editors New Untitled File action
We have introduced a New Untitled File action in the Open Editors view title area.
I was having some issues when the document was eventually saved using this solution from Matt, but I was able to use it in combination with DarkTrick's response.
By using the default behavior of creating an empty document and making it active in the then clause.
vscode.workspace.openTextDocument({
content: newXmlContent,
language: "xml"
}).then(newDocument => {
vscode.window.showTextDocument(newDocument);
});
This allows me to create an untitled document with any content I want and show it in the editor. I was not able to give it a specific name though. This might be a limitation of creating an untitled document.

Poltergeist-phantomjs - switching to the new popped up window

Test env: Capybara,poltergeist, phantomjs.
A new window opens up when I click a link in my test case. I was able to switch to the new window and verify text using selenium driver. However, I am unable to switch to the new window with poltergeist. I tried the following method to switch to the new window and none of them worked.
I wanted to see if a new browser is getting open at all and looks like it is.
main = page.driver.browser.window_handles.first
puts main (gives 0)
popup = page.driver.browser.window_handles.last
puts popup (gives 1)
1. within_window(->{ page.title == '2015-11-5.pdf' }) { assert_text(facility_name) }
2. page.switch_to_window(window=popup)
3. page.switch_to_window(page.driver.browser.window_handles.last)
4. page.driver.browser.switch_to().window(page.driver.browser.window_handles.last)
Could someone provide any inputs here? Thanks!
I used the following and the popup is getting generated and the control switches to it.
page.switch_to_window(page.window_opened_by{click_link('Generate Report')})
The new window has a pdf embedded in it.I was able to read and verify the contents of the document when I use selenium driver. With poltergeist, I am unable to read the pdf. Could you give me some pointers on how to proceed?
Capybara has a number of cross driver methods for dealing with this without having to go to driver specific methods.
popup = page.window_opened_by {
click_link('whatever link opens the new window')
}
within_window(popup) do
# perform actions in the new window
end

trying to access Thunderbird-tabmail does not work

I want to open a new tab with a gloda conversation from inside calendar code.
I receive an error from error console:
window not defined (or document not defined), depending on which of the two I use to Access tabmail:
let tabmail = window.document.getElementById("tabmail");
let tabmail = document.getElementById("tabmail");
The code works fine if the js file is included in an overlay xul-file.
But I want to use it outside of xul in my code.
Somewhere in my calendar code (in my 'addevent'), the same code throws the error.
This code is originally called from a rightclick on an email, but several layers deep into calendar code.
In MDN, I read that window is global? So what do I Need to do to add an tab?
This part works if tabmail is properly referenced:
tabmail.openTab("glodaList", {
collection: queryCollection,
message: aCollection.items[0],
title: tabTitle,
background: false
});
So how do I get a reference for tabmail?
Any help is appreciated.
after trying and looking through code for really some time before posting, it took only ca. 20 minutes to accidentally find the solution after submitting the question..
While browsing mailutils on mxr for something else, I found the solution in some function:
mail3PaneWindow = Services.wm.getMostRecentWindow("mail:3pane");
if (mail3PaneWindow) var tabmail = mail3PaneWindow.document.getElementById("tabmail");

sendkeys() enters text and clears it in an auto-complete search field

I am not able to get webdriver working on entering some text into an auto-complete based search text-field.
I am using the following code:
//here elmt is a WebElement variable.
elmt = driver.findElement(By.id(testDataMap.get("globalSearchTextLocator")));
elmt.sendKeys(patientName);
//Here I am finding the search result list once webdriver enters the characters.
elmt = driver.findElement(By.cssSelector(testDataMap
.get("searchPatientNameLocator")));
searchedPatientsList = driver.findElements(By.cssSelector(testDataMap
.get("searchPatientNameLocator")));
I also tried using elmt.click() before elmt.sendKeys(). It worked for a few random times I ran the test. But mostly, it fails.
What happens is webdriver enters the text into the search-field and clears it the next moment. This leads to no search result and fails the test. I am not able to trace the problem behind this weird behaviour. Any help? Thanks in advance!
I have face similar type of problem, but it is for numberic field. Try using this below code and execute ur script.
String accOffNoID = OR.getProperty("AccOffNo_ID");
WebElement accOffNoInput = driver.findElement(By.id(accOffNoID));
accOffNoInput.clear();
accOffNoInput.sendKeys(String.valueOf(9874651230L));
accOffNoInput = driver.findElement(By.id(accOffNoID));
Change the names according to your script and execute, it should work.