In my current role, we have around 30 projects distributed across roughly 20 different repositories. Most of them are Swift packages for various features used in our apps.

This setup requires me to frequently switch between multiple projects. Just a few days ago, I was working on the login flow. This involved modifying two Swift packages and one Xcode project.

Remembering whether a given project is a Swift package, a workspace (.xcworkspace), or a standard project (.xcodeproj) is exhausting. This adds unnecessary complexity and slows down the workflow. It would be better if we had a single command to open all of them at once. Here’s a script that does just that:

# The osp (Open Swift project) function first attempts to open a Package.swift file in the current directory; if it doesn't exist, it then searches for and opens either a .xcworkspace or a .xcodeproj directory, or outputs an error message if none are found.

osp() {
# Check for Package.swift in the current directory
if [[ -f "Package.swift" ]]; then
open "Package.swift"
return
fi

# Look for .xcworkspace directories in the current directory
local workspace=$(find . -maxdepth 1 -name "*.xcworkspace" -type d | head -n 1)

if [[ -n "$workspace" ]]; then
open "$workspace"
return
fi

# Look for .xcodeproj directories if no .xcworkspace directories were found
local project=$(find . -maxdepth 1 -name "*.xcodeproj" -type d | head -n 1)
if [[ -n "$project" ]]; then
open "$project"
return
fi

# Output error message if neither file type was found
echo "No project or workspace files found"
}

To use it, simply append it to your .zshrc file and run source ~/.zshrc or restart the shell.