odd behavior with sys.clipboard
My goal is to pipe output from command line GNU DIFF to clipboard and then log it. My problem is that Sys.Clipboard is holding the last thing I copied to clipboard BEFORE running the script. I have verified that the script is running fine when I do it by hand.
I have a TC12 script function that calls the GNU DIFF and then a driver function to make testing more convenient...
Function that does the GNU DIFF and should copy results to clipboard and then log whatever is in clipboard:
def diffTwoFilesToLog(file1, file2): diffCmd = "\"C:\\Program Files (x86)\\GnuWin32\\bin\\diff.exe\" " + file1 + " " + file2 + " | clip" WshShell.Run(diffCmd) Log.Warning(Sys.Clipboard)
Test driver for that function:
def diffTwoFilesToLogDriver(): f1 = "C:\\imports\\exportCompare\\publishtest.txt" f2 = "C:\\imports\\exportCompare\\slush\\actualOCount.txt" diffTwoFilesToLog(f1, f2)
If I put a breakpoint on Log.Warning(Sys.Clipboard) in diffTwoFilesToLog, let it execute, go to "Locals" I see that 'diffCmd' has this value:
"C:\Program Files (x86)\GnuWin32\bin\diff.exe" C:\imports\exportCompare\publishtest.txt C:\imports\exportCompare\slush\actualOCount.txt | clip
^If I copy that exact line into my command line "dos box" and execute by hand it copies the DIFF to clipboard (desired result):
33d32 < ROC Protocol 0 {F0F0ED91-DD1E-4DE3-82AE-9B7AC011D670}
My problem is that Sys.Clipboard is instead holding the last thing I copied to clipboard BEFORE running the TC12 script.
Thoughts on why that is the case?
WshShell.Run by default does not wait for the command to complete, so Log.Warning(Sys.Clipboard) happens before diff completes. To make it wait you need to specify True as the third parameter of the Run method.
WshShell.Run(diffCmd, 0, True)
Alternatively, you can use Python's built-in subprocess.check_output so you won't have to deal with the clipboard:
import subprocess
def diffTwoFilesToLog(file1, file2):
output = subprocess.check_output(["C:\\Program Files (x86)\\GnuWin32\\bin\\diff.exe", file1, file2])
output = output.decode() # convert byte-string to string
Log.Warning(output)