Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1494  / 3 Years ago, wed, july 14, 2021, 1:03:22

I have a collection of small text files that I often want to copy wholesale into, say emails or the like. Ideally I'd like to add an option to the right click menu for these small text files that copies the whole contents of the file to clipboard ready to be pasted.



Or is there some smarter gadget that could manage these snippets? It has to be something that can paste the text into arbitrary texboxes: in firefox, in thunderbird, in emacs...



Is there such a tool?


More From » menu

 Answers
7

You could write a small perl script like the one I posted below and run it as a nautilus script.



It would work like this:




  • Mark the files in nautilus

  • Right click for context menu

  • Click 'Scripts' entry of the context menu then click the name of the script (in my case the name is 'contents2clipboard').



Now the contents of all files is in the clipboard an can be pasted into a text editor or any other application.



To use the script copy it into a new file in the ~/.gnome2/nautilus-scripts directory. The file's name will be the text of the corresponding entry in the 'Scripts' menu of nautilus. In my case the name is 'contents2clipboard'.



Here's a screenshot:



contents to clipboard script



Here is the very basic script:



#!/usr/bin/perl
# indent-mode: spaces, tabsize: 4, encoding: utf8
#
# © 2011 [email protected]. Use under the MIT license:
# http://www.opensource.org/licenses/mit-license.php
#
# This nautilus script copies the content of the selected files
#+into the clipboard. Nautlis scripts are usually located in
#+'$HOME/.gnome2/nautilus-scripts' and need to be executable.

# Gtk2 stuff
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';
use utf8;

# Get files list
@files = split( "
", $ENV{'NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'} );
$content = '';
# read content of files in list
foreach $path (@files) {
if( -T $path ) {
open FILE, "<$path";
$content .= do { local $/; <FILE> };
close(FILE);
}
}

# Copy content to clipboard
$clipboard = Gtk2::Clipboard->get(Gtk2::Gdk->SELECTION_CLIPBOARD);
$clipboard->set_text($content);
Gtk2->main;


As it is it has a few quirks you might want to fix:




  • It doesn't work on remote file systems because of the way NAUTILUS_SCRIPT_SELECTED_FILE_PATHS is handeld.

  • It doesn't check the size of the files provided. So very large files might cause trouble.



Don't know if this suits your needs. If so: enjoy! (and mark answered)


[#44008] Friday, July 16, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
corsee

Total Points: 479
Total Questions: 122
Total Answers: 106

Location: Barbados
Member since Sat, May 9, 2020
4 Years ago
;