Prevent Spotlight from indexing external drive Sometimes I use an external drive with macOS on it. This is in conjunction to my current internal drive which also is running macOS.

I find it annoying that, whenever I search for something (such as an application) when I'm in either OS when the external drive is connected, I get results from both my external and internal drive. However, I am only interested in running the application from the drive I've booted off of. 

I tried circumventing this issue while on my external drive by creating a Spotlight exception for internal drive. This prevented files and apps from my internal drive from showing up. However, this also prevents me from searching for files on my internal drive, when I'm booted off my internal drive! Clearly this is something that I don't want. 

It seems as though the Spotlight exclusions are per drive, which is not ideal in this scenario.

Is there a way to make Spotlight index only the drive which is currently booted? In this way when I'm booted off my external drive I'll get results only from the external drive, and same with the internal drive.
You could have a script that runs at startup that employs the technique suggested in this post https://apple.stackexchange.com/a/91759/183505

When booting from DriveA (when you want to disable spotlight indexing for External DriveB) you could execute :

touch /Volumes/DriveB/.metadata_never_index


When booting from external DriveB and you want to re-enable spotlight perhaps you could have your startup script execute:

rm /Volumes/DriveB/.metadata_never_index


The linked post also lists other ways to programatically alter the spotlight exclusions.

Here are some ways to add a script that will launch at login : https://stackoverflow.com/questions/6442364/running-script-upon-login-mac

Good luck!



Edit : Method using bash scripts and plist files



First create a startup script. I chose to create one at ~/script.sh

Make sure it's executable chmod +x ~/script.sh

Script for OS that wants to hide a drive from spotlight

#!/bin/bash
flagLocation="/Volumes/DriveToHide"
flagRemoved=".ney_the_index"  # a new name

# if flag exists rename it.
if [ -a "$flagLocation/.metadata_never_index" ]; then 
    mv "$flagLocation/.metadata_never_index" "$flagLocation/$flagRemoved";
fi


Script on the OS that wants to index the drive

#!/bin/bash
flagLocation="/Volumes/DriveToHide"
flagRemoved=".ney_the_index"

if [ -a "$flagLocation/$flagRemoved" ]; then
    mv "$flagLocation/$flagRemoved" "$flagLocation/.metadata_never_index"
fi

if [ ! -a "$flagLocation/$flagRemoved" ] || [ ! -a "$flagLocation/.metadata_never_index" ] ; then
    touch "$flagLocation/.metadata_never_index"
fi


Create a plist file ~/Library/LaunchAgents/com.user.loginscript.plist

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
   &lt;key&gt;Label&lt;/key&gt;
   &lt;string&gt;com.user.loginscript&lt;/string&gt;
   &lt;key&gt;Program&lt;/key&gt;
   &lt;string&gt;/Users/yourusername/script.sh&lt;/string&gt;
   &lt;key&gt;RunAtLoad&lt;/key&gt;
   &lt;true/&gt;
&lt;/dict&gt;
&lt;/plist&gt;


Test it by loading and unloading it:

launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist