Taking screen shot of current OS X or iTerm terminal window - macos

In Linux, I'm using imagemagick import -window $WINDOWID to take a screencapture of the terminal.
Is there something similar in OS X?

You want to be using screencapture rather than import, which only works on X11 windows.
If the window is in front, you can try:
screencapture -l$(osascript -e 'tell app "Terminal" to id of window 1') test.png
See this question for some more background.

Try the tty command.
$ tty
/dev/ttys001
You can see what commands are running on that terminal by using `ps -t :
$ ps -t s001 #Where `tty` returns /dev/ttys001
You can also try pgrep too:
$ pgrep -t s001
Edit (You can/I can)
Oh, this is for a screen capture... Wasn't in the OP.
$WINDOWID is a X11 thing, and so is imagemagick. Linux GUI (both KDE and Gnome) are based upon the X11 protocol. The Mac GUI isn't.
On Mac OS X, there's a screencapture command. I haven't used it, but let's look at the manpage:
The screencapture utility is not very well documented to date.
You're on your own.
One more trick...
You can run the X11 server on the Mac. The server is no longer included in Mountain Lion, but Apple recommends you to install it from the XQuartz Project.
You can run the X11 server, then use XTerm windows which will have a Windows ID that can be used with imagemagick.

( Command + Control + Shift + 4 + Space ) for capturing a screenshot of the app window ..
Refer to this link: https://support.apple.com/en-us/HT201361 for learning more about capturing screenshots in macOS.. 👍

Related

How to hide/quit terminal while program is still running in OS X?

I'm doing my project and I need to log keystrokes system wide in macOS. So, I turned over to GitHub and found Swift-Keylogger. The only problem is I can't quit the Terminal while the program is still running.
Is there any way to not to run this in terminal or closing the terminal window instead of creating a mac app like here.
Note: There are some mac app style executables in github but they are not providing the output I want and some need additional permissions.
Instead of running the executable like ./Keylogger use nohup ./Keylogger &.
You can quit the Terminal after executing the command.
To kill the Keylogger, run ps -e | grep "Keylogger" to get pid and kill -9 pid.
P.S. I thought of adding it as well as running at startup but I'm too lazy to update the repository.

Computer Shut Off Python 3.4

A friend of mine wants to automatically shut off his computer using Python 3.4 (to put in a program). Does anyone know a basic as-small-as-possible way to do this?
OS:
Mac OSX Yosemite
Thanks
OS X is a Unix at its base, so you can use Unix commands. you can use either:
#!/usr/bin/python
import os
os.system("shutdown -h now")
Must be run as root, won't work otherwise. You can however add to sudoers (man visudo) shutdown as NOPASSWD program for the users that want to execute the script and use sudo shutdown … instead of just shutdown…
or
import subprocess
subprocess.call(['osascript', '-e',
'tell app "System Events" to shut down'])

How to examine processes in OS X's Terminal?

I’d like to view information for processes running in OS X. Running ps in the terminal just lists the open Terminal windows. How can I see all processes that are running?
Say I’m running a web browser, terminal and text editor. I’d like to see information for the text editor and web browser.
Running ps -e does the trick. Found the answer here.
You can just use top
It will display everything running on your OSX
Using top and ps is okay, but I find that using htop is far better & clearer than the standard tools Mac OS X uses. My fave use is to hit the T key while it is running to view processes in tree view (see screenshot). Shows you what processes are co-dependent on other processes.
You can install it from Homebrew using:
brew install htop
And if you have Xcode and related tools such as git installed on your system and you want to install the latest development code from the official source repository—just follow these steps.
First clone the source code from the htop GitHub repository:
git clone git#github.com:hishamhm/htop.git
Now go into the repository directory:
cd htop
Run autogen.sh:
./autogen.sh
Run this configure command:
./configure
Once the configure process completes, run make:
make
Finally install it by running sudo make install:
sudo make install
Try ps -ef. man ps will give you all the options.
-A Display information about other users' processes, including those without controlling terminals.
-e Identical to -A.
-f Display the uid, pid, parent pid, recent CPU usage, process start time, controlling tty, elapsed CPU usage, and the associated command. If the -u option is also used, display
the user name rather then the numeric uid. When -o or -O is used to add to the display following -f, the command field is not truncated as severely as it is in other formats.
Try the top command. It's an interactive command that will display the running processes.
You may also use the Apple's "Activity Monitor" application (located in /Applications/Utilities/).
It provides an actually quite nice GUI. You can see all the running processes, filter them by users, get extended informations about them (CPU, memory, network, etc), monitor them, etc...
Probably your best choice, unless you want to stick with the terminal (in such a case, read the top or ps manual, as those commands have a bunch of options).
To sort by cpu usage: top -o cpu
if you are using ps, you can check the manual
man ps
there is a list of keywords allowing you to build what you need. for example to show, userid / processid / percent cpu / percent memory / work queue / command :
ps -e -o "uid pid pcpu pmem wq comm"
-e is similar to -A (all inclusive; your processes and others), and -o is to force a format.
if you are looking for a specific uid, you can chain it using awk or grep such as :
ps -e -o "uid pid pcpu pmem wq comm" | grep 501
this should (almost) show only for userid 501. try it.
The slightly GUI way
if you are a cli (ui) fan. I recommend trying https://github.com/clementtsang/bottom which shows not only processes, but also temperature, disk usage and network. Screenshot is running from kitty (terminal) as an example, I use it on OSX default terminal and the color shows up a bit different, but still amazing.
The tree way
As described here : https://en.wikipedia.org/wiki/Pstree will give a better connection on the hierarchy of the processes
brew install pstree # if you need to install it
pstree
pstree -u <user> # show only processes by your user
pstree -s <string> # show only processes with string
pstree -help # show help

Disable/Cancel Sleep Command on MacOSX

It seems to be impossible to completely disable the Sleep option in MacOSX so that a user cannot manually put the system to sleep.
Is there a way in Leopard (or even Snow Leopard) for AppleScript to catch the Sleep event and cancel it?
The answer to your problem is however a simple Apple-osx-Terminal command that prevents OSX to go idle:
$ pmset noidle
Preventing idle sleep (^C to exit)...
The only way to reset the no-sleeping is to issue a 'Control-C'.
See the man pages of pmset for more details.
This command has been deprecated in favor for
$ caffeinate -i
Which does roughly the same.
You can also run caffeinate for a pre-determined amount of time to prevent sleeping, say for 4 hours while you download something, and then run it in the background by adding & to it:
$ caffeinate -t 144000 &
You can't do this from user mode, because the power manager prevents it, so you'll need to use a kernel extension, such as InsomniaX.
Newer version(s) of OSX make it more possible to disable certain options according to https://apple.stackexchange.com/questions/35256/mac-os-x-lion-server-how-to-disable-sleep-restart-shutdown-options-for-users

X11: get list of all gnome-terminal windows on my display?

I have two xterms and several gnome-terminal windows active on my X display.
However, xlsclients only shows one gnome-terminal client.
$ xlsclients
luban.local /usr/X11/bin/xterm
ohm gnome-terminal
luban.local xterm
How can I get a list of the gnome-terminal sessions attached to my display?
This option will run the new terminal window in its own process and as a distinct X client.
--disable-factory
These options
--instance=foo --class=bar
provide a convenient hook to distinguish between sessions:
$ xlsclients -l
...
Window 0x3000001:
Machine: ohm
Name: Terminal
Icon Name: foo
Command: foo
Instance/Class: foo/bar
This is correct; gnome-terminal (like konsole) starts just a single process. If you ask for a second console, the process opens a new window but doesn't start another process just for that window (unlike xterm). This saves a lot of memory and resources.

Resources