caiorss / clip.jar

Utility to paste images from clipboard to Emacs org-mode or any markdown.
10 stars 2 forks source link

+TITLE: Clip - Utility to copy image from clipboard to file.

+AUTHOR: Caio Rodrigues

+EMAIL: caiorss DOT rodrigues AT gmail DOT com

+KEYWORKDS: clip clipboard image paste emacs org-mode markdown org md scala java

+STARTUP: content

Clip is a self-contained command line utility written in Scala to extract images from clipboard and save it to a file. It is a module of [[https://github.com/caiorss/org-wiki][org-wiki]] project.

Motivation

This utility is useful to paste images on Emacs org-mode and markdown formats like github markdown.

Features

Design decisions

Scala was used in this application because Java platform has the more reliable API to access the clipboard in many platforms including Linux and Windows and the user doesn't need to install any other dependency than Java.

If it was Python the user would have to install gtk or any GUI toolkit dependencies and binaries and also a Python distribution and would be hard distribute it as a self-contained one-file solution.

** Usage

  1. Save an image from clipboard to a file with given name.

    • =$ java -jar Clip.jar --name imageName=

It save the image in the clipboard to ./imagerName.png

Example:

+BEGIN_SRC

$ java -jar ./target/scala-2.11/Clip-assembly-1.0.jar --name /tmp/image.png file:/tmp/image.png

+END_SRC

  1. Save an image from clipboard to a file with a given name

    • =$ java -jar Clip.jar --name imageName /tmp/directory=

It saves the image in the clipboard to /tmp/directory/imageName.png

  1. Copy an image from clipboard to file with an uuid (automatically generated name).

    • =$ java -jar Clip.jar -uuid /tmp=

It saves the image in the clipboard to file /tmp/715dbacb-c6d1-40db-8d91-c90e7217ace8.png

Example:

+BEGIN_SRC

$ java -jar ./target/scala-2.11/Clip-assembly-1.0.jar --uuid /tmp tmp/715dbacb-c6d1-40db-8d91-c90e7217ace8.png

+END_SRC

+BEGIN_SRC text

$ java -jar ~/bin/Clip.jar Usage:

Save clipboard image with a given Name

$ javar -jar Clip.jar --name imageName
  -> save image to ./imageName.png and print ./imageName.png

$ java -jar Clip.jar --name imageName  /tmp
  -> save image to /tmp/imageName.png and print /tmp/imageName.png

Save clipboard image with an automatic generated name

** Build

Development Dependencies

To build it is necessary Scala and Sbt be installed on the system.

It is necessary Java >= v8.0 and Scala runtime >= v2.11.8

+BEGIN_SRC sh

$ java -version java version "1.8.0_74" Java(TM) SE Runtime Environment (build 1.8.0_74-b02) Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

$ scala -version Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL

+END_SRC

The Scala runtime can be downloaded from:

The Sbt building automation tool that is necessary to build and bundle the application with Scala run-time can be downloaded from:

Sbt can be installed with those commands:

+BEGIN_SRC sh

mkdir -p ~/bin

Download sbt building tool to ~/bin/sbt-launch.ja

curl -L -o ~/bin/sbt-launch.jar https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/0.13.13/sbt-launch.jar

Create the sbt launch ~/bin/sbt

cat < ~/bin/sbt

!/bin/bash

SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M" java \$SBT_OPTS -jar ~/bin/sbt-launch.jar "\$@" EOF

Make the sbt launcher executable

chmod u+x ~/bin/sbt

Optional: It is not necessary if ~/bin is already in the $PATH variable.

Add ~/bin to $PATH variable permanently. Oor invoke it with absolute path ~/bin/sbt

# echo "export PATH=\$PATH:~/bin" >> ~/.bashrc

+END_SRC

Build instruction

  1. Install Scala

  2. Install Sbt

  3. Run =$ make= to build the ubber jar. The output file will be ./target/scala-2.11/Clip-assembly-1.0.jar

If Make is not available the compilation can be run with =$ sbt assembly=.

** Test

Copy an image and enter $ make test1. If the app works it will write the image to the file clipboard.png.

Copy an image to

The output will be like:

+BEGIN_SRC

$ make test2 java -jar ./target/scala-2.11/Clip-assembly-1.0.jar -uuid . file:./fba53c12-3f23-4728-9f52-a26a3d285d7c.png

+END_SRC

** Emacs Client Code *** Client code 1

This emacs command can be used to paste images. The command M-x org-paste-image ask the user the directory him whichs to paste and then it inserts the path to the image relative to current directory. If the current directory is ~ (/home/dummy) and the the current file is ~/test.org , if the user chooses to paste the file at directory ~/Pictures it inserts the path to the image file like this.

The advantage of automatic name with UUID is that is easier to paste and move images without any name conflict or overwrite any image.

Warn: This code is synchrnous, therefor Emacs will be frozen while the process doesn't finish. If it takes too long you can abort the execution with C-g.

+BEGIN_SRC elisp

(defvar clipjar-location "~/bin/Clip.jar")

(defun org-paste-image () (interactive) (let* ((dir (read-directory-name "Dir: ")))
(insert
(org-make-link-string (concat "file:" (shell-command-to-string (mapconcat #'identity `("java" "-jar" ,(expand-file-name clipjar-location) "--uuid" ,(file-relative-name dir default-directory) ) " " )))))))

+END_SRC

The image can be identified with a caption attribute:

+BEGIN_SRC sh

+CAPTION: Power Supply Circuit Diagram.

file:Pictures/acb19f19-31c0-4550-874d-1111aafbb93f.png

+END_SRC

+CAPTION: Demonstration of org-paste-image

[[file:images/example-org-paste-image-uuid.gif]]

*** Client code 2

+BEGIN_SRC elisp

(defvar clipjar-location "~/bin/Clip.jar")

(defun org-paste-image2 () (interactive) (let* ((image-name (string-trim (read-string "Image name: "))))
(insert
(org-make-link-string (concat "file:" (shell-command-to-string (mapconcat #'identity `("java" "-jar" ,(expand-file-name clipjar-location) "--name" ,(concat "'" image-name "'") ;; image name without extension must be quoted "'/tmp/scala images'" ;; Directory which the image will be saved '/tmp/images scala' ) " " )))))))

+END_SRC

+CAPTION: Demonstration of org-paste-image2

[[file:images/example-org-paste-image2.gif]]

** Download Release

You can download a compilead binary release from:

The file is about 5 MB because it was compiled with the Scala run-time bundled with the app.

Or automatically with this bash script:

+BEGIN_SRC sh

mkdir -p ~/bin && cd ~/bin && curl -O -L https://github.com/caiorss/clip.jar/raw/build/Clip.jar

+END_SRC