Call an AppleScript from Qt/es: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (AutoSpider moved page Call an AppleScript from Qt Spanish to Call an AppleScript from Qt/es: Localisation)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:QtDevelopmentSpanish::General]]<br />[[Category:HowTo]]<br />[[Category:Spanish]]
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}


[[Call_an_AppleScript_from_Qt|English]]<br />| [[Call_an_AppleScript_from_Qt_Russian|Русский]]<br />| [[Call_an_Apple_Script_from_Qt_Italian |Italiano]]<br />| '''Español'''<br />| [[Call_an_AppleScript_from_Qt_Albanian|Shqip]]
[[Category:QtDevelopmentSpanish::General]]
[[Category:HowTo]]
[[Category:Spanish]]
 
[[Call_an_AppleScript_from_Qt|English]]
| [[Call_an_AppleScript_from_Qt_Russian|Русский]]
| [[Call_an_Apple_Script_from_Qt_Italian |Italiano]]
| '''Español'''
| [[Call_an_AppleScript_from_Qt_Albanian|Shqip]]


Si necesitas hacer una llamada a un comando AppleScript desde dentro de Qt, este trozo de código te puede servir como inicio
Si necesitas hacer una llamada a un comando AppleScript desde dentro de Qt, este trozo de código te puede servir como inicio


<code><br />#include <QApplication><br />#include <QProcess><br />#include <QDebug>
<code>
#include <QApplication>
#include <QProcess>
#include <QDebug>


int main(int argc, char **argv)<br />{<br /> QApplication a(argc, argv);
int main(int argc, char **argv)
{
QApplication a(argc, argv);


QString aScript =<br /> "tell application quot;System Eventsquot;"<br /> " activate\n"<br /> " display dialog quot;Hello worldquot;"<br /> "end tell\n";
QString aScript =
"tell application quot;System Eventsquot;"
" activate\n"
" display dialog quot;Hello worldquot;"
"end tell\n";


QString osascript = "/usr/bin/osascript";<br /> QStringList processArguments;<br /> processArguments << "-l" << "AppleScript";
QString osascript = "/usr/bin/osascript";
QStringList processArguments;
processArguments << "-l" << "AppleScript";


QProcess p;<br /> p.start(osascript, processArguments);<br /> p.write(aScript.toUtf8());<br /> p.closeWriteChannel();<br /> p.waitForReadyRead(–1);<br /> QByteArray result = p.readAll();<br /> QString resultAsString(result); // if appropriate<br /> qDebug() << "the result of the script is" << resultAsString;
QProcess p;
p.start(osascript, processArguments);
p.write(aScript.toUtf8());
p.closeWriteChannel();
p.waitForReadyRead(–1);
QByteArray result = p.readAll();
QString resultAsString(result); // if appropriate
qDebug() << "the result of the script is" << resultAsString;


return 0;<br />}<br /></code>
return 0;
}
</code>


Este mantiene el script actual en una variable aScript. Luego crea un QProcess para invocar el comando AppleScript usando la herramienta de linea de comandos osascript.
Este mantiene el script actual en una variable aScript. Luego crea un QProcess para invocar el comando AppleScript usando la herramienta de linea de comandos osascript.

Latest revision as of 15:58, 16 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English | Русский | Italiano | Español | Shqip

Si necesitas hacer una llamada a un comando AppleScript desde dentro de Qt, este trozo de código te puede servir como inicio

#include <QApplication>
#include <QProcess>
#include <QDebug>

int main(int argc, char **argv)
{
 QApplication a(argc, argv);

QString aScript =
 "tell application quot;System Eventsquot;"
 " activate\n"
 " display dialog quot;Hello worldquot;"
 "end tell\n";

QString osascript = "/usr/bin/osascript";
 QStringList processArguments;
 processArguments << "-l" << "AppleScript";

QProcess p;
 p.start(osascript, processArguments);
 p.write(aScript.toUtf8());
 p.closeWriteChannel();
 p.waitForReadyRead(1);
 QByteArray result = p.readAll();
 QString resultAsString(result); // if appropriate
 qDebug() << "the result of the script is" << resultAsString;

return 0;
}

Este mantiene el script actual en una variable aScript. Luego crea un QProcess para invocar el comando AppleScript usando la herramienta de linea de comandos osascript.

Usamos el argumento -l AppleScript para llamar a osascript, esto es necesario para que no tenga que adivinar que lenguaje de script es usado.

El guión se alimenta entonces a través osascript via stdin.

El programa espera la salida de datos del script para iniciar. Debemos_Leer la salida del script para a continuación utilizar waitForReadyRead.

Si hay bytes disponibles, el programa los lee y los convierte luego a QString (si eso está bien para los datos esperados!).

If there are bytes available, the program reads them and converts them to a QString (if that is ok for the expected data!).

En un programa en el mundo real este debe conectarse a las diferentes signals readyReadXXX() y conectar un slot a el para recolectar los datos.