Call an AppleScript from Qt/es

From Qt Wiki
Jump to navigation Jump to search
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.