Call an AppleScript from Qt

From Qt Wiki
Revision as of 14:18, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

English | Русский | Italiano | Español | Shqip | Български

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

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.

If you don’t need the output of the script, just call waitForFinished() right after closeWriteChannel(). The program will then wait until your script has finished, otherwise flow of control will go the end and prematurely end the called script.

Categories: