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 20.8k 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
    #1

    MacOS X. I want to automate an AppleScript-scriptable application, i.e. to execute applescript without using any native apps or command-line tools.

    There're some pieces of relevant code "here":http://habrahabr.ru/blogs/qt_software/104633/, but Carbon is used. And I'd like to use Cocoa

    Qt rulez

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

      At least you must call the AppleScript interpreter or create an executable of your script and call it with QProcess.

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

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

        [quote author="Volker" date="1291395979"]At least you must call the AppleScript interpreter[/quote]

        I clearly understand that :) The question is how exactly to do that? code samples would be great

        [quote author="Volker" date="1291395979"]or create an executable of your script and call it with QProcess[/quote]

        That's not desirable. Some API would be better.

        Qt rulez

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

          There is no API in Qt to do that. You must include native API calls (Objective C, most probably) or call some command line tool (osascript) via QProcess.

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

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

            Of course there's no API in Qt for that :)

            Qt rulez

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

              Then I don't understand you problem. What are you trying to do?

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

              1 Reply Last reply
              0
              • 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

                                          • Login

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