By popular demand, I updated the below solution to include a second function which stores the screenshots in a subfolder.
The Final Result
Let me start this article by showing you my usage of the functionality described. I work as a Data Scientist and use org-mode in Emacs for a large number of every day tasks. One of them is the documentation of new findings within datasets or other software's documentation or websites etc. In order to easily collect all these informations into a single reference, I like to use screenshots:
The only thing "hidden" from this clip is the fact that I hit the combination
C-c s right after finishing my sentence. Everything else ran automatically.
The Step-by-Step Guide (there is only one step)
Assuming you run Emacs on Windows 10, the result is extremely simple to achieve. For Windows 7 and 8, I assume that this approach works perfectly fine, I couldn't put this assumption to the test, though. Here is the singe step needed: edit your
init.el to include the following block:
Copy it in, evaluate it, and you're ready to go: just hit
C-c s whenever you are inside an org-mode-buffer and take screenshots at any whim. They will be saved inside your org-mode folder and displayed inline.
Alternative solution with subfolders
If you want to store your screenshots in a subfolder "images", relative to the location of the org-file, you simply need to replace the function with the second one in the code snippet above.
The (a little more) detailed description
So, what does the magic function do and how does it do it? There are three major parts:
Creating a Unique Name for the Image
This is the first 4 lines, in which the filename of the buffer, the current time stamp and a random combination of letters (via
make-temp_name) is created.
Getting the Screenshot into the Clipboard
This is the function of the line
(shell-command "snippingtool /clip")
which calls the wonderful "SnippingTool" inside Windows and skips all preliminary steps with the command line argument
Saving the Contents of the Clipboard to Disk
This part took me the longest to figure out. The core idea is that
powershell.exe, when called from CMD, has the functionality to save Clipboard contents to file. Hence, the command given here
- Adds appropriate types for Powershell to understand
- Checks if the Clipboard contains an image, and if so
- Saves it to
filename, given in step 3.