Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Call an AppleScript from Qt/it: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Undo revision 19876 by Mcosta (talk))
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Call_an_AppleScript_from_Qt|English]]<br />| [[Call_an_AppleScript_from_Qt_Russian|Русский]]<br />| '''Italiano'''<br />| [[Call_an_AppleScript_from_Qt_Spanish|Español]]<br />| [[Call_an_AppleScript_from_Qt_Albanian|Shqip]]<br />[[Category:HowTo]]
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
 
[[Call_an_AppleScript_from_Qt|English]]
| [[Call_an_AppleScript_from_Qt_Russian|Русский]]
| '''Italiano'''
| [[Call_an_AppleScript_from_Qt_Spanish|Español]]
| [[Call_an_AppleScript_from_Qt_Albanian|Shqip]]
[[Category:HowTo]]


= Includere AppleScript in Qt =
= Includere AppleScript in Qt =
Line 5: Line 12:
Se volete includere un AppleScript in Qt potete usare questo script come punto di partenza.
Se volete includere un AppleScript in Qt potete usare questo script come punto di partenza.


<code><br />#include &lt;QApplication&amp;gt;<br />#include &lt;QProcess&amp;gt;<br />#include &lt;QDebug&amp;gt;
<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 /> &quot;tell application quot;System Eventsquot;&quot;<br /> &quot; activate\n&amp;quot;<br /> &quot; display dialog quot;Hello worldquot;&quot;<br /> &quot;end tell\n&amp;quot;;
QString aScript =
"tell application quot;System Eventsquot;"
" activate\n"
" display dialog quot;Hello worldquot;"
"end tell\n";


QString osascript = &quot;/usr/bin/osascript&amp;quot;;<br /> QStringList processArguments;<br /> processArguments &lt;&lt; &quot;-l&amp;quot; &lt;&lt; &quot;AppleScript&amp;quot;;
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() &lt;&lt; &quot;the result of the script is&amp;quot; &lt;&lt; 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>


Prende l'attuale script e lo mette nella variabile aScript. Quindi crea un QProcess per invocare il tool a riga di comando AppleScript osascript.
Prende l'attuale script e lo mette nella variabile aScript. Quindi crea un QProcess per invocare il tool a riga di comando AppleScript osascript.

Latest revision as of 11:07, 20 November 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

Includere AppleScript in Qt

Se volete includere un AppleScript in Qt potete usare questo script come punto di partenza.

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

Prende l'attuale script e lo mette nella variabile aScript. Quindi crea un QProcess per invocare il tool a riga di comando AppleScript osascript.

Gli argomenti passati invocano osascript con -l AppleScript, in modo che non necessiti di indovinare il linguaggio di scripting.

Lo script è quindi mandato a osascript via stdin.

Il programma attende i dati in output dello script per iniziare. Noi_dobbiamo_leggere l'ouput dello script, quindi usiamo waitForReadyRead.

Se ci sono bytes disponibili, il programma li legge e li converte in un QString (se questo va bene per i dati previsti!). Nel mondo reale il programma deve collegarsi ai vari segnali di readyReadXXX() e connettersi agli slot per collezionare i dati.

Se non necessiti dell'output dello script, chiama waitForFinished() dopo closeWriteChannel().