playCopy скрипт за GNOME
Слушайки почти постоянно музика и често коментирайки различни песни с познати в skype, icq и т.н. ми трябваше някакво решение, което да копира настоящата песен от плейъра и да я копира в активния прозорец. Едно от решенията за skype беше да използвам Skype4Py, но не ми хареса факта, че скрипта трябва да работи непрекъснато във фона, пък и скрипта, който написах неможеше да махне командата, с която да изпълни действието. Съответно в чат прозореца непрекъснато трябваше да пиша !np, за да покаже песента – накратко, доста нечисто решение, а и един процес в повече, колкото и малък е дразнещо. Решението с много по-малко код и използвайки стандартните библиотеки на python и опциите в GNOME се оказа поне за мен по-добро.
Принципа:
- Проверка за активния плейър (pa -A | grep <player>)
- Извличане на информацията за свирещата в момента песен
- Копиране на форматирания стринг в clipboard
Скрипта:
#!/usr/bin/env python # # playCopy 0.3 (python script) # # Copyright 2009 Vladimir Kolev # # 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. # # Special thanks to Umang for the changes in the script. # See section 4. in INSTALL file for the changes # import os import commands def cur_song(): """Attempts to find the name of the song playing by checking which music player is running and retreiving the song name from the application. Players currently supported: Rhythmbox, mocp, Exaile, Banshee""" if "banshee" in os.popen('ps -A | grep banshee').readline(): # Get the banshee artist and title and replace the unneccessery tiles from the string artist = os.popen('banshee --query-artist').readline().replace('artist: ', '').replace('\n', '') title = os.popen('banshee --query-title').readline().replace('title: ', '').replace('\n', '') song = "%s - %s" % (artist, title) elif "exaile" in os.popen('ps -A | grep exaile').readline(): # Get the exaile artist and title and replace the unneccessery tiles from the string artist = os.popen('exaile --get-artist').readline().replace('artist: ', '').replace('\n', '') title = os.popen('exaile --get-title').readline().replace('title: ', '').replace('\n', '') song = "%s - %s" % (artist, title) elif "rhythmbox" in os.popen('ps -A | grep rhythmbox').readline(): # Get the rhythmbox current playing information and set it to artist song = os.popen('rhythmbox-client --print-playing').readline() elif "mocp" in os.popen('ps -A | grep moc').readline(): # Get the information from mocp info = commands.getoutput("mocp --info").splitlines() if info == ["State: STOP"]: #If mocp is stopped, then change the artist to Nothing to show song = "moc is stopeed" else: # If there is a song information split it and formatid for the final string artist = info[3].replace('Artist:', '') title = info[4].replace('SongTitle:', '') song = "%s - %s" % (artist, title) else: song = "No supported player running" return song if __name__ == "__main__": import pygtk import gtk # Define the clipboard clipboard = gtk.clipboard_get() # Get the song name from cur_song(), copy the string to the clipboard and store it clipboard.set_text(cur_song()) clipboard.store()
Изтегляне:
playCopy (70)
Изходния код можете да откриете и изтеглите и от launchpad. Със следната команда:
bzr branch lp:playcopy
Конфигуриране и инсталиране:
Инсталацията не е никак сложна:
- Разархивирайте архива и направете playCopy изпълним (chmod +x playCopy)
- Копирайте playCopy в /usr/bin
- Стартирайте gconf-editor и навигирайте до apps -> metacity -> keybindings_commands
- За command_1 задайте стойност playCopy
- Навигирайте до apps -> metacity -> global_keybindings
- За run_command_1 задайте стойност <Control><Alt>c (или желана от вас комбинация, но не забравяйте < и >)
Поддържаниете приложения са:
Това е! Приемам предложения за още плейъри и за оптимизиране
# Get the information from mocp
info = commands.getoutput(„mocp –info“).splitlines()
if info == ["State: STOP"]:
#If mocp is stopped, then change the artist to Nothing to show
artist = „Nothing to show!“
title = „“
else:
# If there is a song information split it and formatid for the final string
artist = info[3].replace(‘Artist:’, “)
title = info[4].replace(‘SongTitle:’, “)

