How do I make a custom right click command for nautilus? [duplicate]

Just save this file as 'Mark as executable', make itself executable and drop it to ~/.gnome2/nautilus-scripts It will be shown at your right-click menu, under the submenu Scripts

You may want to remove the gksudo from there if you are planning to mark as executable only files that you own (under your home directory), otherwise, every time that you run the script through the right click menu, it will ask you for password...

BUT the script you have up there is not going to work for files which contain spaces to their filename. In order to make it work for them, you have to alter the script to the following:

#!/bin/bash 

IFS_BAK=$IFS
IFS="
"

for line in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
   if [[ "$line" = "" || "$line" = " " ]]; then
      exit
   else
      chmod +x "$line"
   fi
done

IFS=$IFS_BAK
IFS_BAK=

ALSO, you can have your very own entry for your "Mark as executable" thingy. But, in order to enable this you will need to install the python-nautilus package:

sudo apt-get install python-nautilus

Then save this file as mark_as_executable.py under ~/.local/share/nautilus-python/extensions/ :

#!/usr/bin/env python
# -*- coding: utf8 -*-

from gi.repository import Nautilus, GObject, Gio
from os import system
import urllib

class BackgroundImageExtension(GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        print 'Mark as executable extensions is being initialized'

    def menu_activate_cb(self, menu, file):
        if file.is_gone():
            return
        system("chmod +x '"+urllib.unquote(file.get_uri()[7:])+"' &")

    def get_file_items(self, window, files):
        if len(files) != 1:
            return
        file = files[0]

        if file.get_uri_scheme() != 'file':
            return
        item = Nautilus.MenuItem(name='Nautilus::set_executable_bit', label='Mark as executable', tip='Add the executable bit to the selected file')
        item.connect('activate', self.menu_activate_cb, file)
        return item,

The above will only work for single files as well, if you want to add support for multiple files, you have to paste this inside the python script, though:

#!/usr/bin/env python
# -*- coding: utf8 -*-

from gi.repository import Nautilus, GObject, Gio
from os import system
import urllib

class BackgroundImageExtension(GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        print 'Mark as executable extensions is being initialized'

    def menu_activate_cb(self, menu, file):
        if file.is_gone():
            return
        system("chmod +x '"+urllib.unquote(file.get_uri()[7:])+"' &")

    def get_file_items(self, window, files):

        item = Nautilus.MenuItem(name='Nautilus::set_executable_bit', label='Mark as executable', tip='Add the executable bit to the selected file')
        for yourfile in files:
            if yourfile.get_uri_scheme() != 'file':
                return
            item.connect('activate', self.menu_activate_cb, yourfile)

        return item,

In order to be able to use the functionality, you will need to restart nautilus too:

nautilus -q

and then open a nautilus window.