Last active
March 18, 2016 23:41
-
-
Save korc/931c76933089b9907430 to your computer and use it in GitHub Desktop.
Image rename tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
from gi.repository import Gtk, GLib | |
class SignalHandler(object): | |
selected_model = None | |
def __init__(self, builder): | |
self.builder = builder | |
def on_quit(self, window, ev): | |
Gtk.main_quit() | |
def on_fname_edited(self, renderer, path, newval): | |
oldname=self.selected_model[path][0] | |
if oldname == newval: | |
return | |
copy_nr=0 | |
new_filename=newval | |
while os.path.exists(new_filename): | |
copy_nr+=1 | |
new_filename=newval.rsplit(".", 1) | |
if len(new_filename)>1: | |
new_filename="%s (%d).%s"%(new_filename[0], copy_nr, new_filename[1]) | |
else: new_filename="%s (%d)"%(new_filename[0], copy_nr) | |
newval=new_filename | |
os.rename(oldname, newval) | |
self.selected_model[path][0]=newval | |
def on_selected(self, treesel): | |
self.selected_model, rows=treesel.get_selected_rows() | |
if not len(rows): return | |
fname=self.selected_model[rows[0]][0] | |
image=self.builder.get_object("image") | |
image.set_from_file(fname) | |
def on_edit_start(self, renderer, entry, path): | |
try: dot_idx=entry.get_text().rindex(".") | |
except ValueError: return | |
GLib.timeout_add(50, lambda: entry.select_region(0, dot_idx)) | |
def main(): | |
bld=Gtk.Builder() | |
bld.add_from_file(__file__[:-3]+".ui") | |
bld.connect_signals(SignalHandler(bld)) | |
fname_store=bld.get_object("filenames") | |
for fname in os.listdir("."): | |
fname_store.append((fname,)) | |
Gtk.main() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- Generated with glade 3.18.3 --> | |
<interface> | |
<requires lib="gtk+" version="3.12"/> | |
<object class="GtkListStore" id="filenames"> | |
<columns> | |
<!-- column-name filename --> | |
<column type="gchararray"/> | |
</columns> | |
</object> | |
<object class="GtkWindow" id="window1"> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<signal name="delete-event" handler="on_quit" swapped="no"/> | |
<child> | |
<object class="GtkPaned" id="paned1"> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<child> | |
<object class="GtkScrolledWindow" id="scrolledwindow1"> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<property name="shadow_type">in</property> | |
<child> | |
<object class="GtkTreeView" id="treeview1"> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<property name="model">filenames</property> | |
<child internal-child="selection"> | |
<object class="GtkTreeSelection" id="treeview-selection1"> | |
<signal name="changed" handler="on_selected" swapped="no"/> | |
</object> | |
</child> | |
<child> | |
<object class="GtkTreeViewColumn" id="treeviewcolumn1"> | |
<property name="title" translatable="yes">column</property> | |
<child> | |
<object class="GtkCellRendererText" id="cellrenderertext1"> | |
<property name="editable">True</property> | |
<signal name="edited" handler="on_fname_edited" swapped="no"/> | |
<signal name="editing-started" handler="on_edit_start" swapped="no"/> | |
</object> | |
<attributes> | |
<attribute name="text">0</attribute> | |
</attributes> | |
</child> | |
</object> | |
</child> | |
</object> | |
</child> | |
</object> | |
<packing> | |
<property name="resize">False</property> | |
<property name="shrink">True</property> | |
</packing> | |
</child> | |
<child> | |
<object class="GtkScrolledWindow" id="scrolledwindow2"> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<property name="shadow_type">in</property> | |
<child> | |
<object class="GtkViewport" id="viewport1"> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<child> | |
<object class="GtkImage" id="image"> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<property name="stock">gtk-missing-image</property> | |
</object> | |
</child> | |
</object> | |
</child> | |
</object> | |
<packing> | |
<property name="resize">True</property> | |
<property name="shrink">True</property> | |
</packing> | |
</child> | |
</object> | |
</child> | |
</object> | |
</interface> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment