Linux Lite Forums

Development => Scripting and Bash => Topic started by: misko_2083 on June 18, 2015, 11:29:32 AM

Title: Thunar custom action: audio files
Post by: misko_2083 on June 18, 2015, 11:29:32 AM
Here is how this works.
(http://i.imgur.com/tyUskEg.png)
You can select multiple audio files or folders.
After that this window will open.
(http://i.imgur.com/7x9ir6k.png)
Instuctions
Save the file to /usr/local/bin/zoose-audio

Setting thunar custom action:
Name: Audio options
Description : Manipulate Audio Files
Command: python3 /usr/local/bin/zoose-audio "%F"
 
File Pattern: *
Appearance: Directories, Audio files

Code: [Select]
# Thunar custom actions launcher for the audio files
#  Milos Pavlovic 2015
# (Based on the control panel concept of  Johnathan "ShaggyTwoDope" Jenkins)
#
# Save this file to /usr/local/bin/zoose-audio
# Setting thunar custom action:
# Name: Audio options
# Description : Manipulate Audio Files
# Command: python3 /usr/local/bin/zoose-audio "%F"

# File Pattern: *
# Appearance: Directories, Audio files
#
#  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 2 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, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.

#
#

zoose_icons=[
["name","icon","command"],
["Audacious","audacious",'audacious'],
["Enqueue in Audacious","audacious",'audacious -e'],
["Clementine","clementine",'clementine -l'],
["Enqueue in Clementine","clementine",'clementine -a'],
["SMPlayer","smplayer",'smplayer'],
["Enqueue in  SMPlayer","smplayer",'smplayer -add-to-playlist'],
["VLC","vlc",'vlc'],
["Qmmp","qmmp",'qmmp'],
["Enqueue in Qmmp","qmmp",'qmmp -e'],
["Convert","soundconverter",'/usr/bin/soundconverter'],
["Bulk Rename (Thunar)","thunar",'/usr/bin/thunar --bulk-rename'],
]


import os
import sys
import string
try:
    from gi import pygtkcompat
except ImportError:
    pygtkcompat = None

if pygtkcompat is not None:
    pygtkcompat.enable()
    pygtkcompat.enable_gtk(version='3.0')

from gi.repository import Gtk as gtk
from gi.repository.GdkPixbuf import Pixbuf



class Startup:

    def destroy(self, widget, data=None):
        # zoose mode
        gtk.main_quit()


    def valtamaction(self, widget, event, data=None):
        if event.button==1:
           pathinfo=widget.get_path_at_pos(int(event.x),int(event.y))
           if pathinfo==None: return False
           pathnr=pathinfo[0] 
           comm=" %s" % str(sys.argv[1])
           execc="%s" % self.liststore[pathnr][2]
           command=self.liststore[pathnr][2] + comm
           # case for audacious, if not found displays dialog
           if (execc=='audacious' or execc=='audacious -e'):
              if not os.path.exists('/usr/bin/audacious'):
                 dialog = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK, "apt-get install audacious")
                 result = dialog.run()
                 dialog.destroy()
                 command=''
           # case for clementine, if not found displays dialog
           elif (execc=='clementine -a' or execc=='clementine -l'):
              if not os.path.exists('/usr/bin/clementine'):
                 dialog = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK, "apt-get install clementine")
                 result = dialog.run()
                 dialog.destroy()
                 command=''
           # case for soundconverter, if not found displays dialog
           elif execc=='/usr/bin/soundconverter':
              if not os.path.exists('/usr/bin/soundconverter'):
                 dialog = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK, "apt-get install soundconverter")
                 result = dialog.run()
                 dialog.destroy()
                 command=''
           # case for smplayer, if not found displays dialog
           elif (execc=='smplayer' or execc=='smplayer -add-to-playlist'):
              if not os.path.exists('/usr/bin/smplayer'):
                 dialog = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK, "apt-get install smplayer")
                 result = dialog.run()
                 dialog.destroy()
                 command=''
           # case for qmmp, if not found displays dialog
           elif (execc=='qmmp' or execc=='qmmp -e'):
              if not os.path.exists('/usr/bin/qmmp'):
                 dialog = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK, "apt-get install qmmp")
                 result = dialog.run()
                 dialog.destroy()
                 command=''
           if command=='':
               gtk.main_quit()
           os.system(command+' &')
           gtk.main_quit()
           return True
        return False

    def __init__(self):
        # create the window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window=window
        window.set_title('File actions')
        window.connect("destroy", self.destroy)
        window.set_border_width(5)
        window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        # we make the icons
        it=gtk.icon_theme_get_default()
        gtk.window_set_default_icon(it.load_icon("gtk-preferences",48,gtk.ICON_LOOKUP_FORCE_SVG))
        window.resize(740,400)
        self.liststore=gtk.ListStore(gtk.gdk.Pixbuf,str,str)
        self.iv=gtk.IconView(self.liststore)
        self.iv.set_pixbuf_column(0)
        self.iv.set_text_column(1)
        self.iv.set_events(self.iv.get_events() | gtk.gdk.BUTTON_PRESS_MASK)
        self.iv.connect("button-press-event", self.valtamaction)
        sw = gtk.ScrolledWindow()
        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        sw.add(self.iv)
        window.add(sw)
        first=True
        for line in zoose_icons:
            if first:
               first=False
               continue 
            try:
               if '/' in line[1]:
                  pixbuf=gtk.gdk.pixbuf_new_from_file(line[1])
               else:
                  pixbuf=it.load_icon(line[1],48,gtk.ICON_LOOKUP_FORCE_SVG)
            except:
               pixbuf=it.load_icon('audio-speakers',48,gtk.ICON_LOOKUP_FORCE_SVG)
            namen=(line[0])
            self.liststore.append([ pixbuf,namen,line[2] ])
        window.show_all()


    def main(self):
        # Cliche init
        gtk.main()


# I swear zoose this better work
if __name__ == "__main__":

     app = Startup()
     app.main()
Title: Re: Thunar custom action: audio files
Post by: Wirezfree on June 18, 2015, 01:28:51 PM
Nice one misko...  :)

Just a thought for the future..??
You, and others have provided many useful "Thunar Custom Actions"

Would it be useful in "Lite Tweaks" to have an option to "Add Thunar Custom Actions"
This could then install a useful selection/collection of these all in one go.??

Just a thought.

Dave
Title: Re: Thunar custom action: audio files
Post by: misko_2083 on June 19, 2015, 10:11:54 AM
Hi Dave,
This is a python application. I thought it would be easier to spot the custom action this way if there are lots of them in the right-click menu.
Also it adds several custom actions so it's easier to add one that runs several.
This has a downside, of course. It adds one more click to the custom actions.
I don't see a way to add a custom action to the thunar automatically because thunar seems to add a unique id to the custom actions.
Would probably have to make a plugin which would handle the file types and pass the files/paths to the application. That is if I would know how to do that. :)

I've also made another version for single files that runs some of my zenity scripts.
https://plus.google.com/u/0/photos/110692908086784906343/albums/6161115991544673649/6161115995583284098?pid=6161115995583284098&oid=110692908086784906343 (https://plus.google.com/u/0/photos/110692908086784906343/albums/6161115991544673649/6161115995583284098?pid=6161115995583284098&oid=110692908086784906343)
Title: Re: Thunar custom action: audio files
Post by: Wirezfree on June 19, 2015, 12:01:17 PM
Hi Misko,


Thanks... understood.
Maybe there could be a "Adding Thunar Custom Actions" added to the "Help/User Guide" at some point.
Just a thought.


Dave
Title: Re: Thunar custom action: audio files
Post by: misko_2083 on June 20, 2015, 03:06:07 PM
Maybe.
Thanks for suggesting Dave.