Archive

Posts Tagged ‘clipboard’

Put a text on the clipboard from your webpage

December 18, 2012 Leave a comment

Problem
From an HTML page you want to copy some text on the clipboard by pressing a button.

Example: on a page you present a list of URLs. Next to each URL there is a button. If the user clicks on the button, the corresponding URL is copied to his/her clipboard.

Solution
You can use clippy for this task. “Clippy is a very simple Flash widget that makes it possible to place arbitrary text onto the client’s clipboard.

Here is an HTML template that you must paste in your HTML: clippy.html. Simply replace “{{ clippy_text }}” and “{{ bgcolor }}” with the values you want.

Update (20130103)
GitHub also used clippy but recently they switched to ZeroClipboard. Here is their announcement.

Categories: html Tags: , ,

Show the absolute path of a file

March 17, 2011 Leave a comment

When working in a terminal, a common task is to get the absolute path of a file. For instance, you want to open the given file with another (already running) application.

Old method:

Well, to get the absolute path of a file, I was doing the following:

C:\usr\local\lib\python2.6\dist-packages> ls -al
total 32
drwxrwsr-x  4 root staff 4096 2011-03-16 14:41 .
drwxrwsr-x  4 root staff 4096 2010-10-25 21:22 ..
drwx--S--- 10 root staff 4096 2010-09-29 09:30 pyglet
-rw-------  1 root staff 1039 2010-09-29 09:30 pyglet-1.1.4.egg-info
drwx--S---  2 root staff 4096 2011-03-16 14:41 termcolor-1.1.0.egg-info
-rw-r--r--  1 root staff 5044 2011-03-16 14:41 termcolor.py
-rw-------  1 root staff 3672 2011-03-16 14:41 termcolor.pyc
C:\usr\local\lib\python2.6\dist-packages> pwd
/usr/local/lib/python2.6/dist-packages
C:\usr\local\lib\python2.6\dist-packages>

First, find the file (termcolor.py in this example). Second, get the current working directory with the pwd command. Third, copy and paste the current working directory. Finally, copy and paste the filename. At each copy/paste, I had to switch between the terminal and the other application where I needed the absolute path. Too much work…

New method:

C:\usr\local\lib\python2.6\dist-packages> ls -al
total 32
drwxrwsr-x  4 root staff 4096 2011-03-16 14:41 .
drwxrwsr-x  4 root staff 4096 2010-10-25 21:22 ..
drwx--S--- 10 root staff 4096 2010-09-29 09:30 pyglet
-rw-------  1 root staff 1039 2010-09-29 09:30 pyglet-1.1.4.egg-info
drwx--S---  2 root staff 4096 2011-03-16 14:41 termcolor-1.1.0.egg-info
-rw-r--r--  1 root staff 5044 2011-03-16 14:41 termcolor.py
-rw-------  1 root staff 3672 2011-03-16 14:41 termcolor.pyc
C:\usr\local\lib\python2.6\dist-packages> sp termcolor.py
/usr/local/lib/python2.6/dist-packages/termcolor.py
C:\usr\local\lib\python2.6\dist-packages> sp termcolor.py | tocb
C:\usr\local\lib\python2.6\dist-packages>

The script “sp” (see below) takes one argument (a file name) and it prints the concatenation of the current directory and the file name, i.e. the absolute path of the file. Of course, you need to use the script in the directory where the given file is located. If no argument is given, it will print the current working directory.

If your goal is to paste the absolute path to somewhere else, you can copy the output of sp to the clipboard with tocb.

The script

The name of the script stands for “show path”.

#!/usr/bin/env python

# sp.py ("show path")

import os
import os.path
import sys

if len(sys.argv) == 1:
    print os.getcwd()
else:
    print os.path.join( os.getcwd(), sys.argv[1] )

Update (20110402)

The script is pushed to GitHub: https://github.com/jabbalaci/Bash-Utils.

Categories: bash, python Tags: , , , ,

Copy and paste the output of a program

February 24, 2011 Leave a comment

It is a common task to copy the output of a program/script and paste it somewhere else. If the output is short (just some lines), we can select the text easily with the mouse. What about longer outputs?

Old method

Until now I’ve been using the following method:

  • ./produce-long-output >a (redirect the output to a temporary file)
  • gedit a (open the temp file with a text editor)
  • CTRL+A (select the whole text)
  • copy selected text to the clipboard
  • paste text to somewhere else
  • rm a (remove the temp file)

New method

Recently, I discovered some tools (xclip and xsel) that can copy a text to the clipboard. With them, it is unnecessary to use a temporary file.

./produce-long-output | xsel

It’ll copy the text to the “primary” clipboard that you can paste with the middle mouse button. If you prefer pasting with Shift + Insert, then copy texts to the 3rd clipboard (it is simply called “clipboard”).

./produce-long-output | xsel -b

Life has become a little bit less complicated :)

Categories: bash Tags: , , , ,

Copy string to X clipboard in shell

February 24, 2011 6 comments

This entry is based on the post Copy Shell Prompt Output To Linux / UNIX X Clipboard Directly.

Problem

Under Linux, you want to copy a given text to the X clipboard. How to do that?

Three different clipboards

First of all, we should know that under X there are three clipboards :)

  • “primary”: The primary one’s paste operation is usually accessed with the middle mouse button.
  • “secondary”: This one didn’t really work for me…
  • “clipboard”: Its paste operation is accessed with Shift + Insert (or right mouse click and Paste in popup menus).

The xclip command

Reads from standard in, or from one or more files, and makes the data available as an X selection for pasting into X applications. Prints current X selection to standard out.

Installation:

sudo apt-get install xclip

Copy a string to the clipboard. By default, xclip copies to the “primary” clipboard.

echo "this is a test" | xclip
echo "this is a test" | xclip -selection primary
echo "this is a test" | xclip -selection p

The three variations are equivalent.

Copy a string to the “clipboard” (3rd clipboard).

echo "another test" | xclip -selection clipboard
echo "another test" | xclip -selection c

The two variations are equivalent.

Copy the contents of a file to the clipboard.

cat file.txt | xclip

Print the contents of the clipboard to the standard output.

xclip -o

Of course, you can use redirection.

xclip -o > out.txt

The xsel command

Retrieve and set the X selection.

xsel is similar to xclip, but it offers some additional features like append, clear, etc.

Installation:

sudo apt-get install xsel

Copy a string to the primary clipboard (default).

echo "1" | xsel
echo "1" | xsel -p

The two variations are equivalent.

Copy a string to “clipboard” (3rd clipboard).

echo "1" | xsel -b

Copy the contents of a file to the clipboard.

cat file.txt | xsel

Print the contents of the clipboard to the standard output.

xsel -o

Homework

Find a solution to copy a string to the “primary” and “clipboard” clipboards, i.e. on both. Hint: “echo homework | xsel -p -b” and “echo homework | xclip -selection p -selection c” won’t work. Post your solution in the comments.

Categories: bash Tags: , , ,
Follow

Get every new post delivered to your Inbox.

Join 62 other followers