Qt World Summit: Register Today!

Run Osascript with QT

  • Hi,
    I'm currently trying to run an apple script via Osascript in a QT app.
    Here is my code:
    QString aScript =
    "on isVoiceOverRunning()\n"
    " set isRunning to false\n"
    " tell application quot;System Eventsquot;\n"
    " set isRunning to (name of processes) contains quot;VoiceOverquot;\n"
    " end tell\n"
    " return isRunning\n"
    "end isVoiceOverRunning\n"
    "on isVoiceOverRunningWithAppleScript()\n"
    " if isVoiceOverRunning() then\n"
    " set isRunningWithAppleScript to true\n"
    " tell application quot;VoiceOverquot;\n"
    " try\n"
    " set x to bounds of vo cursor\n"
    " on error\n"
    " set isRunningWithAppleScript to false\n"
    " end try\n"
    " end tell\n"
    " return isRunningWithAppleScript\n"
    " end if\n"
    " return false\n"
    " end isVoiceOverRunningWithAppleScript\n"
    " set msg to %1\n"
    " if isVoiceOverRunningWithAppleScript() then\n"
    " tell application quot;VoiceOverquot;\n"
    " output msg\n"
    " end tell\n"
    " else\n"
    " say msg\n"
    " end if\n";
    QString osascript = "/usr/bin/osascript";
    QStringList processArguments;
    processArguments << "-l" << "AppleScript";
    QProcess p;
    p.start(osascript, processArguments);

    When I run my app in console, I have the following error:
    "QProcess: Destroyed while process ("/usr/bin/osascript") is still running."
    And my script is not running.
    Have you an idea of my mistake?
    Thanks in advance.

  • Lifetime Qt Champion

    @Oreonan said in Run Osascript with QT:

    Have you an idea of my mistake?

    As the warning message already tells you you destroy the QProcess instance before the application you want to execute is finished. Read the docs and don't destroy the QProcess until you get the QProcess::finished() signal.