Matlab: how to select a running task/application in Windows - matlab

I want to use Matlab to automatically control the mouse and the keyboard to operate a running Task/Application. I found Matlab code which helps to show process ID (PID) of running task/application. E.g.
[status,result] = system('tasklist /FI "imagename eq excel.exe" /fo table /nh')
However, how can I select the listed task/application (like left click on the task using its PID)? Since if the wanted task/application is not selected, I may end up to controll the wrong task.
Thanks

Related

distinguish between different instances or windows of a process and find their PID instead of their owner PID to send input event in macOS?

If I open multiple windows/tabs of a browser, and I want to distinguish between them so that I can control those windows individually from my application, what is the approach? If I send command programmatically to a process with PID id it will send that command to the most recently active window but I want to send command to all the window of that process. If I query for PID of running process with CGWindowListOption I get owner PIDs of running processes. let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
I need to know and use something that will trigger multiple window/process at the same time. Is there a different PID of different window of the same process? For example: Chrome tab 1 has a pid, tab 2 will have another pid. How to find those PIDs instead of the owner PID only?
So how can I find out different process id or similar attribute of a process with multiple window with same owner PID?
This activates each window in TextEdit and sends a space keystroke.
activate application "TextEdit"
tell application "System Events"
tell application process "TextEdit"
repeat with theWindow in windows
perform action "AXRaise" of theWindow
keystroke " "
end repeat
end tell
end tell
But to note how this would work without pressing keys, the following appends "A" to all the currently open documents without bringing anything to the foreground or interfering with the user's input.
tell application "TextEdit"
repeat with theDocument in documents
set text of theDocument to (text of theDocument) & "A"
end repeat
end tell
Whenever possible, you want things like this rather than sending keystrokes.

psql -f only runs the first query in file

I have a file make_partition.sql like:
CREATE INDEX my_nice_index ON mytable (ts);
CREATE TABLE mytable_2017_10 AS SELECT * FROM mytable WHERE date_trunc('month', ts) = '2017-10-01';
CREATE TABLE mytable_2017_11 AS SELECT * FROM mytable WHERE date_trunc('month', ts) = '2017-11-01';
I run it in the background using screen and then the command:
psql postgresql://usr:pw#host.com:5432/db_name -f make_partition.sql
And detach from the screen. It completes, but then it appears that only the first query was run. I've tested this a few times with the same result.
How can I get it to run them all? Do I have to put them in a procedure like this? How to run multiple SQL queries?
When you 'detach' from the screen, what is likely happening is an HUP signal is sent to the process and the script stops executing, the query that is currently executing will finish because it is part of a different process.
You can properly send it to the background and make it safe to detach in a couple of ways, one of the ways is using the command nohup (see man nohup for more) which ignores the HUP signal.
An alternative is to start the script as you have above, then pause it by pressing CTRL+Z, you can then send it to the background using bg and finally you can ensure it is ignoring the HUP signal using disown -h (see man disown for more).
This question is relatively old, and Adam Dadvar's answer provides the basics, but I'll provide one other piece of the puzzle that could trip someone up. If the SQL within your file will create output after each statement, you need to redirect at least the stdout into files, rather than letting it go to the terminal. Doing the latter will mean it's displaying the results and waiting for you to spacebar through the results before running the next command. If your code may produce error that you want to ignore, you should also redirect that output. Concretely, using the disown option, it would look something like:
psql postgresql://usr:pw#host.com:5432/db_name -f make_partition.sql 1> messages.log 2> errors.log
Then use CTRL+Z to suspend the current process, disown -h %1 to disown the process, and bg to "background" the process.

Cannot press any key to continue with command pause

I wrote a script to remote execute test.cmd file by PsExec.exe.
PsExec.exe \\IP -u administrator -p password "C:\\Users\Administrator\Desktop\test.cmd"
Although I can run the script successfully, I cannot press any key to continue the process when it runs into command pause.
Press any key to continue . . . aaaaaa
The only way to keep going is press Enter.
Besides, if there are many lines of Pause or set /p which waits users to input, once user press Enter, it will affect couple lines of Pause or set /p. It really confuses me.
Best is that you use different logic in accordance with your limitations.
Try exchanging the Pause with a different suitable solution.
Other suggestion would be that you share your code, inputs and expectations (output is already given - the script doesn’t continue). Then someone can review the code and suggest for further steps.
Alternatively, it is also possible that you can fix it by using Console.ReadKey()

MATLAB code break

I have started running a script on MATLAB that takes days to finish. Usually, if I changed my mind and I don't want wait for it to finish and I get content with the intermediate results, I highlight the command window and press Ctrl-C to break the code.
Now, I have run MATLAB. But its desktop got kinda stuck in the background. When I try to restore the desktop from the toolbar, it does not restore. But I know from the task manager that the process is running and is consuming Memory and CPU performance. So, I am kinda stuck. I don't want to kill the process because I need the intermediate values in the workspace, and I can't open the desktop to break the code using ctrl-c.
Is there any solution? For example, is there any command that can be used in the command prompt to act as ctrl-c for MATLAB?
I am using MATLAB R2012b and Windows 8.
Quick try to fix the recent Problem:
Try ty set a higher priority to matlab.exe in the Task Manager. (Right click -> Priority -> Higher than normal). Then see if you can get the window to front.
Some approaches to avoid this problem in future:
Try to optimize your code. For starters look at: http://de.mathworks.com/help/matlab/matlab_prog/vectorization.html
Use Matlab compiler for faster execution: http://de.mathworks.com/products/compiler/
Include some drawnow commands at stratetic positions in the code. This allows matlab to process the Event Queue and capture ctr-C commands.
Save intermediate results to output files. For example you could write an output file all 30 min with your intermediate results. Easyiest way would be just save(filename). Then a .matfile with all your workspace variables is generated. You can than kill the process in the task manager, without loosing too much results.

How can I select the specific window I want from a list of CMD windows with the same name with AutoHotKey?

I want to make a AutoHotKey script that will send command to a certain CMD window, but I am having problems with selecting the needed window, because I am running at least 3-4 CMD windows and their titles all start with "C:\Window\System32\cmd.exe" so at most I just looping trough the windows one by one.
How can I select !the specific window I want from a list of CMD windows with the same name ?
Most of the cases those are the CMDs I use and I want to target the first one (Upper left corner) which has the most generic name.
You could try starting it from AHK and retrive the ID automatically from there ...
You also could try to build a function which "teaches" which CMD to use ... this would require to click in them CMD window once after it appeared ... basically your AHK script then have to read the UID of this window.
WinGet
Retrieves the specified window's unique ID, process ID, process name, or a list of its controls. It can also retrieve a list of all windows matching the specified criteria.
WinGet, OutputVar [, Cmd, WinTitle, WinText, ExcludeTitle, ExcludeText]
e.g.: WinGet, OutputVar , ID , A would retrieve the ahk_id of the active window
But without further details it is hard to tell what you really need.
I dont think you can distinguish different cmd.exe windows with AutoHotkey.
But you can use other command line software which will allow to distinguish different command line sessions with AutoHotkey. For example, here and here are some other command line software. Other software are easily findable with google. Look at there screenshots, most of them have tabs for different command line sessions. You can switch between different tabs by using AutoHotkey GUI automation commands.
when initializing the windows you could use the title command.
Run, %comspec% /k cd c:\ && title MyWindow 1 && tasklist
winwait,MyWindow 1
WinMove,MyWindow 1,,20,20
Run, %comspec% /k cd c:\ && title MyWindow 2
winwait,MyWindow 2
WinMove,MyWindow 2,,20,365
ControlSend,,taskkill /pid ` ,MyWindow 2