20

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?

Community
  • 1
  • 1
Blaz
  • 3,548
  • 5
  • 26
  • 40

5 Answers5

15

UPDATE: Apple removed support for this type of plugin as of Xcode 8. AppleScript is probably your best bet.

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!

kitschpatrol
  • 454
  • 4
  • 6
  • 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? – Smikey Aug 01 '14 at 16:10
  • @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? https://github.com/kitschpatrol/KPRunEverywhereXcodePlugin – kitschpatrol Oct 08 '14 at 21:28
  • One click install and everything works fine! Would be perfect if you could also include one of the simulators to run with. – Alex Cio Jan 27 '15 at 14:46
  • Did I ever tell you that you are awesome? I don't think I have. You are awesome. Thank you. – Alex Zavatone Jun 03 '15 at 17:19
  • 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/KPRunEverywhereXcodePlugin.xcplugin' not present in DVTPlugInCompatibilityUUIDs – Alex Zavatone Jun 03 '15 at 17:45
  • @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. – kitschpatrol Jun 23 '15 at 19:39
  • Works for me in XCode 6.4. Makes testing so much less cumbersome. – Paul Slocum Aug 22 '15 at 11:44
  • 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) – Paul Slocum Jan 08 '16 at 06:15
  • @PaulSlocum Should be fixed. – kitschpatrol Mar 01 '16 at 05:45
  • Thanks, works great on 7.3! Any advice on how to locate the UUID for Xcode when the latest version comes out? – Paul Linsay May 06 '16 at 16:34
  • Not working at all in XCode 10 (probably because apple has removed support for plugins in v8) – Christophe Blin Aug 23 '19 at 08:31
12

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.

Blaz
  • 3,548
  • 5
  • 26
  • 40
  • 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" – joon Aug 18 '14 at 12:51
  • @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.) – Blaz Aug 20 '14 at 05:41
  • @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! – Omer Jan 08 '16 at 20:22
  • Works fine on Mojave – SevenDays Feb 13 '19 at 11:09
5

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);
    });
}
Ivan Neeson
  • 2,823
  • 2
  • 16
  • 11
  • 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? – ilan Feb 25 '18 at 08:23
  • 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" – Aidan Host Nov 03 '18 at 08:35
  • Can anyone please point me to the documentation of this? what other API do we have other than `platform()` ? – gutte Jan 13 '22 at 08:12
  • To see the methods available open Script Editor, go File > Open Dictionary and choose Xcode. Change language from AppleScript to JavaScript. – Ivan Neeson Aug 22 '22 at 23:32
1

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

Community
  • 1
  • 1
TheThibz
  • 71
  • 5
  • 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. – Blaz May 06 '13 at 19:07
  • Since GDB is simply launched from the terminal, using lldb would be a matter of finding out the correct command. – TheThibz Jun 03 '13 at 08:02
0

Here's a script that will run all of the devices currently available in your Product -> Destination menu. NB: it relies on the following conditions:

  1. Devices are selected from the Product -> Destination menu (could change in future versions of Xcode)
  2. The menu item before your devices is named "My Mac 64-bit" (could change in future versions of Xcode)
  3. The menu item after your devices is named "iOS Simulator" (guess when that might change?)

    tell application "Xcode"
        activate
    end tell
    
    tell application "System Events"
        tell process "Xcode"
            set deviceMenu to menu 1 of menu item "Destination" of menu 1 of menu bar item "Product" of menu bar 1
            set allUIElements to entire contents of deviceMenu
            set startAfterName to "My Mac 64–bit"
            set stopName to "iOS Simulator"
            set started to false
            repeat with anElement in allUIElements
                try
                    set menuName to name of anElement
                    if menuName is equal to stopName then
                        set started to false
                        exit repeat
                    else if menuName is equal to startAfterName then
                        set started to true
                    else if started then
                        click menu item menuName 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
                    end if
                end try
            end repeat
        end tell
    end tell
    
kalperin
  • 509
  • 5
  • 20
  • 1
    Oldie, but goodie has been superseded by https://github.com/kitschpatrol/KPRunEverywhereXcodePlugin . – kalperin Jan 19 '15 at 15:49