Call an AppleScript from Qt/sq

From Qt Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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

Nëse dëshironi që të bëni thirrje të AppleScript komandave nga Qt mund ta përdorni këtë pjesë të kodit që është më poshtë.

#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;
}

It holds the actual script in variable aScript. Then creates a QProcess for invoking the AppleScript command line tool osascript.

Kodi më lartë përmban apple skriptën në variablën e quajtur aScript. Pastaj krijon një instancë të QProcess për të bërë thirrjen e komandës të AppleScript i cili quhet osascript.

The arguments call osascript with -l AppleScript, so that the it needs not to guess the script language.

Argumenti -l në osascript përdoret që të mos mundohet të kuptojë se për cilën gjuhë skriptuese bëhet fjalë.

The script is then fed to osascript via stdin.

Pastaj skriptës i shtohen të dhënat nga hyrja standarde (stdin).

Programi pret për të dhëna nga dalja e skriptës dhe më pas ne lexojmë daljen nga skripta.

If there are bytes available, the program reads them and converts them to a QString (if that is ok for the expected data!). In a real world program on should connect to the various readyReadXXX() signals and connect a slot to it to collect the data.

Nëse ka byte që kanë ardhur nga ekzekutimi, programi jonë i lexon ato dhe i shëndron në QString (kjo duhet të ndodhë vetëm nëse presim string nga skripta). Zakonisht programi duhet të lidhë slot-ët në disa signale p.sh readyRead().