Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This gist describes how to switch Xcode schemes and run destinations using keybindings and Xcode behaviors.

How to Switch Xcode Schemes Quickly Using Keybindings

If you have a lot of schemes in your Xcode project, searching for the one you want to run may be slow, even if you use the “search as you type” feature. Because of this, you may choose to hide or not generate schemes that you don’t regularly use. Here’s an example of an Xcode project with a lot of schemes:

There’s an alternative. Until we have better scheme management in Xcode, let’s make our life easier by writing some AppleScript commands for the schemes that we typically use and bind them to some key combinations.

Create an AppleScript file (MyScript.applescript, for example) in your favorite editor and write the following code:

 tell application "Xcode"
	open "<Path to MyProject.xcworkspace>"
	set workspaceDocument to workspace document "MyProject.xcworkspace"
	repeat 120 times
		if loaded of workspaceDocument is true then
			exit repeat
		end if
		delay 0.5
	end repeat
	if loaded of workspaceDocument is false then
		error "Xcode workspace did not finish loading within timeout. If it's a very large project you could try increasing the timeout."
	end if
	set schemeToUse to scheme "MyProjectApp" of workspaceDocument
	set active scheme of workspaceDocument to schemeToUse
	set destinationToUse to run destination "iPad Air 2" of workspaceDocument
	set active run destination of workspaceDocument to destinationToUse
 end tell

This script will switch and wait for the workspace MyProject.xcworkspace to load (if it’s not already loaded) and then select the scheme “MyProjectApp”, with “iPad Air 2” as the run destination. Once you have switched the current scheme and run destination in Xcode, you could even build it automatically using AppleScript:

set buildResult to build workspace document 1

Save the script and mark it as executable by running this shell command:

chmod +x MyScript.applescript

Go to Xcode, Behaviors, Edit Behaviors. Click on the plus (+) sign, check Run and select the MyScript.applescript file. You can also set a keybinding for the new Xcode behavior (recommended).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment