How to 'Build & Run' on multiple destinations at once in Xcode?

12,192

Solution 1

I ran into the same issue, so I wrote an Xcode plugin to help out with this. I found it to be more robust and easier to invoke than the AppleScript options.

The plugin is called KPRunEverywhereXcodePlugin and is available via Alcatraz or on GitHub: https://github.com/kitschpatrol/KPRunEverywhereXcodePlugin

New menu items

Hope this helps!

Solution 2

It's actually simpler than I thought. This AppleScript puts some pain out of Xcode:

tell application "Xcode"
    activate
end tell

tell application "System Events"
    tell application process "Xcode"
        click menu item "1st iDevice Name" of menu 1 of menu item "Destination" of menu 1 of menu bar item "Product" of menu bar 1
        click menu item "Run" of menu 1 of menu bar item "Product" of menu bar 1
        delay 5
        click menu item "2nd iDevice Name" of menu 1 of menu item "Destination" of menu 1 of menu bar item "Product" of menu bar 1
        click menu item "Run" of menu 1 of menu bar item "Product" of menu bar 1
        delay 5
        click menu item "iPhone 6.1 Simulator" of menu 1 of menu item "Destination" of menu 1 of menu bar item "Product" of menu bar 1            
        click menu item "Run" of menu 1 of menu bar item "Product" of menu bar 1
    end tell
end tell
  1. Save the above AppleScript as an .app. (Customize delays to your machine.)
  2. Create a new Service in Automator: choose Launch Application and select the .app from previous step.
  3. Save Service from previous step and give it a keyboard shortcut. Tip: avoid shortcuts with ^, since it will bring up this dialog: enter image description here

Granted, this isn't strictly a simultaneous 'Build & Run', but it sure beats manually trifling between destinations.

Solution 3

Here is a script that will build and run on all connected iOS devices. To use:

  1. Open "Automator".
  2. Create a new "Quick Action".
  3. Select "no input" for "Workflow receives".
  4. Select Xcode for the application.
  5. Add a "Run JavaScript" action and paste the script.
  6. Save as "Run on All", you can now access this from the Xcode -> Services menu from Xcode.

Javscript:

function run(input, parameters)
{
    var xcode = Application("Xcode");
    var ws = xcode.activeWorkspaceDocument();
    var genericDest = null;
    var devices = [];
    ws.runDestinations().forEach(function(runDest)
    {
        if (runDest.platform() != "iphoneos")
            return;
        if (runDest.device().generic())
        {
            genericDest = runDest;
        }
        else
        {
            devices.push(runDest);
        }
    });
    devices.forEach(function(device)
    {
        ws.activeRunDestination = device;
        var buildResult = ws.run();
        while (true)
        {
            if (buildResult.completed())
                break;
            if (buildResult.buildLog() && buildResult.buildLog().endsWith("Build succeeded\n"))
                break;
            delay(1);
        }
        delay(1);
    });
}

Solution 4

It would indeed be nice to have multiple uploads at once with Xcode. However as I understand it, aggregate only allows you to compile multiple targets, not run them.

Given the second part of you question (after the edit) I can point you to another way to do it. You won't have xcode attached (but gdb in console mode) and you should be able to make it simultaneous on several device, although this was not the primary goal. This particular solution doesn't work with simulator, but there are other methods for those.

launching iOS App from Mac OS X console

Share:
12,192

Related videos on Youtube

Blaz
Author by

Blaz

Software developer focused on backend development, primarily using Elixir & OTP. Contact me.

Updated on January 07, 2020

Comments

  • Blaz
    Blaz over 4 years

    How can one run a project on multiple destinations (say, iPhone, iPad and iSimulator) at once? Xcode Product optionsXcode multiple destinations


    There are 2 related questions:

    1. Xcode 4 - One Click Build to Multiple Devices?
    2. Run on simulator and phone with one click

    The 1ˢᵗ question (supposedly) has an answer, but I can't figure out how exactly should you use the Aggregate target (if this is the right direction at all), and apparently neither could Josh Kahane, the OP; the "answer" still somehow got/remained accepted.

    The 2ⁿᵈ question was closed down as a "duplicate", as if the 1ˢᵗ one provided a (workable) answer.


    Added a bounty: (how) can one use Aggregate target for simultaneous, multiple Build & Run? Perhaps one can achieve simultaneous, multiple Build & Run via some .sh script using xcodebuild? Any other possible solution?

    • Dani
      Dani about 5 years
      2019 and Apple still doesn't provide a built in method to do this.
    • Witek Bobrowski
      Witek Bobrowski over 2 years
      2021 and Apple still doesn't provide a built in method to do this.
    • Dave Y
      Dave Y over 2 years
      2022 and Apple still doesn't provide a built in method to do this.
  • Blaz
    Blaz about 11 years
    How would you do it for LLDB? Including the simulator would be awesome as well. @aggregate target, yes that is my understanding as well. I can't fathom why the 1ˢᵗ related question has an accepted answer.
  • TheThibz
    TheThibz about 11 years
    Since GDB is simply launched from the terminal, using lldb would be a matter of finding out the correct command.
  • Smikey
    Smikey almost 10 years
    Hmmm, thanks for the script, installs fine, but when I choose Run Everywhere, it just builds, but doesn't run on any of my plugged in devices?
  • joon
    joon almost 10 years
    does this still work? I tried doing this and OSX tells me it no longer support classic mode "because the classic environment is no longer supported"
  • Blaz
    Blaz almost 10 years
    @joon, works fine on latest Mavericks. Not sure about Yosemite though. (Although this is besides the point, since I presume the Classic refers to OS 9.)
  • kitschpatrol
    kitschpatrol over 9 years
    @Smikey, I just tested the plugin again on Xcode 6 and it seems fine. If you're still having this problem, could you please open an issue with more info on GitHub? github.com/kitschpatrol/KPRunEverywhereXcodePlugin
  • kalperin
    kalperin over 9 years
    Oldie, but goodie has been superseded by github.com/kitschpatrol/KPRunEverywhereXcodePlugin .
  • Alex Cio
    Alex Cio over 9 years
    One click install and everything works fine! Would be perfect if you could also include one of the simulators to run with.
  • Alex Zavatone
    Alex Zavatone about 9 years
    Did I ever tell you that you are awesome? I don't think I have. You are awesome. Thank you.
  • Alex Zavatone
    Alex Zavatone about 9 years
    How do you deal with an error like this on startup?: [MT] PluginLoading: Required plug-in compatibility UUID E969541F-E6F9-4D25-8158-72DC3545A6C6 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/KPRunEverywhereXcode‌​Plugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs
  • kitschpatrol
    kitschpatrol about 9 years
    @AlexZavatone, do you have the latest version of the plugin? Which version of Xcode are you using? Please open an issue on GitHub if this is still a problem. Thanks.
  • Paul Slocum
    Paul Slocum almost 9 years
    Works for me in XCode 6.4. Makes testing so much less cumbersome.
  • Paul Slocum
    Paul Slocum over 8 years
    I was using this for a while, but unfortunately it keeps getting broken with crash bugs when XCode is updated (currently not working for me on XCode 7.1 or 7.2)
  • Omer
    Omer over 8 years
    @courteous This is great, I'm triggering it from automator directly (which is just fine) since on El Capitan I got the following error "You can’t open the application because PowerPC applications are no longer supported." and the service does not appear on the services menu of the Xcode app... still very helpful!
  • kitschpatrol
    kitschpatrol over 8 years
    @PaulSlocum Should be fixed.
  • Paul Linsay
    Paul Linsay about 8 years
    Thanks, works great on 7.3! Any advice on how to locate the UUID for Xcode when the latest version comes out?
  • ilan
    ilan over 6 years
    Hi , this is not working for me. I select the service in xcode and nothing happens. How can i test this and check what is the problem?
  • Aidan Host
    Aidan Host over 5 years
    Works well for me. One thing to note is that in Mojave it appears that the Automator steps are named slightly different. For step 2 instead of "Service" choose "Quick Action" and for step 3 "Workflow receives"
  • SevenDays
    SevenDays over 5 years
    Works fine on Mojave
  • Christophe Blin
    Christophe Blin almost 5 years
    Not working at all in XCode 10 (probably because apple has removed support for plugins in v8)
  • gutte
    gutte over 2 years
    Can anyone please point me to the documentation of this? what other API do we have other than platform() ?