-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnautilus-new-file.py
105 lines (84 loc) · 3.68 KB
/
nautilus-new-file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright (C) 2018 Maximilian Schambach
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Nautilus extension to create new empty files.
"""
import os
try:
from urllib import unquote
except ImportError:
from urllib.parse import unquote
import gi
from gi.repository import Nautilus, GObject
class NewFileExtension(Nautilus.MenuProvider, GObject.GObject):
def __init__(self):
pass
def get_file_items(self, window, files):
pass
def get_background_items(self, window, file):
# Top Menu
top_menuitem = Nautilus.MenuItem(name='NewFileExtension::NewFileBG',
label='New File',
tip='',
icon='')
# Sub Menu
submenu = Nautilus.Menu()
# Sub Menu Item 1: New Empty File
sub_menuitem = Nautilus.MenuItem(name='NewFileExtension::NewFileEmptyBG',
label='New Empty File',
tip='',
icon='')
sub_menuitem.connect('activate', self.newfile_menu_cb, file)
submenu.append_item(sub_menuitem)
# Sub Menu Item 2: New Empty File
sub_menuitem = Nautilus.MenuItem(name='NewFileExtension::NewFileTxtBG',
label='New .txt File',
tip='',
icon='')
sub_menuitem.connect('activate', self.newtxt_menu_cb, file)
submenu.append_item(sub_menuitem)
# Sub Menu Item 3: New Empty File
sub_menuitem = Nautilus.MenuItem(name='NewFileExtension::NewFileOdsBG',
label='New .ods File',
tip='',
icon='')
sub_menuitem.connect('activate', self.newods_menu_cb, file)
submenu.append_item(sub_menuitem)
top_menuitem.set_submenu(submenu)
return top_menuitem,
# Define Item Actions
def _new_file(self, file):
# Replace %20 with whitespaces
filepath = unquote(file.get_uri()[7:])
filepath = os.path.join(filepath, "new_file")
os.system("touch '" + filepath + "'")
def _new_txt_file(self, file):
# Replace %20 with whitespaces
filepath = unquote(file.get_uri()[7:])
filepath = os.path.join(filepath, "new_file.txt")
os.system("touch '" + filepath + "'")
os.system("gnome-text-editor '" + filepath + "'")
def _new_ods_file(self, file):
# Replace %20 with whitespaces
filepath = unquote(file.get_uri()[7:])
filepath = os.path.join(filepath, "new_file.ods")
os.system("touch '" + filepath + "'")
os.system("soffice --writer '" + filepath + "'")
# Define Menu callbacks
def newfile_menu_cb(self, menu, file):
self._new_file(file)
def newtxt_menu_cb(self, menu, file):
self._new_txt_file(file)
def newods_menu_cb(self, menu, file):
self._new_ods_file(file)