Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Possible Duplicate:
How to customize the context menu in Nautilus?

I'm trying to make a Mark as executable command for the right click in nautilus for a faster work flow. I came up with this script:

#! /bin/bash
for file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
gksudo "chmod +x $file" &
done

How do I get this to work though. I currently have a .sh file and have no idea where to put it.

share|improve this question

marked as duplicate by Jorge Castro, Mitch, Takkat, Jacob Johan Edwards, Tom Brossman Sep 11 '12 at 20:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
See here for Nautilus Actions: askubuntu.com/questions/21953/… –  Takkat Sep 11 '12 at 11:24

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
Okay thanks, is there a way to have it display in it's own submenu without "scripts" that way it looks more "native" in a sense? –  Matthew Sep 11 '12 at 11:20
    
Of course there is, I will add this to my answer shortly –  hakermania Sep 11 '12 at 11:20
    
This may be a dumb question, but they use /usr/bin/script can I simply mkdir /usr/administrative_tools/mark_as_excutive.sh? Also, I shouldn't need any parameters should I? –  Matthew Sep 11 '12 at 11:31
    
I fail to understand your question or how it is related to your problem :/ See my edited answer for info on how to add this right-click option directly to your menu. –  hakermania Sep 11 '12 at 11:54
    
You answered my question perfectly. I was confused all together, but with your updated answer I fixed the issue. Thank you very much for your time. :) –  Matthew Sep 11 '12 at 12:12

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