JohnDoe365 · 2022-04-24 21:10
I do the bigger part of my note taking in markdown. These files reside where my projects are. Now an image is often worth more than words. I snip it but I have yet to find an elegant solution to quickly save the system clipboards content as a file (automatic file naming) and insert it in Emacs at point as a reference in markdown syntax.
Is there any Windows solution available?
Professional_Cow5629 · 2022-04-24 21:30
You want something like:
1) take a snapshot with your favourite screen capture tool which stores the image in the clipboard
2) then you go to your org file (or markdown) and then via something like C-x i m you want to get:
- the link created in your org file (or markdown) at point
- with ad-hoc png file created in a directory
If that is what you want, then this is what I do with a mix of elisp plus some powershell. Happy to share.
ArchAuthor · 2022-04-25 03:37
I also use this extensively at work and would love to see what you've written!
Cheers!
JohnDoe365 · 2022-04-25 07:47
Exactly that's what I need. I already have a powerful snipping tool I like, it's about writing the clipboard contents to disk an insert it a point as a local markdown link (not using org but of course, would be nice if the tool could support more than one mode)
Professional_Cow5629 · 2022-04-25 09:57
1st part of the solution ... This part of code goes into your powershell profile.
If you do not know where it is just enter in a Powershell window
start notepad $profile
\------------------------------------------------------------------------------------
function ImageinClipboard2File($Dest)
{
\# Any image available in the clipboard
$img=get-clipboard -format image
if ($img -ne $null) {
\# Create the file and the directory if required - No Output
New-Item -Path $Dest -ItemType File -Force | out-null
\# Actually save the image into the file
$img.save($Dest)
return "Image found in clipboard and created in $Dest"
}
else {
return "No image found in clipboard"
}
}
\----------------------------------------------------------------
Professional_Cow5629 · 2022-04-25 09:59
Then goes the part from your emacs configuration
;; Adding images
(defvar my/screenshot-directory "./images/")
;; Key function ... Script part commented out // to be improved
(defun my/take-screenshot ()
"This works only in Windows. Take the image in the
clipboard, name it with a timestamp,and store it in png format
into \`my/screenshot-directory' "
(let ((filename))
(setq filename (concat my/screenshot-directory
(format-time-string "%Y-%m-%dT%H%M%S")
".png"))
(message (shell-command-to-string (concat "Powershell ImageinClipboard2File -Dest " filename " ")))
filename))
;; Create the link at point in the org document
;; v1 is just inserting the link
;; v2 is adding the caption so that it is included automatically
(defun my/org-screenshot ()
"Save a screenshot in clipboard into\`my/screenshot-directory'.
Insert an Org link to the image file."
(interactive)
(let ((filename (my/take-screenshot)))
(insert
;; (concat "\[\[file:" filename "\]\]")
;; (concat "#+ATTR\_ORG: :width 900" "\\n" "#+NAME: fig:" filename "\\n" "#+CAPTION: " filename "\\n" "\[\[file:" filename "\]\]")
(concat "#+ATTR\_ORG: :height 300" "\\n" "#+NAME: fig:" filename "\\n" "#+CAPTION: " filename "\\n" "\[\[file:" filename "\]\]")
)))
;; Associate to C-c i m to get it in the org buffer
(global-set-key (kbd "C-c i m") 'my/org-screenshot)
Professional_Cow5629 · 2022-04-25 10:09
A few comments:
1) This example is powershell 5 but it works on windows with powershell 7
2) The directory where the images are saved is a subdirectory of the file you are editing. This subdirectory is called images. If you want to change that (for a global directory) just change the value of the variable my/screenshot-directory
3) Format-time-string is the place where I create the unique name which is taking the timestamp of the moment. When editing my org document, changing the caption enables me to give a user-friendly reference to the image
4) Could that work on non-windows ? Maybe yes.... 2 conditions:
\- Using powershell 7 ==> I have used it on Mac so potentially OK.
\- Powershell 7 is able to access the clipboard and recognise that it is an image (cf. the 1st line of the powershell code). I did not test it on Mac and non-Windows. This is the test to be made as pretty much everything else is plain vanilla Powershell or elisp.
5) I use PicPick to capture screen and edit the image. So from org I can do C-c C-o and I get the image in PicPick editor from the file linked to my org document and I can edit it add annotations and save it....
C-u C-c o to see the image in emacs
Hope it helps...
Professional_Cow5629 · 2022-04-25 10:11
For markdown - which I do not use - tweak slightly the link defined in the example.. That is now simply some tweaking of the elisp used.