Send Keystroke using powershell without activating window - powershell-2.0

I am performing keystroke to a application, while performing this, application will become active. This will interrupt my current activity.
Method 1:
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
$wshell.SendKeys('~')
Method 2:
Importing WASP module and sending key stroke.
$window = Select-Window | Where-Object {$_.Title -like "*ApplicationName*" }
Send-Keys -Keys " " -Window $window
Both the methods will activate the application window and then send keystroke.
Is there any way to send key without activating the window ?

Related

What would the equivalent code be in JXA for getting the URL from a Firefox browser?

tell application "System Events" to get value of UI element 1 of combo box 1 of toolbar "Navigation" of first group of front window of application process "Firefox"
I am using the above in an AppleScript to get the URL from the Firefox browser, what would the equivalent be using JXA.
I am using JXA rather than an AppleScript because applescripts hate when you don't have a specific browser installed but still use it in the script.
Here I tested the apple-script you mentioned from the GitHub site that you would like to translate into JXA.
It contains 2 important bugs: 1) When the script is run, the front application is one, which executes the script and not the browser. This has been erroneously ignored. 2) If you do not have the Google Chrome application installed, then the script will not even compile.
The following Apple-script fixes these 2 critical bugs. I am not a JXA expert, so I leave my script as is.
property chromium_variants : {"Google Chrome", "Chromium", "Opera", "Vivaldi", "Brave Browser", "Microsoft Edge"}
property webkit_variants : {"Safari", "Webkit"}
property browsersList : chromium_variants & webkit_variants & "firefox"
tell application "System Events"
repeat 10 times
set frontApp to name of first process whose frontmost is true
if browsersList contains frontApp then exit repeat
set visible of process frontApp to false
end repeat
end tell
if (frontApp starts with "Safari") or (frontApp starts with "Webkit") then
set videoURL to run script "tell application " & frontApp & " to return URL of front document"
set videoTitle to run script "tell application " & frontApp & " to return name of front document"
else if frontApp is "Firefox" then
set videoURL to my firefoxCurrentTabURL()
set videoTitle to my firefoxCurrentTabUTitle()
else if (frontApp starts with "Google Chrome") or (frontApp starts with "Chromium") or (frontApp starts with "Opera") or (frontApp starts with "Vivaldi") or (frontApp starts with "Brave Browser") or (frontApp starts with "Microsoft Edge") then
set videoURL to run script "tell application " & frontApp & " to return URL of active tab of first window"
set videoTitle to run script "tell application " & frontApp & " to return title of active tab of first window"
else
return "You need a supported browser as your frontmost app"
end if
return {videoURL:videoURL, videoTitle:videoTitle}
on firefoxCurrentTabURL()
-- Store the current clipboard contents.
set theClipboard to (the clipboard as record)
-- Set the clipboard to a default blank value
set the clipboard to ""
-- Bring Firefox to the front, highlight the URL in the URL field and copy it.
tell application "System Events"
set frontmost of application process "firefox" to true
keystroke "lc" using {command down}
end tell
-- Read the clipboard contents until either they change from "" or a second elapses.
repeat 10 times
delay 0.2
set theURL to the clipboard
if theURL is not "" then exit repeat
end repeat
-- Restore the old clipboard contents.
set the clipboard to theClipboard
return theURL
end firefoxCurrentTabURL
on firefoxCurrentTabUTitle()
tell application "System Events" to tell process "firefox"
set frontmost to true
set the_title to name of windows's item 1
set the_title to (do shell script "echo " & quoted form of the_title & " | tr '[' ' '")
set the_title to (do shell script "echo " & quoted form of the_title & " | tr ']' ' '")
end tell
end firefoxCurrentTabUTitle

Command line print via GhostScript is handling printer settings differently if using print dialog

I'm trying to print a PDF file via GhostScript command and want
to keep alive the default printer settings be done within the system environment (Windows 10 - set paper tray 3 as default).
When is done so without silent mode by using the upcoming printer dialog this works fine (even without setting up paper tray especially)
BUT - as I want the process to be done without dialog - I've also tried it with defining the printer name within the command line.
What works properly, is that the print out happens without dialog - BUT the default configured paper tray doesn't get used - when I print silently - paper tray 1 is used
Is there a possibility to keep the default paper tray settings alive while naming the printer within the command line?
Here is my codeline:
gswin64c.exe -dPrinted -dNoCancel -dBATCH -dNOPAUSE -dNOSAFER -q -dBitsPerPixel=4 -sDEVICE=mswinpr2 -sPAPERSIZE=a4 -sOutputFile=%printer%" + "\"" + printerName + "\"" +" " + "\""+pdfFileName+ "\"";
To the best of my knowledge, the mswinpr2 device always uses the default setup of the printer, unless you get a print dialog, in which case you can override the default.
Perhaps the default tray isn't the one you think it is.
Yes - I'm sure that the paper tray is set correct (tray3)
It is used when printing with other applications and also when I print via Ghostscript using a print dialog but not when I send a print job silently via command line.
Oliwan

Start .vbs script without icon of wscript.exe in the taskbar

How can I Start .vbs script without icon of wscript.exe in windows taskbar
Unlike the CScript.exe, Windows does not show the WScript.exe host executable icon by default (shows Popup, MsgBox, InputBox and Echo only). Check the difference:
start "" cscript 30598853.vbs
will show a Windows script host icon in the Windows taskbar whilst
start "" wscript 30598853.vbs
will not. In both cases, an additional Windows script host icon will appear for a while (i.e. for the duration of the Popup is active in given example)...
Create a sample 30598853.vbs script as follows:
Dim WshShell, BtnCode
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
BtnCode = WshShell.Popup( _
WScript.ScriptName & vbNewLine & "Exit script?" _
, 5 _
, WScript.FullName _
, vbOKCancel + vbQuestion)
If BtnCode = vbOK Then Exit Do
Wscript.Sleep 15000
Loop
Edit
To the best of my belief, a window invoked via Popup method or MsgBox function or InputBox function (and Echo method in case of the WScript.exe host executable as well) will always show it's icon in the taskbar. This happens regardless of that particular window is on top or isn't.
Read in another forum that one could assign a fully transparent icon to a particular executable...

PowerShell sending keystrokes e.g. F5 after opening an application

I am using PowerShell to open an application and then send keystrokes F5, which is "Running", just like someone presses the F5 keyboard to start the shortcut.
However, [System.Windows.Forms.SendKeys]::SendWait("{F5}") is not working, I don't know if it is the right sentence or not?
Start-Process "C:\Users\minnie\Documents\Ixia\IxLoad\Repository\HTTP Concurrent conns port1n2.rxf"
Start-Sleep -s 50
[System.Windows.Forms.SendKeys]::SendWait("{F5}")
exit
This is certainly not the best way but you can try something like :
[System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
start-process notepad
$p=get-process | where {$_.name -eq "notepad"}
[Microsoft.VisualBasic.Interaction]::AppActivate($p.ID)
[System.Windows.Forms.SendKeys]::SendWait("~Name:{TAB}$env:UserName~")
# [System.Windows.Forms.SendKeys]::SendWait("{F5}{ENTER}")
You should also look at that extension :
http://wasp.codeplex.com/

Responsive Compile-and-Run Vim Mapping

I have the following mapping in my .vimrc.
:nmap <F5> :<C-U>make %:r && ./%:r<CR>
I press F5 in VIM, and it compiles, exits VIM, and runs my code. When the program terminates, it asks me to "press ENTER or enter a command to continue." It then takes me to a blank screen with the text (1 of 5): and the same "press ENTER or enter a command to continue" prompt. I press enter and it finally returns me back to VIM. This behavior is consistent across the board. Is there a way to remove any or both of those occurrences? Perhaps have the mapping press ENTER twice after the program terminates? If so, how?
EDIT: So I realized appending two more <CR>'s doesn't quite solve the problem. As soon as the program terminates, it IMMEDIATELY goes back to VIM and I don't have time to review the output. Can I make the mapping wait for ME to press the first enter, and automatically press the 2nd ENTER afterwards?
Would this work:
nmap <F5> :<C-U>silent make %:r<CR>:redraw!<CR>:!./%:r<CR>
A longer solution but this one also allows you to see errors (reference):
:function! MakeAndRun()
: silent make %:r
: redraw!
: if len(getqflist()) == 1
: !./%:r
: else
: for i in getqflist()
: if i['valid']
: cwin
: winc p
: return
: endif
: endfor
: endif
:endfunction
:nmap <F5> :call MakeAndRun()<cr>
Yes and yes (you answered your own question):
:nmap <F5> :<C-U>make %:r && ./%:r<CR><CR>
For me this works fine:
" Compile
noremap <F4> :<C-U>silent make<CR>:redraw!<CR>
" Automatically open, but do not go to (if there are errors) the quickfix /
" location list window, or close it when is has become empty.
autocmd QuickFixCmdPost [^l]* nested cwindow
autocmd QuickFixCmdPost l* nested lwindow
It compiles, and immediately jumps to vim, showing the quickfix window. No intermediate enters.

Resources