1

i would like to add nautilus script for right click,like convert selected file with base64 but no luck.Regular command in terminal is like:

base64 input.jpg output.txt

This is my closest script to do it;

#!/bin/sh
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_Path" | base64 > Base64

But it gives the path of file as base64. Can anybody help me about this?

  • 1
    I think your problem is related to echoing the path instead of usinf the variale content as an argument. Also your variable name has a typo, it's $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS (I think in you real script is ok, because you could obtain the file path). You can use base64 "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" > "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS".b64, but better take a look at proposed answers. – dgonzalez Jan 23 '17 at 02:04

3 Answers3

3

Your original script has a couple issues:

  • "$NAUTILUS_SCRIPT_SELECTED_FILE_Path" should be all capitalized and spelled differently, as in "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS".
  • echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | base64 will only encode the string "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" and that's it. If you want to encode a file, you need to pass that variable on via xargs or better yet - pass it directly. base64 can take files as arguments , thus base64 "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" is what you really want to do. No pipes here, no echo.

Below I provide alternative and fairly simplistic approach

Script

#!/bin/bash
for item in "$@"
do
   file=$(basename "$item" )
   base64 "$item" > "$HOME/$file.base64"
done

Instructions

  • Place this file into ~/.local/share/nautilus/scripts/ directory

  • ensure it is executable with chmod +x <path to script here> command.

  • Select the files that you want, right click , and go to scripts submenu. Select your script.

  • Once the the script is done, each base64 encoded file will be placed into your home folder, with original filename and .base64 extension. You may wish to alter the script to create .base64 files in the same directory, which is totally fine, but it might be a problem if you do not have permission to create files in that directory.

    Of course this script is slightly simplistic. I've created a bit more involved script in Python3 , which you can find on my GitHub repository for nautilus scripts. It does a bit more than the other scripts - checks for file type, show info and error popups,etc.

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
0

I am assuming you can already add custom script to nautilus and only need the right script to do the actual work, if that be the case then try this script:

#!/bin/bash
# Generating base64 files from nautilus
m=0 # counter
while [ -n "$1" ] && [ -f "$1" ] 
do
   echo " " >> output.txt     
   echo "$1" >> output.txt
   base64 "$1" >> output.txt
   echo " " >> output.txt
   m=$(($M + 1))
   shift
   echo " " >> output.txt           
done
zenity --info --text="Operations finished and "$m" file(s) encoded"
exit 0

Please make file executable with:

chmod +x /path/to/script

Explained:

  1. -n "$1" checks if the values are greater than 0, ie not 0
  2. -f "$1" checks if file exists and is regular file.
  3. shift moves through the given arguments
  4. m=$(($m + 1)) adds 1 to 0 initial count value to keep track of number of files being converted
  5. zenity displays a dialogue box to indicated end of operations.
George Udosen
  • 36,677
  • 2
    The echo won't let you see what is happening in the classic sense. You can find out after the fact by looking in /var/log/syslog. To see what is happening in real time you need to use zenity with --info option as the GUI interface from your script. – WinEunuuchs2Unix Jan 23 '17 at 03:44
  • @WinEunuuchs2Unix, thanks will improve my answer but note just learning scripting :). – George Udosen Jan 23 '17 at 05:14
  • 1
    @ George I've seen your many great posts and edits here in AU. I have an example of zenity here: http://askubuntu.com/questions/837078/application-that-will-lock-screen-after-a-set-amount-of-time-for-ubuntu/837115#837115. Also for scripting in Nautilus here: http://askubuntu.com/questions/871312/nautilus-can-set-desktop-wallpaper-how-can-it-set-login-and-lock-screen-wallpap/871313#871313 – WinEunuuchs2Unix Jan 23 '17 at 05:17
  • @WinEunuuchs2Unix the zenity is installed by default right and one just needs to use is as in the links you provided ? – George Udosen Jan 23 '17 at 05:49
  • 1
    I believe zenity is installed by default. It might be Debian / Ubuntu only default though. It is an upgrade to xmessage which is very difficult to use on high-dpi monitors. – WinEunuuchs2Unix Jan 23 '17 at 11:41
0

Thank you for all guys,i did two scripts with your help and suggestions for encode and decode.

Encode:

#!/bin/bash
for item in "$@"
do
   mkdir /yourpath/encode
   file=$(basename "$item" )
   base64 "$item" > "/yourpath/encode/$file-base64"
done

Decode:

for item in "$@"
do
   mkdir /yourpath/decode
   file=$(basename "$item" )
   name=`echo $file | cut -f1 -d'-'`
   base64 -d "$item" > "/yourpath/decode/$name"

done