AmarokScripting

[00:03] <Riddell> time for our final talk!
[00:03] <Riddell> with special guest speaker sven423 from Amarok
[00:04] <Riddell> talking about Amarok Scripting
[00:04] <sven423> ok, hello everyone ^^
[00:04] <sven423> so, before I start the boring introduction: it would be a good idea if you have a recent version of Amarok installed
[00:05] <Riddell> instrictions here for jaunty users http://www.kubuntu.org/news/amarok-2.1
[00:05] <sven423> if you use svn: just fix the bugs you encounter during the next hour ;)
[00:05] <Riddell> karmic users can just apt-get install amarok
[00:06] <sven423> while downloading: the introduction
[00:06] <sven423> my name is Sven Krohlas, i'm in the Amarok team since 2006 or so, back then doing mainly promotion stuff
[00:07] <sven423> last year I started amarok scripting while integrating the Free Music Charts into Amarok
[00:07] <sven423> (HINT: http://kde-apps.org/content/show.php/Free+Music+Charts?content=91484 <-- cool script)
[00:07] <sven423> currently i'm working on a somewhat related GSoC project
[00:08] <sven423> oh, and the useualy stuff: i'm 26, study computer science in karlsruhe, germany
[00:08] <sven423> so, that should be enough time to install amarok, I hope ;)
[00:08] <sven423> well, now let's get to scripting
[00:09] <sven423> hehe, you won't need it in the first 10 seconds
[00:09] <sven423> so, basically there are two ways to enhance amarok with scripts: d-bus and qtscript
[00:10] <sven423> d-bus is the rather "boring" approach. there you can remote.control amarok
[00:10] <sven423> something like "qdbus org.kde.amarok /Player Play" would amarok start playing, for example
[00:11] <sven423> this is nice when you have to interact with other apps, but today we focus on the second approach: QtScript
[00:11] <sven423> == What's QtScript? ==
[00:12] <sven423> you all heard of ECMA, better known as JavaScript. qtscript is basically javascript with bindings to the qt libs
[00:12] <sven423> so you can use nearly all of qt in your amarok enhancements
[00:13] <sven423> the scripts you write get interpreted by the Amarok script engine, which also offers a lot of amarok specific apis
[00:13] <kwwii> americans and such will know it as javascript, ecma is the real name but mainly only known in europe
[00:13] <sven423> you have bindings for qtcore, qtxml (very important when parsing information sources), qtgui 8to create your own gui...)
[00:14] <sven423> qtnetwork, qtsql and, very cool, the qtuitools
[00:14] <sven423> with the uitools you can create your .ui files graphically in qtdesigner and then use them in amarok
[00:15] <sven423> so, that paart was very theoretical...
[00:15] <sven423> == What can be done with QtScript? ==
[00:15] <sven423> for example: have a look at the services in Amarok 2
[00:16] <sven423> the LibriVox service is completely written in qtscript
[00:16] <sven423> also the rather trivial "cool streams" service
[00:16] <sven423> or, if you installed some additional ones: the BBC and NPR integration, too
[00:17]  * Riddell finds /usr/share/kde4/apps/amarok/scripts/librivox_service/main.js
[00:17] <sven423> exactly
[00:17] <sven423> each script consists of at least 2 parts: the main.js and a script.spec file
[00:18] <sven423> the main.js contains the javascript sources, of course
[00:18] <sven423> the script.spec some infos amarok needs to know about the script
[00:18] <sven423> for example: the kind of script we have
[00:19] <sven423> the ones I mentioned up to now are all "scripted services"
[00:19] <sven423> they show up in the internet services area and integrate a service of some kind
[00:19] <sven423> other categroies are: lyrics and generic
[00:20] <sven423> lyrics scripts are here to integrate... SURPRISE... lyrics from different sources
[00:20] <sven423> and all the rest(TM) are generic scripts, like the alarm script
[00:20] <sven423> (which is a cool way to make a radio clock alarm device out of your quad core machine)
[00:21] <sven423> if you are not yet asleep: go to the script manager
[00:21] <sven423> there is one gerneric script called "script console"
[00:22] <sven423> activate it
[00:22] <sven423> the script console, itself a script, does nothing else but interpret script code you enter there
[00:22] <sven423> so it's a nice demo and debugging tool
[00:23] <sven423> enter: Amarok.Window.Statusbar.longMessage( "Hello #kubuntu-devel" );
[00:23] <sven423> this is a very trivial example of the amarok apis
[00:23] <sven423> just shows a small notification on the left down corner of the amarok window
[00:24] <kwwii> so how far does this control go?
[00:24] <sven423> Amarok.Engine.Play(); <-- starts playback
[00:24] <kwwii> seems pretty killer, just wondering where one could go with it
[00:24] <sven423> kwwii: well, a lot of stuff is possible
[00:25] <sven423> you could for example even send your own sql to the amarok database
[00:25] <sven423> Amarok Scripting API
[00:25] <sven423> http://amarok.kde.org/wiki/Development/Script_API
[00:25] <sven423> that's the current api ^
[00:25] <Kabal458> sven423: is editing tags possible?
[00:25] <kwwii> works here just fine...really nifty if you ask me
[00:26] <kanibal> Can I send "delete * from artist"???
[00:27] <sven423> kanibal: theoretically yes
[00:27] <cpk> Is there a difference in the level top which JavaScript vs. Python are supported for scripting? I've been playing with both (per the examples in SVN) and would much rather use Python than JavaScript.
[00:28] <sven423> kanibal: of course it's as with all code you run on your machine: don't run it if it comes from untrusted sources
[00:28] <sven423> cpk: with python you could do two thins: use d-bus or write scripted context applets
[00:29]  * sven423 REALLY wants to encourage everybode to use QtScript
[00:29] <sven423> due to several reasons:
[00:29] <sven423> * it runs out of the box, no additional dependencies (as long as you don't use QProcess to run another app)
[00:30] <sven423> (yeah, finally, my cat jumped onto the desk)
[00:30] <sven423> * it runs platform independent
[00:30] <sven423> so from a users point of view: it just works(TM)
[00:30] <sven423> and it also makes life easier for packagers
[00:31] <WielkieG> scriptConsoleMainWindow.historyList.addItem("Hello, World!");
[00:32] <WielkieG> it inserts a line into script console
[00:33] <WielkieG> so it's scriptable, I think :)
[00:33] <sven423> anyway ;)
[00:33] <sven423> well, the console already does some kind of meta-scripting
[00:34] <sven423> ok, some important apis:
[00:34] <sven423> each script gets interpreted in its own script engine
[00:34] <sven423> so if you want to use some aprts of qt use sth like:
[00:34] <sven423> Importer.loadQtBinding( "qt.xml" );
[00:35] <sven423> et voilĂ : you are able to use QDomDocument and similar stuff in your script
[00:35] <sven423> perfect for parsing feeds, xml, web sites, etc
[00:35] <sven423> but... before you can aprse sth you need to download it....
[00:36] <sven423> so here we have two things: the Downloader and the DataDownloader
[00:36] <sven423> a = new Downloader( xmlUrl, fmcShowsXmlParser ); <--- from the free music cahrts script
[00:37] <sven423> downloads from xmlUrl and calls fmcShowsXmlParser() when the download has finished
[00:37] <sven423> so you don't have to care about networking errors and similar ugly stuff
[00:37] <sven423> the Downloader is for test, the dataDownlaoder for binaries
[00:38] <sven423> *text
[00:38] <sven423> one rather big limitation currently is the influence scripts can have on the gui
[00:39] <sven423> you can add menu entries to the tools- and settings-menu
[00:39] <sven423> call the osd or put messages into the status bar
[00:39] <sven423> create a scripted service... but that's it atm
[00:39] <sven423> so sadly no context menus in the playlist, yet
[00:40] <sven423> btw, if anyone has questions: just ask ^^
[00:40] <Riddell> no scripted centre widgets then?
[00:40] <sven423> Riddell: NEW NEW NEW in 2.2 svn: yes, there they are possible ^^
[00:41] <sven423> but not yet in a released version
[00:41] <sven423> with those something like a karaoke applet could be done
[00:41] <sven423> iff there is a good source for lyrics with timestamps on the net
[00:41] <YenTheFirst> A question in the back of my mind: I never really scripted amarok 1.4, but I understand it could be scripted in ruby. Is that no longer true in 2?
[00:42] <sven423> YenTheFirst: 1.4 only had DCOP, the successor of D-BUS
[00:42] <sven423> and DCOP/D-BUS can be used from whatever you like
[00:44] <sven423> kanibal: only when calling external apps with QProcess or implementing the logic in the script...
[00:44] <sven423> but both would be more or less a hack
[00:44] <sven423> depending on what you want to do exactly
[00:44] <Kabal458> Another question: I can't seem to find the spec on the object returned from Amarok.Engine.currentTrack(), is there any way to get the members of this object?
[00:45] <sven423> Kabal458: good point, I guess atm you need to look it up in the sources
[00:46] <sven423> Kabal458: maybe the docs could be improved on that part...
[00:47] <WielkieG> write = function(str) { scriptConsoleMainWindow.historyList.addItem(str); }
[00:47] <WielkieG> list = function(obj) { for (x in obj) { write(x); } }
[00:47] <sven423> sharing is a good keyword: i'd also like to encourage every script developer to use our public SVN
[00:47] <sven423> we have a directory for contributed scripts there
[00:47] <sven423> amarok/playground/src/scripts
[00:47] <sven423> so you get code reviews and are notified early on api changes
[00:48] <Kabal458> how does the contribution process work?
[00:48] <sven423> Kabal458: currently... get an svn account, check it in... there are not yet many scripts sot ehre is not yet an established process
[00:49] <Kabal458> awesome :) how do we go about getting an svn account?
[00:49] <sven423> we use svn.kde.org
[00:49] <sven423> somewhere on kde.org the steps are outlined
[00:49] <Nightrose> Kabal458: there is a page with information on techbase.kde.org
[00:50] <Kabal458> thanks! alright I'll have a look
[00:50] <sven423> so, one nice feature i'd like to show: Amarok-Urls <--- I REALLY love those
[00:51] <sven423> open a service in amarok, like jamendo, the fmc, librivox, magnatune, etc
[00:51] <sven423> right click on an album -> add bookmark
[00:51] <sven423> or better: "bookmark this album"
[00:51] <sven423> then add the "bookmarks" applet to the context view
[00:51] <sven423> there you see the bookmark
[00:51] <sven423> select it
[00:52] <sven423> and in the address filed you see sth like:
[00:52] <sven423> amarok://navigate/service/Free%20Music%20Charts//%222009-05%20/%20May%202009%22
[00:52] <sven423> those urls can be used to navigate in amarok, send links to free albums to your friends, etc
[00:52] <sven423> also as position markers
[00:52] <sven423> inside a song
[00:53] <sven423> the service front page in the services context applet uses those
[00:53] <sven423> or the fmc-script to link to jamendo in the song details info
[00:54] <sven423> and yes, theoretically they can also be used in web sites to open a specific album in amarok
[00:54] <sven423> (but please make it so that other players still work, too)
[00:55] <sven423> so with that you can make cross-service features or integrate amarok with a web application
[00:56] <sven423> ok, those were the most important points on my cheat sheet ^^
[00:56] <sven423> questions, questions, questions ^^
[00:56] <Riddell> how hard is it to export a qtscript API from within an application?
[00:57] <sven423> oh, dammit, I just have to do such stuff for my GSoC project
[00:57] <sven423> as long as you want to export QObject based classes: quite easy
[00:57] <sven423> others are relatively hard, you need to write wrappers and suchs tuff
[00:58] <sven423> but for QObject based ones you can make all public slots available to a script within minutes
[00:58] <sven423> (once you understood how it goes)
[00:58] <sven423> the code for that is in ScriptManager.cpp in Amarok
[00:58] <sven423> also AmarokTest.cpp does that stuff
[00:59] <WielkieG> sven423: (a bit offtopic) What do you know about integrating QtWebKit's JSC into QtScript?
[01:00] <sven423> not much
[01:00] <sven423> I doN't know which js engine qt is using for scripts internally
[01:00] <sven423> but I think code sharing would really make sense
[01:01] <sven423> so maybe there is some webkit code in it... dunno
[01:01] <Riddell> it's been a packed five hours, well done to anyone who followed all the talks
[01:02] <sven423> so, I hope it was not too much information for one hour ^^
[01:02] <Riddell> and thanks very much to our speakers, sven423, kwwii, rgreening, apachelogger

Kubuntu/Archives/KubuntuTutorialsDay/AmarokScripting (last edited 2013-04-07 14:16:11 by myriam)