How to do Threading in Powershell for a function? - multithreading

I have a Powershell GUI, and there are a couple of Checkboxes the user can click ,a Button and a TextBox. If the button is clicked the functions starts working and does a lot of copying renaming etc...
When each "section" is done (like copying a .zip file) I Append a to the Textbox, heres how my program looks like:
$button.Add_Click({Test})
function Test{
"some zip copying code"
$TextBox.AppendText("'r'n ZIP file copied...")
"some renaming code"
$TextBox.AppendText("'r'n File succesfully renamed..")
}
If i press the button the whole program freezes and if done, it prints all "messages" at once. How can i do a threding in it so that the program wont freeze, and when a section is done, it prints to the textbox?

Related

Opening a random file only knowing the extension name anywhere on the computer in python [duplicate]

I want to do the following:
Save numeric data in a CSV-like formatting, with a ".foo" extension;
Associate the ".foo" file extension with some python script, which in turns opens the .foo file, reads its content, and plots something with a plotting library (matplotlib most probably).
The use-case would be: double-click the file, and its respective plot pops up right away.
I wonder how I should write a python script in order to do that.
Besides, the windows "open with" dialog only allows me to choose executables (*.exe). If I choose "fooOpener.py", it doesn't work.
This isn't really a programming question, but what you need to do is to figure out how to get the Python executable into the registry key that opens your data file.
For example, I created a little Python script called opener.py that looks like this:
import sys
print(sys.argv)
input()
Then I created a testfile.foo and used the "change" button in that file's property dialog to choose opener.py. (You can do this if you click Browse and change the Open With dialog's file filter to "All Files".)
Of course this didn't work (as you noticed). So I opened regedit and searched for opener.py and found it at the following registry key:
HKEY_CURRENT_USER\Software\Classes\Applications\opener.py\shell\open\command
The default value of this key was "C:\opener.py" %1. I changed it to python "C:\opener.py" %1. It worked!
Long story short, to do this properly you need to custom-edit the registry. Actually setting up the file association is more complex than just editing that one key (you have to also indicate that .foo is associated with opener.py).
An alternative approach would be to turn your Python script into a standalone executable using one of the several tools available for that purpose, or write a small executable in C that launches the script.
press the windows key
type cmd
right click the result and choose "run as administrator"
assoc .foo=foofile
ftype foofile="C:\Users\<user>\AppData\Local\Programs\Python\PYTHON~1\python.exe" "C:\<whatever>\fooOpener.py" "%1" %*
Use pythonw.exe if it's a .pyw file (to prevent a cmd window from spawning).
If you want to use an existing file type, you can find its alias by not assigning anything. For example, assoc .txt returns .txt=txtfile.
instead of editing registry, you can create batch file
opener.cmd
with content
python "C:\<whatever>\fooOpener.py" %1
and associate extension with this file.
It works!

I need to add code to tkinter exe program?

I want to be able to show the GUI user the code used to do background calculations. However copying and pasting and using "\n" in a text box takes forever. Now I know you can save the code as a pdf. Is there a simple way to attach the pdf to the program and the code still be readable even when it is moved to a different computer.
Main issues:
- How to import and attach a pdf to a button...
- How to include pdf into program so it is readable on any pc...
Thanks in advance.
Packaging:
You can attach your code as pdf file with your program like this using pyinstaller
On Windows:
pyinstaller --add-data="relative/full_path_to_pdf;." my_script.py
On Linux:
pyinstaller --add-data="relative/full_path_to_pdf:." my_script.py
This will pack your pdf file and copy it to the same folder as the .exe package(in case of single file, it will extract it to the temp path along with main .exe which can be accessed with sys._MEIPASS) or You can change the extraction path instead of using '.' Read more here.
In Code:
You can add this type of button in your UI, to open the pdf file with the default viewer of Windows/Linux(same behavior as when you double click the file)
source_code_btn = Button(root, text="Source", command=lambda: subprocess.Popen('{} {}'.format(
"start" if os.name=="nt" else "xdg-open \"$#\" 2>/dev/null",
relative/full_path_to_pdf_file), shell=True))

Search and replace with term list?

I wonder if there is a program that I can use with a list of terms I want to replace instead of take one by one.
Example
À=À
â=â
Â=Â
å=å
Å=Å
ã=ã
Ã=Ã
Thank you in advance
I use UltraEdit and powergrep atm.
UltraEdit has 2 features for automating reformatting tasks: macros and scripts.
See UltraEdit forum topic When to use Scripts over Macros for a brief overview of the differences.
UltraEdit macro
An UltraEdit macro can be created by simply recording the replaces you manually do once on a file or you code them directly in Edit/Create Macro dialog.
A manual creation is done as follows:
Click in UltraEdit in menu Macro on menu item Edit Macro.
Click on button New macro.
Enter as macro name for example ReplaceEntities.
Uncheck macro property Show cancel dialog for this macro.
Let macro property Continue if search string not found checked.
Assign a hotkey or chord for quick execution by key if this is wanted for quick macro execution by key in future.
Click on button OK.
Back in Edit/Create Macro, there is now the new macro selected with 3 lines already present in edit field with the macro commands:
InsertMode
ColumnModeOff
HexOff
Below those 3 macro commands must be added for this reformatting task the macro code lines posted below.
Click on button Close and confirm the question to update the macro with button Yes.
The macro is then ready for usage
by assigned hotkey/chord,
by double clicking on it in Macro List opened via View - Views/Lists - Macro List in case of not being already visible (docked or floating), or
by using Macro - Play Any/Multiple Times.
Also Macro - Play Again can be very often used depending on which macro was last time executed.
The macro code required additionally to 3 standard commands already present in the dialog to replace in entire active file HTML entities is for example:
Top
UltraEditReOn
Find MatchCase "À"
Replace All "À"
Find MatchCase "â"
Replace All "â"
Find MatchCase "Â"
Replace All "Â"
Find MatchCase "å"
Replace All "å"
Find MatchCase "Å"
Replace All "Å"
Find MatchCase "ã"
Replace All "ã"
Find MatchCase "Ã"
Replace All "Ã"
It is necessary to save this UE macro (without or with other UE macros) into a macro file by using Macro - Save All.
For using this macro (and other macros store in same macro file) later again, it is necessary to load the macro file using Macro - Load.
With Macro - Set Auto Load it is possible to select a macro file for being automatically loaded on startup of UltraEdit so that the macros in this macro file are available from beginning without an explicit loading of the macro file.
The macro properties can be also changed later by using Macro - Delete Macro/Modify Properties. Don't forget to use Macro - Save All after making a change to a macro code or its properties to save this change in macro file, too.
UltraEdit script
UltraEdit scripts make use of the JavaScript core engine. An UltraEdit script is an ASCII/ANSI text file containing JavaScript core code with additional UltraEdit related scripting commands. This means an UltraEdit script can be written directly like any other text file and must not be edited in a dialog.
An UltraEdit script which makes exactly the same as the macro above would be:
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
UltraEdit.activeDocument.hexOff();
// Move caret to top of the active file.
UltraEdit.activeDocument.top();
// Define all parameters for several Replace All in entire active file.
UltraEdit.ueReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
UltraEdit.activeDocument.findReplace.selectText=false;
// This property is only available since UE v14.10.
if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
{
UltraEdit.activeDocument.findReplace.searchInColumn=false;
}
UltraEdit.activeDocument.findReplace.replace("À","À");
UltraEdit.activeDocument.findReplace.replace("â","â");
UltraEdit.activeDocument.findReplace.replace("Â","Â");
UltraEdit.activeDocument.findReplace.replace("å","å");
UltraEdit.activeDocument.findReplace.replace("Å","Å");
UltraEdit.activeDocument.findReplace.replace("ã","ã");
UltraEdit.activeDocument.findReplace.replace("Ã","Ã");
}
Such an UltraEdit script should be saved with file extension .js, for example ReplaceEntities.js.
Once an UE script is saved, it can added via Scripting - Scripts to the Script List with adding a short description for the script and assigning a hotkey/chord to the script for quick execution by key.
The script is then ready for usage
by assigned hotkey/chord,
by double clicking on it in Script List opened via View - Views/Lists - Script List or Scripting - Script List in case of not being already visible (docked or floating), or
by clicking on script file name in menu Scripting.
If an UE script is the active file AND it is written to run NOT on active document, the script can be also executed with Scripting - Run Active Script. But most scripts like the one above are written to run on active file and therefore requires adding the script file to the script list for execution.
The core objects and functions of JavaScript are not documented anywhere within UltraEdit although they can be also used in UltraEdit scripts. The documentation for the core features can be found on Mozilla Developer site.
The additional scripting commands of UltraEdit are documented in help of UE on page with title Scripting commands. There is additionally View - Views/Lists - Tag List containing the tag groups UE/UES Script Commands and also UE/UES Macro Commands for quick adding a scripting or macro command of UE in active file at current position of the caret.
Here is my umlaut2html-Makro which does a few automated text-replacements. I guess it can serve as inspiration ;-)
// Lessons learned from Mofi ;-)
UltraEdit.ueReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
UltraEdit.activeDocument.findReplace.selectText=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.replace("ä","ä");
UltraEdit.activeDocument.findReplace.replace("ö","ö");
UltraEdit.activeDocument.findReplace.replace("ü","ü");
UltraEdit.activeDocument.findReplace.replace("Ä","Ä");
UltraEdit.activeDocument.findReplace.replace("Ö","Ö");
UltraEdit.activeDocument.findReplace.replace("Ü","Ü");
UltraEdit.activeDocument.findReplace.replace("ß","ß");
Save this into a .js-file anywhere you like (MyDocuments\UE-Scripts might be a good choice), then call Script > "Scripts..." and "Add", navigate to select that .js-file.

Taking numbers from csv file using labviiew

My program should take all numbers from CSV file put them to array and have some problems.
Have can I take some char of string? Here my program:
https://drive.google.com/file/d/0B7eFfQuRzPgAX3o3WmJIb2dzMFU/view?usp=sharing
Example of csv fie:
1, 2, 3, 4
6, 1, 2, 10
Your code is overly complicated. You can load the file using the Read from Spreadsheet file function and simply create a 2D array of the output.
The Read from Spreadsheet function is polymorphic. That means you can select what data type it uses. Right-click on the node and select 'Visible Items' and then 'Polymorphic VI Selector'.
Change the pull-down on the bottom of the node to String, and then create a string constant and set it's value to , (comma).
please consider using deliminator input on read from spreadsheet VI.
Please check attached
The read from spreadsheet file vi is the simplest solution as answered previously. One thing to note about using that function though is that it internally uses the labview error handler with the "Stop" or "Continue" dialog box popup. I've run into the problem where the user cancels out of the file dialog, then sees the error handler dialog box, presses "Stop" and gets confused why the program behaves unexpectedly afterwards.
To prevent this, test the file path using the "File/Directory Info" vi and "Check if File or Folder exists.vi". Put the Read from spreadsheet file in case structure which only runs when the path is not a directory and exists. I tried to attach a snippet to show this, but do not have enough points.

Opening Excel and PDf files with Tcl Tk

I am having problems opening an existing Excel file with Tcl Tk. I am able to open an existing MS Word file with no problems. The code that I am using is as follows, also my test application has "package require tcom" included:
proc OpenFile {} {
#Path to file
set app [::tcom::ref getobject "C:\\Users\\Me\\Desktop\\Test.doc"]
#Change path to application
set this [$app Application]
#Open application
$this Visible 1
}
This code is executed by a button. Basically, Test.doc gets opened after the button is pressed.
I tried changing the file to an existing Excel file, and when I press the button the file opens for a split second, and then closes. This also happens with MS Access files, as well.
Does anyone know how to open an existing Excel file with Tcl Tk, and make it stay open? Additionally, for PDFs and text files, I understand that I cannot use Tcom to open these files. Does anyone know how to open PDFs, text, and other non-MS files with Tcl Tk?
I really appreciate your help!
Thank you,
DFM
Assuming you're on Windows and you just want to open a file (.xls, .pdf, ...) with its usual application (ie. not modifying the file from your script) you can just use "start" like this:
set TestDoc "My Test.xls"
eval exec [auto_execok start \"\" [list $TestDoc]

Resources