9

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.

14

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

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

Test it by loading and unloading it:

launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist
| improve this answer | |
  • Seems like this is the best way to do it. I was hoping for something more "native" in a way, but this should be quite reliable. Thank you. – Skeleton Bow Feb 15 '17 at 13:21
  • I want to mention a problem I'm facing trying to implement this: the rm command requires that I run it using sudo. I'm using the first method in the SO link you posted. Would you happen to have any suggestions for that? Do I need to use the launch daemon method to overcome this? – Skeleton Bow Feb 15 '17 at 14:24
  • I updated my answer with a slightly hacky solution. – hapi Feb 17 '17 at 2:10
  • 2
    If DriveToHide has spaces, then your scripts, as currently written, will fail. If you are going to test with [ ... ] then double quote the variables to prevent blobbing and word splitting. Or use [[ ... ]] to test, then double quoting isn't necessary. – user3439894 Feb 17 '17 at 2:33
  • Nice! thanks user3439894. I updated the answer to reflect your suggestion. – hapi Feb 17 '17 at 3:00
4

I learned today that you can use a sudo touch /.metadata_never_index_unless_rootfs in the root directory of each drive to separate Indexes from OS X Boot-Drives. It is a special version of .metadata-never-index, because it will (re)index the drive when you boot from it, but not when you don't.

| improve this answer | |
  • 1
    This seems like a good answer to the question. But do you have a source for this? – Skeleton Bow Jun 4 at 1:01
2

Apologies for the new answer (not enough rep to comment as I'm new here)

@hapi - I may be confused, but are the scripts the wrong way round?

Script for OS that wants to hide a drive from spotlight: renames .metadata_never_index

Script on the OS that wants to index the drive: creates .metadata_never_index

I thought the presence of .metadata_never_index on the volume meant Spotlight ignored it?

Thanks

| improve this answer | |
-3

Whereas the other answer is a good method to do it, it can inadvertently cause problems and may not be 100% reliable. A better method would be to use Alfred, as it is more flexible than Spotlight, on the external OS.

| improve this answer | |
  • How would you use Alfred for this? – Matt Sephton Jan 2 '18 at 19:34
  • Good question. I wish I remembered how. I believe I was talking about the way that you can exclude folders from the Alfred search without having to use exclusion method which was suggested in the other answer. – Skeleton Bow Jan 2 '18 at 22:05
  • I'm assuming that I meant that I would search for files using Alfred rather than using Spotlight search. In retrospect this seems like a silly answer to my question. – Skeleton Bow Jun 4 at 0:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .