Grabbing command output

Asked by Michael A. Phillips

What's the best way to grab some ascii output from a linux cli command such as:

date +%H
date +%M

which would be the hour and minute respectively (00 and 45 near the time of posting)

I tried this:

starthour = openApp("date +%H")
switchApp("gedit")
sleep(1)
print starthour

the logs seem to hint that nothing is done:

[log] App.open date +%H(0) [log] openApp: date +%H
[log] App.focus gedit(0) #0 [log] switchApp: gedit
0

TIA,

Mike

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Michael A. Phillips
Solved:
Last query:
Last reply:
Revision history for this message
Michael A. Phillips (maphilli14) said :
#1

if I use paste(starthour) instead this log is generated:

[log] App.open date +%H(0) [log] openApp: date +%H
[log] App.focus gedit(0) #0 [log] switchApp: gedit
[error] Stopped [error] An error occurs at line 5 [error] Error message: Traceback (most recent call last): File "/tmp/sikuli-tmp7612209417920060044.py", line 5, in paste(starthour) File "/home/miphilli/Documents/Personal/5-Permanent/Software/Sikuli-IDE/sikuli-script.jar/Lib/sikuli/Region.py", line 180, in paste TypeError: java.lang.String(): 1st arg can't be coerced to byte[]

Revision history for this message
Tsung-Hsiang Chang (vgod) said :
#2

You need Python's popen.

----
import os

date = os.popen("date")
print date.readline()

Revision history for this message
Michael A. Phillips (maphilli14) said :
#3

Tsung-Hsiang, that's awesome! How do I get it from the state it's in into a paste-able item?

output:

[log] App.focus gedit(0) #0 [log] switchApp: gedit
09

but the '09' doesn't show in my app.

Using
paste(date)
gives:

[log] App.focus gedit(0) #0 [log] switchApp: gedit
09
in paste TypeError: java.lang.String(): 1st arg can't be coerced to byte[]

Revision history for this message
Tsung-Hsiang Chang (vgod) said :
#4

I guess you have pasted the wrong variable. You should paste(date.readline()), not date itself.

Revision history for this message
Michael A. Phillips (maphilli14) said :
#5

Tsung-Hsiang, this information is of huge value to me, thanks so much!!!!