Tuesday, August 14, 2012

Nautilus / Thunar Pastebin Script, all Ubuntus

You may or may not regularly use one of those handy Pastebin sites to easily post any text/script you want to share in a support forum or, more common, on IRC. To do that, without having to enter all the stuff manually on their respective websites, there is the equally lightweight and handy command line tool Pastebinit, but if you want to post a file directly from your file browser, with all the same options specifiable from there too, there is no easy way to do that.

So I've written a Nautilus script that makes use of Pastebinit and queries a couple of possible options before posting the selected file on 'pastebin.com', as of now, still the Pastebin with the most features, while also looking good, as far as I've checked at least.

Similarly to Pastebinit itself, the script is rather non-fancy and very straightforward, it queries all the available options for 'pastebin.com' for which the former offers command line options, which are just:
  • Name of Paste
  • Syntax Highlighting
  • Username on 'pastebin.com' (optional, if enabled)
  • Password on 'pastebin.com' (optional, if enabled)
General

If any of abovementioned details are not provided, particularly the first two, the script will just use the respective Pastebinit defaults for 'pastebin.com' present on your system, whether set system-wide or user-specific - by default, the former are:
  • Name of Paste: Linux Username
  • Syntax Highlighting: None
And in addition to those:
  • Expiration of Paste: 1 Month
  • Visibility of Paste: Public
Changing the Defaults

Any of those default values can be changed by either creating the file ".pastebinit.xml", or the directory ".pastebin.d" including the file that holds the defaults specifically for 'pastebin.com', both in the top-level of your home directory (as indicated by the dot in front of the names, they are hidden, so pressing Ctrl+H may be needed).

The former can be set up by just following Pastebinit's manpage, the latter is based on the files contained in the directory for the system-wide defaults, "/usr/share/pastebin.d" or "/etc/pastebin.d", respectively, depending on what version of Pastebinit you are using. In the file for 'pastebin.com's defaults, 'pastebin.com.conf', you'll find these entries, corresponding with the options "Expiration" and "Visibility", respectively, mentioned above:

private = 0
expiry = 1M

Possible values for "private" are:
  • "0" = Public (default)
  • "1" = Unlisted
  • "2" = Private (only with user account)
And possible values for "expiry" are:
  • "N" = Never
  • "10M" = 10 Minutes
  • "1H" = 1 Hour
  • "1D" = 1 Day
  • "1W" = 1 Week
  • "2W" = 2 Weeks
  • "1M" = 1 Month (default)
Prerequisites

First of all, you'll obviously need to install the tool Pastebinit the script is based on, if it isn't already, either via the Software Center, or via a Terminal with this command:

sudo apt-get install pastebinit

Usage

The script will automatically suggest the file name as the name of the Paste, you can change that, of course. In the next dialog, you can choose the syntax highlighting you want to set.

After confirming to post the chosen file with the set options, you'll get the resulting Pastebin URL displayed, which you can either just copy manually, or let the script send it directly to your system's clipboard (see below how to enable that).

You'll also get the option to open the Paste in your preferred web browser, particularly recommended to make sure it's not removed after 10 mins due to triggering captcha spam protection.

Options

There are two options built into the script that are disabled by default, because they are either rather rarely used, or not supported by default, in that order. The first is to enable the support for user accounts on 'pastebin.com', i.e. post via a registered user. If you opt for that, you can also save any of your login credentials in the script, to skip the respective individual query.

The second is to enable the use of the tool XSel to send the resulting Pastebin URL directly to your system's clipboard. XSel is not installed by default, so you'd need to install it first, either via the Software Center, or via a Terminal with this command:

sudo apt-get install xsel

Now, to enable any of the just mentioned options, open the script file, "~/.gnome2/nautilus-scripts/Pastebin It", with your preferred text editor (hidden directory, so pressing Ctrl+H may be needed), and just change the value for the respective option from "no" to "yes", optionally entering any of your 'pastebin.com' login credentials as well, then save it, obviously.

PASTEACCOUNT="no"       # set to "yes" to enable the querying of account info
PASTEUSER=""            # optional, to skip the individual query, if the above is enabled
PASTEPASS=""            # optional, to skip the individual query, if the above is enabled
XSELCLIPBOARD="no"      # set to "yes" to enable the use of 'xsel'

Lastly, if you want to use a syntax highlighting I haven't included in the list yet, you can either code it in yourself, by just editing the script, should be obvious enough, or just use the "Custom" option included in the list, and enter the code for the desired syntax highlighting manually into the empty code field there (the entries of every field are editable, btw). To get the corresponding code for the syntax name, I've used this list to compile my list, not sure if everyone of these is right, though:

"4cs": "4CS", "6502acme": "6502 ACME Cross Assembler", "6502kickass": "6502 Kick Assembler",
"6502tasm": "6502 TASM/64TASS", "abap": "ABAP", "actionscript": "ActionScript",
"actionscript3": "ActionScript 3", "ada": "Ada", "algol68": "ALGOL 68", "apache": "Apache Log",
"applescript": "AppleScript", "apt_sources": "APT Sources", "asm": "ASM (NASM)", "asp" : "ASP",
"autoconf": "autoconf", "autohotkey": "Autohotkey", "autoit": "AutoIt", "avisynth": "Avisynth",
"awk": "Awk", "bascomavr": "BASCOM AVR", "bash": "Bash", "basic4gl": "Basic4GL",
"bibtex": "BibTeX", "blitzbasic": "Blitz Basic", "bnf": "BNF", "boo": "BOO", "bf": "BrainFuck",
"c": "C", "c_mac": "C for Macs", "cil": "C Intermediate Language", "csharp": "C#",
"cpp": "C++", "cpp-qt": "C++ (with QT extensions)", "c_loadrunner": "C: Loadrunner",
"caddcl": "CAD DCL", "cadlisp": "CAD Lisp", "cfdg": "CFDG", "chaiscrip": "ChaiScript",
"clojure": "Clojure", "klonec": "Clone C", "klonecpp": "Clone C++", "cmake": "CMake",
"cobol": "COBOL", "coffeescript": "CoffeeScript", "cfm": "ColdFusion", "css": "CSS",
"cuesheet": "Cuesheet", "d": "D", "dcs": "DCS", "delphi": "Delphi",
"oxygene": "Delphi Prism (Oxygene)", "diff": "Diff", "div": "DIV", "dos": "DOS",
"dot": "DOT", "e": "E", "ecmascript": "ECMAScript", "eiffel": "Eiffel", "email": "Email",
"epc": "EPC", "erlang": "Erlang", "fsharp": "F#", "falcon": "Falcon", "fo": "FO Language",
"f1": "Formula One", "fortran": "Fortran", "freebasic": "FreeBasic", "freeswitch": "FreeSWITCH",
"gambas": "GAMBAS", "gml": "Game Maker", "gdb": "GDB", "genero": "Genero", "genie": "Genie",
"gettext": "GetText", "go": "Go", "groovy": "Groovy", "gwbasic": "GwBasic", "haskell": "Haskell",
"hicest": "HicEst", "hq9plus": "HQ9 Plus", "html4strict": "HTML", "html5": "HTML 5", "icon": "Icon",
"idl": "IDL", "ini": "INI file", "inno": "Inno Script", "intercal": "INTERCAL", "io": "IO", "j": "J",
"java": "Java", "java5": "Java 5", "javascript": "JavaScript", "jquery": "jQuery",
"kixtart": "KiXtart", "latex": "Latex", "lb": "Liberty BASIC", "lsl2": "Linden Scripting",
"lisp": "Lisp", "llvm": "LLVM", "locobasic": "Loco Basic", "logtalk": "Logtalk",
"lolcode": "LOL Code", "lotusformulas": "Lotus Formulas", "lotusscript": "Lotus Script",
"lscript": "LScript", "lua": "Lua", "m68k": "M68000 Assembler", "magiksf": "MagikSF", "make": "Make",
"mapbasic": "MapBasic", "matlab": "MatLab", "mirc": "mIRC", "mmix": "MIX Assembler",
"modula2": "Modula 2", "modula3": "Modula 3", "68000devpac": "Motorola 68000 HiSoft Dev",
"mpasm": "MPASM", "mxml": "MXML", "mysql": "MySQL", "newlisp": "newLISP", "text": "None",
"nsis": "NullSoft Installer", "oberon2": "Oberon 2", "objeck": "Objeck Programming Langua",
"objc": "Objective C", "ocaml-brief": "OCalm Brief", "ocaml": "OCaml", "pf": "OpenBSD PACKET FILTER",
"glsl": "OpenGL Shading", "oobas": "Openoffice BASIC", "oracle11": "Oracle 11", "oracle8": "Oracle 8",
"oz": "Oz", "pascal": "Pascal", "pawn": "PAWN", "pcre": "PCRE", "per": "Per", "perl": "Perl",
"perl6": "Perl 6", "php": "PHP", "php-brief": "PHP Brief", "pic16": "Pic 16", "pike": "Pike",
"pixelbender": "Pixel Bender", "plsql": "PL/SQL", "postgresql": "PostgreSQL", "povray": "POV-Ray",
"powershell": "Power Shell", "powerbuilder": "PowerBuilder", "proftpd": "ProFTPd",
"progress": "Progress", "prolog": "Prolog", "properties": "Properties", "providex": "ProvideX",
"purebasic": "PureBasic", "pycon": "PyCon", "python": "Python", "q": "q/kdb+", "qbasic": "QBasic",
"rsplus": "R", "rails": "Rails", "rebol": "REBOL", "reg": "REG", "robots": "Robots",
"rpmspec": "RPM Spec", "ruby": "Ruby", "gnuplot": "Ruby Gnuplot", "sas": "SAS", "scala": "Scala",
"scheme": "Scheme", "scilab": "Scilab", "sdlbasic": "SdlBasic", "smalltalk": "Smalltalk",
"smarty": "Smarty", "sql": "SQL", "systemverilog": "SystemVerilog", "tsql": "T-SQL", "tcl": "TCL",
"teraterm": "Tera Term", "thinbasic": "thinBasic", "typoscript": "TypoScript", "unicon": "Unicon",
"uscript": "UnrealScript", "vala": "Vala", "vbnet": "VB.NET", "verilog": "VeriLog", "vhdl": "VHDL",
"vim": "VIM", "visualprolog": "Visual Pro Log", "vb": "VisualBasic", "visualfoxpro": "VisualFoxPro",
"whitespace": "WhiteSpace", "whois": "WHOIS", "winbatch": "Winbatch", "xbasic": "XBasic", "xml": "XML",
"xorg_conf": "Xorg Config", "xpp": "XPP", "yaml": "YAML", "z80": "Z80 Assembler", "zxbasic": "ZXBasic"

Using it with Nautilus

To use the script with Nautilus, you can either download it manually as "Pastebin It" into the directory "~/.gnome2/nautilus-scripts" in your home directory (hidden, so pressing Ctrl+H may be needed), and make it executable via "right-click > Properties > Permissions", or just run these commands in a Terminal:

wget -t 5 -T 10 -O ~/.gnome2/nautilus-scripts/Pastebin\ It http://files.tuxgarage.com/pastebinit-gui
chmod +x ~/.gnome2/nautilus-scripts/Pastebin\ It

Manual download: http://files.tuxgarage.com/pastebinit-gui > save it as "Pastebin It"

After installation, you can call it straight away either in Nautilus directly or from your desktop (if you have it set to handle it) by right-clicking the file you want to pastebin, and choosing "Scripts > Pastebin It".

Using it with Thunar

You can just as well use the script with Thunar too, the default file browser of Xfce/Xubuntu. I recommend placing the script into a directory covered by the default "PATH" variable, e.g. "~/bin" or "/usr/local/bin", so you can run it easily from anywhere else as well, for example from the Terminal or via any Run dialog, just like the standalone described below, or else you'd also need to include its full path in the command below.

Similar as for Nautilus, you can either download it manually, for example into the directory "~/bin" in your home directory (hidden, so pressing Ctrl+H may be needed), and make it executable via "right-click > Properties > Permissions", or just run these commands in a Terminal:

mkdir ~/bin 2> /dev/null && export PATH=$HOME/bin:$PATH
wget -t 5 -T 10 -O ~/bin/pastebinit-gui http://files.tuxgarage.com/pastebinit-gui
chmod +x ~/bin/pastebinit-gui

Manual download: http://files.tuxgarage.com/pastebinit-gui

If you've chosen the manual way, and had to create the directory "~/bin" (as it doesn't exist by default), make sure to include it in the "PATH" variable, otherwise the script would only be found after the next login:

export PATH=$HOME/bin:$PATH

Then, to integrate it into Thunar, go to "Edit > Configure custom actions", and create a new entry for it, with "Pastebin It" as the name, and as the command:

pastebinit-gui %f

For its icon, I suggest, just like the standalone, "document-import". The defaults under "Appearance Conditions" are just fine, i.e. "File Pattern" set to "*", and the only checkmark at "Text Files". When done with that, you can also specify its position in the context menu, among the custom actions that are already there.

After setup, you can call it either in Thunar directly or from your desktop by right-clicking the file you want to pastebin, and choosing "Pastebin It".

Incidentally, I find this way of integration more proper, more nice, and more handy than the way it is in Nautilus.

Using it without File Browser

Additionally to using the script as a file browser extension, you can also run it separately, just from the menu or Dash. Obviously, you'll need a ".desktop" launcher file for that, placed into "~/.local/share/applications" (hidden), for example. Also, just like for Thunar, I recommend symlinking/placing the script into a directory covered by the "PATH" variable, e.g. "~/bin" or "/usr/local/bin", so you can run it easily from anywhere else as well, for example from the Terminal or via any Run dialog, or else you'd also need to modify its ".desktop" file to include its full path.

If you've already installed it as Nautilus script, we'll just symlink it to "~/bin"; or if you've already placed it into a directory covered by the "PATH" variable for use with Thunar, we'll just keep it that way; or else download it directly into "~/bin". You can either just run these commands to download the launcher file, and symlink or download the script, respectively; or manually download both files to the proper locations, and make the script executable:

mkdir ~/bin 2> /dev/null && export PATH=$HOME/bin:$PATH
[ -x ~/.gnome2/nautilus-scripts/Pastebin\ It ] && ln -s ~/.gnome2/nautilus-scripts/Pastebin\ It ~/bin/pastebinit-gui
[ ! -f ~/bin/pastebinit-gui ] && [ ! $(which pastebinit-gui) ] && wget -t 5 -T 10 -O ~/bin/pastebinit-gui http://files.tuxgarage.com/pastebinit-gui
[ ! -h ~/bin/pastebinit-gui ] && chmod +x ~/bin/pastebinit-gui
mkdir -p ~/.local/share/applications
wget -t 5 -T 10 -O ~/.local/share/applications/pastebinit-gui.desktop http://files.tuxgarage.com/pastebinit-gui.desktop

Manual download script: http://files.tuxgarage.com/pastebinit-gui
Manual download launcher: http://files.tuxgarage.com/pastebinit-gui.desktop

If you've chosen the manual way, and had to create the directory "~/bin" (as it doesn't exist by default), make sure to include it in the "PATH" variable, otherwise the script would only be found after the next login:

export PATH=$HOME/bin:$PATH

To also run it from the Terminal or via any Run dialog, as mentioned above already, just enter:

pastebinit-gui

All of the above methods, i.e. calling the script without passing any arguments to it, would result in a GUI file picker dialog coming up, so you can choose to file to pastebin interactively.

Passing Files directly to it

Alternatively to the above, you can also pass the file you want to pastebin directly to it. For that, if calling the script from the Terminal, you can either just append the relative path to the file from your current location, at the simplest just the file name; or the full path:

pastebinit-gui <relative-file-path>

If calling the script via some Run dialog, you must pass the full file path, because there is no working directory then, obviously:

pastebinit-gui <full-file-path>

Piping Output to it

And finally, you can also just pipe the output of some command to it, similar to the plain Pastebinit. For that, just do it as usual, but differently from the former, you need to append a "-" (minus) to it as a parameter:

<some-output-producing-command> | pastebinit-gui -

Also differently, you'll get to see the fed output yourself too, and it's also saved in a temporary file, so you can easily access it later again if needed, without having to run the respective commands again, for example if the pastebin failed for some reason. Of course, it would also tell you the full path to that file.

Other Linux / Known Issues

Generally, the script should work on all Linux systems that use Nautilus as the file manager. Zenity, which is used for the dialogs, should be installed on most, if not all, of those systems by default. Pastebinit and XSel, likewise, should be installable from the official repositories of most, if not all, Linux versions.

However, it has been reported that, on a Debian Sid system, any text copied from the dialog windows, particularly the resulting Pastebin URL, is lost from the system's clipboard once the respective window is closed, and the same person also reported back that using XSel didn't help either. Particularly the first mentioned behavior is very well known ever since the X window system exists, with various default or non-default applications, and I'm afraid I can't do anything about that, if a particular system is affected by that. What should help, though, is to just install any clipboard manager, I suggest Parcellite, which is strongly recommended on any system anyway.

Related Posts:

Accessibility , IRC