Add password option to default compress menu - 20.04

when I click the compress option on a file or folder, by right clicking on that file and choosing compress, I am presented with a simple menu form that only lets me choose the type of file to convert to: zip, 7zip, tar.xz. Is it possible to add the option password to that simple menu so that I can convert the files protected with a password more quickly (without having to go to the menu and choosing the password option)? Thanks!


Solution 1:

I created a very basic script to call it as a Nautilus (File Manager) Action. You can add this script as an action to your File Manager by using the FileManager-Actions Configuration Tool.

Install the FileManager-Actions Configuration Tool using:

$ sudo apt install filemanager-actions

My script uses zenity to display an input form. If zenity is not already installed, you can install it using:

$ sudo apt install zenity

The script is like this:

#!/bin/bash

if (( $# != 1 )) ; then
  aname=""
else
  aname="$(echo "$1"|sed -e 's/\.[^./]*$//').7z"
fi
answer=$(zenity --forms --title="Create Compressed File" \
 --text="Archive Name: $aname" --add-entry="New Archive Name:" \
 --add-password="Password:" --add-password="Confirm Password:")
case "$?" in
  1) echo "Cancelled" >&2 ; exit 1 ;;
  -1) echo "Error!" >&2 ; exit -1 ;;
  0) IFS="|" read -r newaname pass1 pass2 <<< "$answer" ;;
esac
if [[ "$pass1" != "$pass2" ]] ; then
  echo "Passwords do no match!" >&2
  exit 2
fi
if [[ -z "$pass1" ]] ; then
  echo "No password is given!" >&2
  exit 3
fi
[[ "$newaname" == "" ]] && newaname="$aname"
if [[ -z "$newaname" ]] ; then
  echo "No archive name is given!" >&2
  exit 4
fi

7z -t7z -p"$pass1" a "$newaname" "$@"

You can put a copy of it somewhere in your path (and make it executable). I used compress_with_pass.sh as the file name for the script.

The script prompts you for the archive file name (if there is a single file to be compressed, it automatically replaces the extension of the file with .7z) and the password for the archive.

To add this script as a "File Manager Action" use the File-Manager Actions Configuration Tool you installed as I described above and add an action like this (only the fields that are important are shown here):

Action tab:

  • ✓ Display item in selection context menu
  • Context label: COMPRESS WITH PASSWORD

Command tab:

  • Path: gnome-terminal
  • Parameters: -- bash -c "compress_with_pass.sh %F ; read a"

Execution tab:

  • Execution mode: Normal
  • Change the other fields according to your needs.

After this, you can select one or more files in your File Manager and right click to access the action called: COMPRESS WITH PASSWORD.

Notes:

  • The script can be improved to allow compression for other formats.
  • You can use yad for more advanced script input/output (GUI).

Update for Ubuntu 20.10

Since the filemanager-actions package is no longer available in Ubuntu 20.10, we have to revert to Nautilus Scripts. I modified the above script accordingly and copied the new version below:

#!/bin/bash
# See: https://help.ubuntu.com/community/NautilusScriptsHowto

msg ()
{
  zenity --info --no-wrap --no-markup --text="$*"
}

aname=$(echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | head -1)
if [[ $aname != "" ]] ; then
  aname="$(echo "$aname"|sed -e 's/\.[^./]*$//').7z"
fi
answer=$(zenity --forms --title="Create Compressed File" \
 --text="Archive Name: $aname" --add-entry="New Archive Name:" \
 --add-password="Password:" --add-password="Confirm Password:")
case "$?" in
  1)  msg "Cancelled" ; exit 1 ;;
  -1) msg "Error!"    ; exit -1 ;;
  0)  IFS="|" read -r newaname pass1 pass2 <<< "$answer" ;;
esac
if [[ "$pass1" != "$pass2" ]] ; then
  msg "Passwords do no match!"
  exit 2
fi
if [[ -z "$pass1" ]] ; then
  msg "No password is given!"
  exit 3
fi
[[ "$newaname" == "" ]] && newaname="$aname"
if [[ -z "$newaname" ]] ; then
  msg "No archive name is given!"
  exit 4
fi

tmpfile=$(mktemp)
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" >"$tmpfile"
out1=$(echo 7z -t7z -p\""$(echo $pass1|sed 's/./*/g')"\" a \""$newaname"\" -i@"$tmpfile")
out2=$(7z -t7z -p"$pass1" a "$newaname" -i@"$tmpfile" 2>&1)

msg "$out1
___
$out2"

rm "$tmpfile"

You have to put the script under the directory ~/.local/share/nautilus/scripts with an appropriate name (say CompressWith_Pass) and make it executable (chmod +x ~/.local/share/nautilus/scripts/CompressWith_Pass).

After that, the usage is similar: You can select one or more files in your File Manager and right click to access the Scripts menu item having CompressWithPass as a sub-item.