0

I've got the following code, which creates a filename, inserts a link to it at point, and should also save the current image in clipboard to disk at that filename/location. It used to work, but something broke recently, and I can't figure it out. The link is inserted correctly - the saving of the file is broken somehow.

Would greatly appreciate your help!

(defun my/org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
   same directory as the org-buffer and insert a link to this file."
  (interactive)
  (setq filename
        (concat
         (make-temp-name
          (concat (buffer-file-name)
                  "_"
                  (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))
(shell-command (format "powershell.exe -Command \"Add-Type -AssemblyName System.Windows.Forms; $clipImg = [System.Windows.Forms.Clipboard]::GetImage(); $clipImg.Save('%s')\"" filename))
  (insert (concat "[[file:" filename "]]"))
  (org-display-inline-images))

it doesn't render well in the comment, but the error I get (even when running it from powershell itself, but with powershell -Command...) is:

At line:1 char:113 ... .Drawing.Bitmap = [System.Windows.Forms.Clipboard]::GetImage(); Syste ... ~ An expression was expected after '('.

This is the actual command being run: powershell.exe -Command "Add-Type -AssemblyName System.Windows.Forms; $clipImg = [System.Windows.Forms.Clipboard]::GetImage^(^); $clipImg.Save('test.png')"

3
  • I get this error : At line:1 char:92 + ... Windows.Forms; = [System.Windows.Forms.Clipboard]::GetImage(); .Save ... + ~ An expression was expected after '('. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression Apr 3 at 13:46
  • Have you tried executing the powershell.exe command with all its arguments in a Command window? Does it work there? I'm not sure how shell-command does things on Windows, but the above should give you a clue about what's going wrong.
    – NickD
    Apr 3 at 14:58
  • so actually the command itself (inside the double quotes) works well, but using the entire form gives me again (edited the main question with the error) Apr 3 at 16:17

1 Answer 1

0

ok, here's the solution, though I'm not sure why it works :)

(defun my/org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
   same directory as the org-buffer and insert a link to this file."
  (interactive)
  (setq filename
        (concat
         (make-temp-name
          (concat (buffer-file-name)
                  "_"
                  (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))
  (powershell (concat "Add-Type -AssemblyName System.Windows.Forms; $clipImg = [System.Windows.Forms.Clipboard]::GetImage(); $clipImg.Save('" filename "')"))
  (insert (concat "[[file:" filename "]]"))
  (org-display-inline-images))

(defun powershell (script)
  "executes the given script within a powershell and returns its return value"
  (call-process "powershell.exe" nil nil nil
                "-Command" (concat "& {" script "}")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.