Call an AppleScript from Qt: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
m (Fixed formatting. Previously the code block did not appear as a whole code block, but rather only indented lines were in code blocks.)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
'''English'''
| [[Call_an_AppleScript_from_Qt_Russian|Русский]]
| [[Call_an_Apple_Script_from_Qt_Italian |Italiano]]
| [[Call_an_AppleScript_from_Qt_Spanish|Español]]
| [[Call_an_AppleScript_from_Qt_Albanian|Shqip]]
| [[Call_an_AppleScript_from_Qt_Bulgarian|Български]]
[[Category:HowTo]]
[[Category:HowTo]]
If you want to call AppleScript commands from within Qt you can use this code snippet as a starting point.
If you want to call AppleScript commands from within Qt you can use this code snippet as a starting point.


<code>
<syntaxhighlight lang="cpp">
#include <QApplication>
#include <QApplication>
#include <QProcess>
#include <QProcess>
Line 18: Line 10:
int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
QApplication a(argc, argv);
  QApplication a(argc, argv);


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


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


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


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


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

Latest revision as of 18:59, 7 September 2022

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

If you want to call AppleScript commands from within Qt you can use this code snippet as a starting point.

#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.

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

The script is then fed to osascript via stdin.

The program waits for some data on the output of the script to come in. We do read the output of the script, hence waitForReadyRead.

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.