This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Newbie Scripting questions

Tags: None
(comma "," separated)
User avatar
Max
Registered Member
Posts
28
Karma
0
OS

Newbie Scripting questions

Tue Jan 19, 2010 10:27 pm
I'm trying to write a script for 2.2.2 and I've never done this before, so please, bear with me.
1)In order to write to a file in Javascript the local environment has to support some kind of I/O object. What does KDE (or Amarok) have to offer me that I can use to write files?


Cogito ergo surf
I think therefore I network.

Registered Linux user #481826 Get counted!
Image
User avatar
Mamarok
Manager
Posts
6071
Karma
16
OS

Re: Newbie Scripting questions

Tue Jan 19, 2010 10:36 pm
Did you have a look at the Scripting Guide?

http://amarok.kde.org/wiki/Development/ ... _HowTo_2.0


Running Kubuntu 22.10 with Plasma 5.26.3, Frameworks 5.100.0, Qt 5.15.6, kernel 5.19.0-23 on Ryzen 5 4600H, AMD Renoir, X11
FWIW: it's always useful to state the exact Plasma version (+ distribution) when asking questions, makes it easier to help ...
User avatar
Max
Registered Member
Posts
28
Karma
0
OS

Re: Newbie Scripting questions

Tue Jan 19, 2010 10:38 pm
Yes, that's where I started from, unless I'm missing something I don't see a way to write files there.


Cogito ergo surf
I think therefore I network.

Registered Linux user #481826 Get counted!
Image
User avatar
markey
KDE Developer
Posts
2286
Karma
3
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 7:15 am
Well, you can access most of Qt too (not all of its modules are accessible, but many). Have a look here:

http://doc.trolltech.com/4.5/qfile.html


--
Mark Kretschmann - Amarok Developer
nhn
KDE Developer
Posts
114
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 8:21 am
something like:

file = new QFile( "path" );
if( file.open( QIODevice.ReadWrite ) )
{
Do stuff!
}

should work
User avatar
Max
Registered Member
Posts
28
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 9:18 am
Thanks, but it's not working.
When I tried doing this in the Script Console it didn't work.
My code:
Code: Select all
file=new QFile("test1.txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream out (&file);
out << Amarok.Playlist.totalTrackCount()<<"\n";
}
file.close();

The error:
Code: Select all
SyntaxError: Expected `;'`)'

What am I doing wrong?


Cogito ergo surf
I think therefore I network.

Registered Linux user #481826 Get counted!
Image
nhn
KDE Developer
Posts
114
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 9:24 am
One problem at least would be that you are using c++ syntax and not js syntax.

QTextStream out (&file);

should likely be

out = new QTextStream( file );

Also, I am not sure if the << operators work at all in js
User avatar
Max
Registered Member
Posts
28
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 9:47 am
Yeah, I thought it was looking a little funky.
I fixed the syntax (I hope) so that it's JS, but now I'm getting an "undefined" error.
Code: Select all
var file=new QFile("test1.txt");
file.open(new QIODevice.OpenMode(QIODevice.WriteOnly));
if (file.isOpen()){
var out = new QTextStream(file);
out.writeString(Amarok.Playlist.totalTrackCount()+"\n");
}
file.close();


Cogito ergo surf
I think therefore I network.

Registered Linux user #481826 Get counted!
Image
User avatar
Max
Registered Member
Posts
28
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 10:45 am
Boy do I feel foolish.
"undefined" was the output from the console, but it did create file!
I was just looking for it in the wrong place. For some reason it wrote the file to my Documents folder, not the parent folder of my music collection which is Music.
Is there any way to specify that I want the file to be written to the folder that holds the music collection without giving it a specific path?


Cogito ergo surf
I think therefore I network.

Registered Linux user #481826 Get counted!
Image
nhn
KDE Developer
Posts
114
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 10:49 am
Running it step by step in the script console works if I get rid of the "var" prefixes to the variables.

have you remembered to import qt.core ( Importer.loadQtBinding("qt.core"); ) in the script?
nhn
KDE Developer
Posts
114
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 10:53 am
use Amarok.Collection.collectionLocation();

Note however that this might return several paths if the collection is spread out between several top level directories.
User avatar
Max
Registered Member
Posts
28
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 11:16 am
That worked, thanks!
I didn't import the qtcore when I was testing my code in the console and it worked fine, but when I write the actual main.js I'll need to, right?


Cogito ergo surf
I think therefore I network.

Registered Linux user #481826 Get counted!
Image
nhn
KDE Developer
Posts
114
Karma
0
OS

Re: Newbie Scripting questions

Wed Jan 20, 2010 11:45 am
Yup!

As well as any other special modules that you might need (qt.network, qt.gui, ...)


Bookmarks



Who is online

Registered users: Bing [Bot], gfielding, Google [Bot], markhm, Sogou [Bot], Yahoo [Bot]