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
  • 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, 2017 at 2:04

3 Answers 3

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.

2
  • Thank you,i am very appreciated,that was exactly what i was looking for.
    – elanozturk
    Jan 23, 2017 at 18:26
  • @elanozturk You're welcome, and thank you for the good question ! Jan 23, 2017 at 18:28
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.
5
  • 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. Jan 23, 2017 at 3:44
  • @WinEunuuchs2Unix, thanks will improve my answer but note just learning scripting :). Jan 23, 2017 at 5:14
  • 1
    @ George I've seen your many great posts and edits here in AU. I have an example of zenity here: askubuntu.com/questions/837078/…. Also for scripting in Nautilus here: askubuntu.com/questions/871312/… Jan 23, 2017 at 5:17
  • @WinEunuuchs2Unix the zenity is installed by default right and one just needs to use is as in the links you provided ? Jan 23, 2017 at 5: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. Jan 23, 2017 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.