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?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
36 Posts 7 Posters 20.7k Views
  • 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.
  • T Offline
    T Offline
    tk.fs
    wrote on 6 Jan 2014, 09:13 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 18 Jan 2014, 20:12 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 20 Jan 2014, 08:44 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 20 Jan 2014, 08:51 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 20 Jan 2014, 09:24 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 20 Jan 2014, 09:53 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 20 Jan 2014, 20:54 last edited by
                #27

                Hi,

                From a look at osascript's documentation, I would say that you are not feeding it correctly. I think you should be using several -e to build your multiline script.

                Hope it helps

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  janfaroe
                  wrote on 20 Jan 2014, 20:58 last edited by
                  #28

                  Multiline is not a problem, at least not in my case. I'm NL'ing with \n as well. I think QProcess is taking care of that part as long as it's in the argument list.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 20 Jan 2014, 23:16 last edited by
                    #29

                    Since you have an example working, can you compare what parameters you give to oascript ? There may be something like a missing "-" to tell oascript to read from stdin

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      janfaroe
                      wrote on 21 Jan 2014, 08:25 last edited by
                      #30

                      Sure, although it seems very identical to me. I think the problem is in the script itself.

                      @QStringList args;
                      QProcess p;
                      scriptText =
                      "tell application "iTunes"\n"
                      "if not (exists user playlist "%1") then\n"
                      " make new user playlist with properties {name:"%1"}\n"
                      "end if\n"
                      "set newFile to (POSIX file "%2")\n"
                      "set newTrack to (add newFile to playlist "%1")\n"
                      "set comment of newTrack to "%3""
                      "\n"
                      "end tell";
                      fileName = fileName.replace(""", "\"");
                      args << "-l" << "AppleScript";
                      scriptLauncherBinary = "/usr/bin/osascript";
                      p.start(scriptLauncherBinary, args);
                      p.write(scriptText.arg(plName).arg(fileName).arg(getComment()).toUtf8());
                      p.closeWriteChannel();@

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gaojinhsu
                        wrote on 21 Jan 2014, 08:41 last edited by
                        #31

                        I mean the way how chrome opens the network setting dialog, not how it sets proxies...have a look "here":http://dougscripts.com/itunes/2013/10/os-x-10-9-applescripts-and-accessibility-control/ so if chrome uses applescript to open the proxies settings dialog, it should be in the list of "Allow the apps below to control your computer". I am very curious how chrome could skip it.
                        [quote author="janfaroe" date="1390211636"]
                        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. [/quote]

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          janfaroe
                          wrote on 21 Jan 2014, 08:49 last edited by
                          #32

                          Yes, but why would you be interested in opening Chromes network settings, if the system settings can override it?

                          Edit: Ah, because of the "allow the apps below..." setting. Sorry, not an Applescript expert.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gaojinhsu
                            wrote on 21 Jan 2014, 08:55 last edited by
                            #33

                            because I think it is a good user experience, I wanna add a button like that in my app.

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              gaojinhsu
                              wrote on 21 Jan 2014, 09:03 last edited by
                              #34

                              Thanks anyway, that's very kind of you for giving me the advice.

                              [quote author="janfaroe" date="1390294144"]Yes, but why would you be interested in opening Chromes network settings, if the system settings can override it?

                              Edit: Ah, because of the "allow the apps below..." setting. Sorry, not an Applescript expert.[/quote]

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                janfaroe
                                wrote on 21 Jan 2014, 09:22 last edited by
                                #35

                                You're welcome :-)

                                Well, it depends on the scope of your application. If it's necessary, then it's necessary.

                                1 Reply Last reply
                                0
                                • Z Offline
                                  Z Offline
                                  zyfihx
                                  wrote on 31 Dec 2021, 17:14 last edited by
                                  #36

                                  About running such a script as a QProcess : I am trying to make it work in a sandbox environment (App Store), but I'm not sure wether the main app's entitlements apply to the osascript QProcess launched ? I'm getting access violation errors but I'm not sure if it's because my app is not signed correctly with entitlements, or if maybe it is because I must figure out a way to get the entitlements to apply also to the subprocess...

                                  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