Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MacOS: how to run AppleScript script from a Qt app?
Forum Updated to NodeBB v4.3 + New Features

MacOS: how to run AppleScript script from a Qt app?

Scheduled Pinned Locked Moved General and Desktop
36 Posts 7 Posters 21.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    infoctopus
    wrote on last edited by
    #7

    I'm asking someone to point out to proper MacOS API or, ideally, some 3rd party Qt lib for that (which, most likely, doesn't exist, but nevertheless).

    Qt rulez

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #8

      This may give you a start:

      @
      #include <QApplication>
      #include <QProcess>

      int main(int argc, char **argv)
      {
      QApplication a(argc, argv);

      QString aScript =
              "tell application \"System Events\"\n"
              "    activate\n"
              "    display dialog \"Hello world\"\n"
              "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.waitForFinished();
      

      }
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #9

        I've added a wiki page (http://developer.qt.nokia.com/wiki/Call_an_AppleScript_from_Qt) in the how to section for this.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #10

          The wiki now contains a sample how to read the output of the script.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • I Offline
            I Offline
            infoctopus
            wrote on last edited by
            #11

            Volker, great! thank you

            Qt rulez

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #12

              You're welcome. If you have any further questions, don't hesitate to ask.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • I Offline
                I Offline
                infoctopus
                wrote on last edited by
                #13

                Well, I'm thinking about binding Qt's JavaScript to AppleScript, mixing Qt objects and AppleScript-scriptable apps. About executing AppleScript line-by-line. About accessing AppleScript's scripting runtime environment.

                Qt rulez

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #14

                  I don't know if that's possible. I doubt one can "translate" JavaScript to AppleScript in that way.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    infoctopus
                    wrote on last edited by
                    #15

                    [quote author="Volker" date="1291404790"]I don't know if that's possible. I doubt one can "translate" JavaScript to AppleScript in that way.[/quote]

                    It depends. For instance the following code

                    @tell application "System Events"
                    activate
                    display dialog "Hello world"
                    end tell;@

                    obviously can be expressed in Java/ECMAScript

                    Qt rulez

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      infoctopus
                      wrote on last edited by
                      #16

                      I mean if we know the object model and grammar of a scripting language, we can tell if some script valid. And subset of features that is common for both scripting languages can be supported

                      Qt rulez

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #17

                        [quote author="infoctopus" date="1291468109"]I mean if we know the object model and grammar of a scripting language, we can tell if some script valid. And subset of features that is common for both scripting languages can be supported[/quote]

                        From what I understood, you want to create a transformer app that kind of "translates" JavaScript to/from AppleScript. That seems to be a pretty huge task.

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          infoctopus
                          wrote on last edited by
                          #18

                          Well, I have own simplified scripting language (and simple IDE for it) that is mapped to JavaScript.
                          I'm thinking about switching to JavaScript completely, i.e. IDE has to be seriously upgraded.
                          And also to engane AppleScript as much as possible. For this, own scripting language can be left untouched, but mapping to AppleScript can be added. The problem is that I don't know how to control (exec line by line, debug, etc.) AppleScript execution, if it's possible at all. Most likely I have to dig apple docs

                          Qt rulez

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #19

                            I don't know if thats possible with AppleScript at all. I wish you good luck for your research!

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • I Offline
                              I Offline
                              infoctopus
                              wrote on last edited by
                              #20

                              Volker, thank you :) I think that the task is solvable, but not sure about the amount of time/money needed for the solution, even roughly

                              Qt rulez

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                tk.fs
                                wrote on last edited by
                                #21

                                Hi Volker, I've tried your solution on Mac OS X 10.9.1 using a simple AppleScript to retrieve a mail subject from Apple Mail. The script needs around 1 sec to execute with the AppleScript Editor application, but around 20-30 secs when using QProcess/osascript. Running osascript from a shell window also needs around 1 sec for the script.

                                Is this a known problem with QProcess or do you have any idea where this massive delay is coming from?

                                Thanks,

                                Thomas

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  janfaroe
                                  wrote on last edited by
                                  #22

                                  I have no problems running OSAScript via QProcess. It's just as snappy as the command line for me. Perhaps you should review your QProcess code? Try a simple shell command as a comparison?

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    gaojinhsu
                                    wrote on last edited by
                                    #23

                                    The following code cannot succeed to open the proxy settings. but the apple script works fine in AppleScript Editor. Any advice? using Qt 5.2 and OSX10.9
                                    @QString aScript = "tell application "System Preferences"\nactivate\nset current pane to pane "com.apple.preference.network"\n activate\n end tell\n"
                                    "tell application "System Events" to tell process "System Preferences" to tell window 1 \n click button 8 \n"
                                    "click radio button 6 of tab group 1 of sheet 1 \n end tell";

                                    QString osascript = "/usr/bin/osascript";
                                    QStringList processArguments;
                                    processArguments << "-l" << "AppleScript";
                                    QProcess p;
                                    p.start(osascript, processArguments);
                                    p.write(aScript.toUtf8());
                                    p.closeWriteChannel();
                                    p.waitForFinished();@
                                    

                                    [quote author="Volker" date="1291398206"]This may give you a start:

                                    @
                                    #include <QApplication>
                                    #include <QProcess>

                                    int main(int argc, char **argv)
                                    {
                                    QApplication a(argc, argv);

                                    QString aScript =
                                            "tell application \"System Events\"\n"
                                            "    activate\n"
                                            "    display dialog \"Hello world\"\n"
                                            "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.waitForFinished();
                                    

                                    }
                                    @[/quote]

                                    1 Reply Last reply
                                    0
                                    • J Offline
                                      J Offline
                                      janfaroe
                                      wrote on last edited by
                                      #24

                                      What does QProcess::exitStatus() give you after start()? And QProcess::errorString()?

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        gaojinhsu
                                        wrote on last edited by
                                        #25

                                        QProcess::exitStatus(), 0;
                                        QProcess::errorString() , Unkonwn error;

                                        my code works imperfect ( which is system preferences->Network; but I want it to be system preferences->Network->advanced...->Proxies)

                                        And I find chrome does it in a different way, not applescript maybe.(you could check it out by clicking settings->show advanced settings->change proxy settings)
                                        because I cannot find chrome.app at System Preferences->Security&Privacy->Privacy->Accessibility(Allow the apps below to control youe computer).

                                        1 Reply Last reply
                                        0
                                        • J Offline
                                          J Offline
                                          janfaroe
                                          wrote on last edited by
                                          #26

                                          I suggest you try debugging in the terminal, where you'd get warnings/errors right away, and work your way from there.

                                          As for Chrome and other browsers: I strongly believe they take their default settings from the system ones, ie. those presented in the System Preferences.

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved