Using Scripts in Caja (File Manager)

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
parsma

Using Scripts in Caja (File Manager)

Post by parsma »

Personally, I don't particularly like the Bash language. I prefer Python. So, here's how I use Python as a script engine. My goal was to be able to make copies of two image files, one or the other, and copy them to the current directory in Caja. This works for me. YMMV.
This is a Python script, written in Python 3.6.1, does not work in Python < 3.6.0. If you want to use it for lesser versions, replace the f-strings such:

Code: Select all

f'{Variable1}{Variable2}' => '{}{}'.format(Variable1, Variable2)
and it should work, even in 2.7.x.

Code: Select all

from sys import argv
from os import path
from shutil import copy

Path = '/home/<your_path>'
if '-i' in argv:
    File = 'important_file'
else:
    File = 'other_file'
New = f'{File}.txt'
fNum = 0
while path.exists(path.join('.', New)):
    fNum += 1
    New = f'{File}_{fNum}.txt'

copy(path.join(Path, f'{File}.txt'), New)
Save this as 'CopyInPython.py' and store it somewhere.
It seems, however, that we can't go completely without Bash, so here's what I do next:
Shell script one: "Copy Normal File to Current Directory" (no extension used)

Code: Select all

#! /usr/bin/env bash
/<your_path_to_python>/Python3/python /home/<your_path_to_python_script>/CopyInPython.py
Shell script two: "Copy Important File to Current Directory"

Code: Select all

#! /usr/bin/env bash
/<your_path_to_python>/Python3/python /home/<your_path_to_python_script>/CopyInPython.py -i
The two shell scripts will show up under the Scripts right-click menu in Caja file manager, provided they are stored in the "/home/<your_username>/.config/caja/scripts" directory.
This was just a simple example. I'm sure you can conjure up new ideas on how to use this recipe.
Python is, IMO, far easier to write than Bash scripts!
Have fun!
(Oh, and since I use a very old version, Caja might be able to run Python directly now, I don't know, I didn't bother looking before I posted this. Anyway, there might be people out there that use old versions, too. :)

Here's some additional code, for those who still live in the dark.

This copies filenames to the clipboard:

Code: Select all

#! /usr/bin/env python361
'''Script name: copy_filename.py'''
from sys import argv
from os import environ, path
import pyperclip

qs = ''
result = []
illegals = [' ', '!', '(', ')', '[', ']', '?', '*'] #add more of them as you find them

def make_escaped(what):
    ''''''
    result = ''
    for c in what:
        if c in illegals:
            result += '\\' + c
        else:
            result += c
    return result

if '-q' in argv:
    qs = '"'
sel_files = environ['CAJA_SCRIPT_SELECTED_FILE_PATHS'].strip()
if '-e' in argv:
    sel_files = make_escaped(sel_files)
if '-f' in argv:
    #only filename
    for f in sel_files.split('\n'):
        fn = path.split(f)[1]
        result.append(f'{qs}{fn}{qs}')
    pyperclip.copy('\n'.join(result).strip())
else:
    #full path
    fn = sel_files
    pyperclip.copy(f'{qs}{fn}{qs}')
Requirements:
Python 3.6+ (or you can rewrite it as previously stated for lesser versions)
Python package pyperclip (google "python pyperclip" and you should find it, if you don't want to bother with installing it, put its source script directory in the same directory as the Python script above, that ought to work)
The following Bash scripts (i.e., one for each command):

Code: Select all

#! /usr/bin/env bash
#Script name: Copy Escaped Filename to Clipboard
/home/<your path>/Python-3.6.1/python /home/<your path>/Programming/Python/CajaScripts/copy_filename_to_clipboard/copy_filename.py -f -e

Code: Select all

#! /usr/bin/env bash
#Script name: Copy Escaped Full Path to Clipboard
/home/<your path>/Python-3.6.1/python /home/<your path>/Programming/Python/CajaScripts/copy_filename_to_clipboard/copy_filename.py -e

Code: Select all

#! /usr/bin/env bash
#Script name: Copy Filename to Clipboard
/home/<your path>/Python-3.6.1/python /home/<your path>/Programming/Python/CajaScripts/copy_filename_to_clipboard/copy_filename.py -f

Code: Select all

#! /usr/bin/env bash
#Script name: Copy Full Path to Clipboard
/home/<your path>/Python-3.6.1/python /home/<your path>/Programming/Python/CajaScripts/copy_filename_to_clipboard/copy_filename.py

Code: Select all

#! /usr/bin/env bash
#Script name: Copy Quoted Filename to Clipboard
/home/<your path>/Python-3.6.1/python /home/<your path>/Programming/Python/CajaScripts/copy_filename_to_clipboard/copy_filename.py -f -q

Code: Select all

#! /usr/bin/env bash
#Script name: Copy Quoted Full Path to Clipboard
/home/<your path>/Python-3.6.1/python /home/<your path>/Programming/Python/CajaScripts/copy_filename_to_clipboard/copy_filename.py -q
I've not long ago discovered this feature in Caja (while looking through all the hidden stuff in my /home/ directory), so this is why I post all this crap in the first place, which most obviously don't care about. Oh, well. More time wasted...
Post Reply

Return to “Tutorials”