|
Scripting_QtScript_Example_Scripts
Example ScriptsSimple script that adds a button on the toolbar and executes the Konsole program when the button is clicked:
Import("qt.core");
Import("qt.gui");
function actionClicked(){
shellExec("konsole");
}
var a = new MainWindowScript();
a.addToolButton("Konsole", "Konsole", new QIcon(SCRIPTS_PATH+"konsole/konsole.png"));
MainWindow.ToolBar.Konsole.triggered.connect(actionClicked);
function deinit(){//Used by hot-unloading
a.remToolButton("Konsole");
}
auth=Andrey Karlov name=Konsole desc=Simple script that runs Konsole icon=konsole.png Script that adds s button on the toolbar and sends Now-Play (for Amarok) information in the active hub frame when the button is clicked:
Import("qt.core");
Import("qt.gui");
function shellDone(ok, msg){
var hubMgr = new HubManager();
var activeHub = hubMgr.getHubObject();
if (ok && (activeHub != null))
activeHub.sendMsg(msg);
}
function actionClicked(){
var shell = new ShellCommandRunner(SCRIPTS_PATH+"amarok_nowplay/amarok.sh")
shell["finished(bool,QString)"].connect(shellDone);
shell.run();
}
var a = new MainWindowScript();
a.addToolButton("Amarok", "Amarok", new QIcon(SCRIPTS_PATH+"amarok_nowplay/amarok.png"));
MainWindow.ToolBar.Amarok.triggered.connect(actionClicked);
function deinit(){//Used by hot-unloading
a.remToolButton("Amarok");
}
auth=Andrey Karlov name=Now playing: Amarok desc=Now playing icon=amarok.png
message="/me is listening silence now" nowPlaying=$(qdbus org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata 2>/dev/null) if [ -n "$nowPlaying" ] then title=$(echo "$nowPlaying" | sed -ne 's/^title: \(.*\)$/\1/p') artist=$(echo "$nowPlaying" | sed -ne 's/^artist: \(.*\)$/\1/p') message="/me is listening now: $artist - $title" fi echo "$message" Script that add menu on menubar and print "Boom!" message on console when action Boom! is triggered:
Import("qt.core");
Import("qt.gui");
function boomTriggered() {
print("Boom!");
}
function checkMainWindowScript(){
var a = new MainWindowScript();
var menu = new QMenu("TestCase");
print(a.addMenu(menu));
var boom = MainWindow.MenuBar.TestCase.addAction("Boom!");
MainWindow.MenuBar.TestCase.Boom = boom;
MainWindow.MenuBar.TestCase.Boom["triggered()"].connect(boomTriggered);
}
checkMainWindowScript();The following is the !Eiskalt script part of a larger unrelated personal notification system to be made aware about PMs and hubchat highlights. It is missing the associated bash script that is called (arguably everything could be written in QtScript with the available Qt libraries imported) - it is an example of the following concepts:
// Licensed under GPLv3+: http://www.gnu.org/licenses/gpl.html
// (c) Omega Weapon 2011 (OmegaPhil+Eiskalt at gmail dot com)
// Variable allocation
scriptName = 'Status Script';
separator = ' - ';
bashInterfaceScript = '/home/omega/.config/eiskaltdc++/scripts/status_script/interface.bash';
// Defining functions
function returnError(error)
{
// Validating error object
if (!(error instanceof Error))
{
return 'returnError was passed an invalid object: ' + error;
}
// Returning error details
return error.name + ': ' + error.message + '\n\nFile name: ' + error.fileName + '\nLine number: ' + error.lineNumber;
}
function shellJobComplete(shell, output, messageType, hubFrame, CID)
{
// Validating parameters
if (shell == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Called without passing a shell');
return 1;
}
if (output == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Called without passing a output. Parameters: \'' + arguments + '\'');
return 1;
}
if (messageType == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Called without passing a messageType. Parameters: \'' + arguments + '\'');
return 1;
}
if (hubFrame == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Called without passing a hubFrame. Parameters: \'' + arguments + '\'');
return 1;
}
// Dealing with PM-specific parameters
if (messageType == 'TEXT' && CID == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Called without passing a CID. Parameters: \'' + arguments + '\'');
return 1;
}
// Returning error if command failed
if (shell.exitCode() != 0)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Command failed\n\nExit code: ' + shell.exitCode() + '\nError: \'' + output + '\'');
return 1;
}
// Exiting if no message is to be returned to user
if (output == '') { return; }
// Obtaining hub details
hubURL = hubFrame.getHubUrl();
hubName = hubFrame.getHubName();
// Dealing with different message types
switch(messageType)
{
// PM
case 'TEXT':
// PMing user
try
{
var clientManagerScript = new ClientManagerScript();
clientManagerScript.sendPM(CID, hubURL, output);
// Debug code
print("PM sent");
}
catch(error)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Unable to PM user ' + CID + ' on hub ' + hubName + ' (' + hubURL + ') the following message:\n\nMessage: ' + output + '\n\n' + returnError(error));
return 1;
}
break;
// No other message types require feedback currently
}
}
function handleMessage(varMap, hubFrame, messageType)
{
// Validating parameters
if (varMap == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Called with null varMap');
return 1;
}
if (hubFrame == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Called with null hubFrame. Parameters: \'' + arguments + '\'');
return 1;
}
if (messageType == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Called with null messageType. Parameters: \'' + arguments + '\'');
return 1;
}
switch(messageType)
{
case 'PM':
break
case 'Hubchat':
break
default:
printErr(scriptName + separator + arguments.callee.name + separator + 'Called with an invalid messageType (\'' + messageType + '\'. Parameters: \'' + arguments + '\'');
return 1;
}
// Obtaining message details
var nick = varMap["NICK"];
var message = varMap["MSG"];
//var messageTime = varMap["TIME"]; // Not used
var hubURL = hubFrame.getHubUrl();
var hubName = hubFrame.getHubName();
var isOP = hubFrame.isOP(nick);
// Debug code
//print('handleMessage triggered!\nvarMap: ' + varMap + '\nhubFrame: ' + hubFrame);
// Dealing with different messageTypes
switch(messageType)
{
case 'PM':
// Exiting if the message is actually an outgoing PM by myself (both the PM out and PM in are exposed separately in Eiskalt) - this is also critical to avoid recursion when the status PM is sent...
if (varMap["ECHO"] == true) { return; }
// Obtaining client CID - not available in a highlight
var CID = varMap["CID"];
// Converting to appropriate interface value
messageType = 'TEXT';
// Calling bash interface
try
{
//<message type, 'TEXT'/'ACTION'/'HIGHLIGHT'> <hub> <nick> <isOP, 'true'/'false'> <message, speechmark-delimited> <highlight string, speechmark-delimited>
var shell = ShellCommandRunner(bashInterfaceScript, messageType, hubName, nick, isOP, message);
shell["finished(bool, QString)"].connect(
function(ok, output)
{
// Passing off to shellJobComplete
shellJobComplete(shell, output, messageType, hubFrame, CID);
}
);
shell.run();
}
catch(error)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Unable to spawn bash script \'' + bashInterfaceScript + '\' to handle the following message:\n\nMessage: ' + message + '\nMessage type: ' + messageType + '\nHub: ' + hubName + ' (' + hubURL + ')\n\n' + returnError(error));
return 1;
}
break;
case 'Hubchat':
// Converting to appropriate interface value
messageType = 'HIGHLIGHT';
// Obtaining highlight-specific message details
highlightString = varMap["TRIGGER"];
// Calling bash interface
try
{
//<message type, 'TEXT'/'ACTION'/'HIGHLIGHT'> <hub> <nick> <isOP, 'true'/'false'> <message, speechmark-delimited> <highlight string, speechmark-delimited>
var shell = ShellCommandRunner(bashInterfaceScript, messageType, hubName, nick, isOP, message, highlightString);
shell["finished(bool, QString)"].connect(
function(ok, output)
{
// Passing off to shellJobComplete - CID not available
shellJobComplete(shell, output, messageType, hubFrame);
}
);
shell.run();
}
catch(error)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Unable to spawn bash script \'' + bashInterfaceScript + '\' to handle the following message:\n\nMessage: ' + message + '\nMessage type: ' + messageType + '\nHub: ' + hubName + ' (' + hubURL + ')\n\n' + returnError(error));
return 1;
}
break;
}
}
// Pass hubFrame if available, otherwise hubURL
function hookHub(hubURL, hubFrame)
{
// Checking if a hubURL was provided
if (hubURL != '')
{
// Fetching hubframe object and making sure it is valid
try
{
hubFrame = HubManager.getHub(hubURL);
}
catch(error)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Unable to obtain the HubFrame for ' + hubURL + '\n\n' + returnError(error));
return 1;
}
if (hubFrame == null)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Unable to obtain the HubFrame for ' + hubURL + '\n\n' + returnError(error));
return 1;
}
}
else
{
// Making sure the provided hubFrame is valid, as much as I can - cant seem to do instance of 'HubFrame' or 'UIHubFrame'?
if (!(hubFrame instanceof Object))
{
printErr(scriptName + separator + arguments.callee.name + separator + 'The hubFrame provided is invalid: ' + hubFrame);
return 1;
}
// Obtaining hubURL
hubURL = hubFrame.getHubUrl();
}
// Obtaining hub name
var hubName = hubFrame.getHubName();
// Hooking message handlers into hubframe
try
{
hubFrame["highlighted(VarMap)"].connect(
function(varMap)
{
// Passing off to handleMessage
handleMessage(varMap, hubFrame, 'Hubchat');
}
)
}
catch(error)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Unable to hook into ' + hubName + ' (' + hubURL + ')\'s highlighted signal\n\n' + returnError(error));
return 1;
}
try
{
hubFrame["corePrivateMsg(VarMap)"].connect(
function(varMap)
{
// Passing off to handleMessage
handleMessage(varMap, hubFrame, 'PM');
}
)
}
catch(error)
{
printErr(scriptName + separator + arguments.callee.name + separator + 'Unable to hook into ' + hubName + ' (' + hubURL + ')\'s corePrivateMsg signal\n\n' + returnError(error));
return 1;
}
// Logging success
print(scriptName + separator + arguments.callee.name + separator + 'Hooked into hubchat and PMs in ' + hubName + ' (' + hubURL + ')');
}
// Fetching 'registered' hubs
var hubManager = HubManager();
var hubList = hubManager.getHubs();
// Looping through hubs (note that if this is ran at client startup, none are actually initialised at this point, however script must be capable of reloading)
for (i in hubList)
{
// Hooking up hubs
hookHub('', hubList[i]);
}
// Hooking into hub registration (when the user opens a new hub) events so that hubchat/PMs can then be hooked
hubManager["hubRegistered(QObject*)"].connect(
function(hubFrame)
{
// Hooking up hub
hookHub('', hubFrame);
}
)The code has been running live for a few days, so should be free from bugs... |