Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Taming Spotlight

What folders does Spotlight index?


I run spotlight so that I can search in the mail App. What folders would I limit to limi the spotligh search to my mail box? Seems like I could turn off all folders except my mail folder. Does setting privacy on a folder prevent searching all imbeded folders? Guess so.


Spotlight was well behavied on my machine until I made a comment about it on another discussion! It decided about then to re-index my hd.


Robert

Posted on May 10, 2013 10:26 AM

Reply
23 replies

May 22, 2013 11:38 AM in response to BDAqua

Read the source code? 😕 🙂


I missed an obvious bug. Folder not found. An hour later or more, I have a new version of code.


I added a check for a missing folder.

I added comments to the front of the script to explain more details.


(* 

Place marker files in a list of folders so we can "see" what spotlight does.

How to use in SpotLight:
  Once your run this script, you may use spotlight to search for 
    SpotlightIndexedData
    SpotlightIndexedFolder 
  
  SpotlightIndexedData is inside the file.
  SpotlightIndexedFolder starts the file name. The file name contains the path.

Input:
  You need to modify the folderList variable below to contain the list of folders
  you want to add a file too. If for instance, you want to add files to 
  all folders in the root directory ( / ), use the find command to get the list.
  Macintosh-HD -> Applications -> Utilities -> Terminal
  # press return to run the command.
  find -x /   -type d -maxdepth 1 
  You will need to paste here then add the Applescript qutoes etc.The files are in
  Posix ( unix ) syntax.    You may use the ~ to represent your home folder 
  ( whoever is running this script ).  

  You may add folders you do not have write permission.  You will be prompted 
  for your addministrator password. Run as administrator.

Output for the first run.  Ouput will change slightly for second run.

            "~/notfoundtoday", ¬
            "/private/var", ¬
            "~/Desktop"

warrning message since file "~/notfoundtoday" does not exist

prompt for admin password to access "/private/var"

~/spewChangePermissionList on  -Users-mac-.txt
  New log created on Wednesday, May 22, 2013 2:00:29 PM 
     --- debug on Wednesday, May 22, 2013 2:00:29 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  drwxr-xr-x   25 root  wheel  850 May 22 13:59 /private/var

~/Desktop/SpotlightIndexedFolder on  -Users-mac-Desktop.txt
  New log created on Wednesday, May 22, 2013 2:00:40 PM 
     --- debug on Wednesday, May 22, 2013 2:00:41 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  SpotlightIndexedData 

/private/var/SpotlightIndexedFolder on  -private-var.txt
  New log created on Wednesday, May 22, 2013 2:00:37 PM 
     --- debug on Wednesday, May 22, 2013 2:00:38 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  SpotlightIndexedData


Note:
on the second and subsequent runnings will will not get a prompt for administration password since the file /private/var/SpotlightIndexedFolder on  -private-var.txt will already exist.

Author: rccharles
    
For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run
      
      
For running shell commands see:
    http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
    
Copyright 2013 rccharles
    GNU General Public License 
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation,  version 3
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>.

    


 *)


on run
    
    global firstRunning
    
    -- Names of the folders in which to write a marker file.  Use Unix names path.
    -- find -x /   -type d -maxdepth 1 
    set folderList to ¬
        {¬
            "~/notfoundtoday", ¬
            "/private/var", ¬
            "~/Desktop"}
    
    
    
    
    -- Write a message into the applescript editor event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    
    
    log "folderList = " & folderList
    
    set actualCd to do shell script "cd ~;pwd"
    log "actualCd = " & actualCd
    
    
    
    repeat with theFile in folderList
        
        log "theFile = " & theFile
        
        set firstRunning to ""
        
        set expandPath to alterString(theFile, "~", actualCd)
        log "expandPath = " & expandPath
        
        
        debug("SpotlightIndexedData ", expandPath, "SpotlightIndexedFolder")
        
    end repeat
    
    tell application "Finder"
        activate
        display dialog "Done.  You may now search for SpotlightIndexedData and SpotlightIndexedFolder in Spotlight" giving up after 5
    end tell
end run

-- ---------------------------------------------
(*
  Create or add to a log file

  based on log by James Reynolds, 12.18.2000, University of Utah
  called recursively.

  input: 
    text string message writen to file
     expanded posix path to file.  ( no ~ in string )
    file name
   
 *)

on debug(theMessage, thePath, theFileName)
    global firstRunning
    local theSize, startupDiskName, fileToLog, fileReference, appPath, changedPerms
    set lineEnd to ASCII character 10
    
    log "==> debug called"
    log "  theMessage = " & theMessage & " class is " & class of theMessage
    
    log "  thePath = " & thePath
    log "  theFileName = " & theFileName
    
    set savePerms to ""
    
    -- Does the folder exist?
    
    set classicPath to POSIX file thePath as string
    log "classicPath = " & classicPath
    tell application "Finder"
        if not (exists folder classicPath) then
            display dialog "Could not find folder " & thePath & ¬
                " skipping. " giving up after 15
            return
        end if
    end tell
    
    set imbedPath to theFileName & " on  " & alterString(thePath, "/", "-") & ".txt"
    log "imbedPath = " & imbedPath
    
    set unixPathToLog to (thePath & "/" & imbedPath)
    log "unixPathToLog = " & unixPathToLog
    
    
    set fileToLog to POSIX file unixPathToLog as string
    log "fileToLog = " & fileToLog
    
    -- Can we write to this folder?
    
    try
        set fileReference to (open for access file fileToLog ¬
            with write permission)
    on error errorMsg
        log "got error >" & errorMsg & "< "
        
        -- log an attempted permission change on a folder
        set permFileName to "spewChangePermissionList"
        log "permFileName = " & permFileName & " class is " & permFileName
        
        -- check for recursive looping
        if theFileName is equal to permFileName then
            display dialog "This is bad.  Looks like a loop. thePath = " & thePath ¬
                & " theFileName = " & theFileName
            return
        end if
        
        set permFolder to path to home folder
        set unixPermFolder to POSIX path of permFolder
        log "unixPermFolder = " & unixPermFolder
        
        set permList to do shell script "ls -ld " & quoted form of thePath
        log "permList = " & permList & " class is " & class of permList
        
        set priorRunning to firstRunning -- probably "" anyway.
        -- keep track of the folders we plan on changing
        debug(permList, unixPermFolder, permFileName)
        log "... from debug"
        set firstRunning to priorRunning
        
        -- perserve permissions.
        set savePerms to do shell script "stat -f \"%Lp\" " & quoted form of thePath
        
        -- fix permissions if we can.
        try
            set cmd to "chmod  a+wx  " & quoted form of thePath
            log "cmd = " & cmd
            
            set fromUnix to do shell script cmd with administrator privileges
        on error errMsg
            log "chmod error...>" & errMsg & "< on " & thePath
            display dialog "Could not change permissions. >" & errorMsg & ¬
                "< on " & thePath ¬
                giving up after 20
            return
        end try
        set fileReference to (open for access file fileToLog ¬
            with write permission)
        
    end try
    
    log "firstRunning = " & firstRunning
    set theSize to (get eof fileReference)
    log "theSize = " & theSize
    
    if firstRunning = "" then
        
        -- New file to create?
        if theSize is equal to 0 then
            appendToFile(fileReference, "New log created on " & ((current date) as string) & " " & lineEnd)
            
            
            close access fileReference
            try
                -- file must be closed for finder to change creator type.
                tell application "Finder"
                    -- TextWrangler
                    set the creator type of the file fileToLog ¬
                        to "!Rch"
                end tell
                
            on error errorMsg
                display dialog "warning problem with setting creator type. >" & errorMsg & "< results for " & fileToLog giving up after 30
            end try
            
            set fileReference to (open for access file fileToLog ¬
                with write permission)
            
        end if
        appendToFile(fileReference, "   --- debug on " & ((current date) as string) & "   --- " & lineEnd)
        tell current application
            set appPath to path to current application
        end tell
        appendToFile(fileReference, "   --- current AppleScript is " & appPath & " ---" & lineEnd)
        
        set firstRunning to "running"
    end if
    
    
    appendToFile(fileReference, theMessage & lineEnd)
    
    close access fileReference
    
    -- restore directory to what permsissions it had.
    
    if savePerms is not equal to "" then
        try
            set cmd to "chmod  " & savePerms & " " & quoted form of thePath
            log "cmd = " & cmd
            
            set fromUnix to do shell script cmd with administrator privileges
        on error errMsg
            log "chmod error on restoring file ...>" & errMsg & "< on " & thePath
            display dialog "Could not change permissions on attempting retore. >" & errorMsg & ¬
                "< on " & thePath ¬
                giving up after 20
            return
        end try
    end if
    
    
    
end debug

-- ------------------------------------------------------
on alterString(thisText, delim, replacement)
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    try
        set resultList to every text item of thisText
        set text item delimiters to replacement
        set resultString to resultList as string
        set my text item delimiters to tid
    on error
        set my text item delimiters to tid
    end try
    return resultString
end alterString

-- ------------------------------------------------------
-- textToList was found here:
-- http://macscripter.net/viewtopic.php?id=15423

on textToList(thisText, delim)
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    
    try
        set resultList to every text item of thisText
        set my text item delimiters to tid
    on error
        set my text item delimiters to tid
    end try
    return resultList
end textToList

-- ----------------------------------------------------------

on appendToFile(fileId, theData)
    
    local theSize, writeWhere
    log "--> class is " & class of theData
    set theSize to (get eof fileId)
    set writeWhere to theSize + 1 as integer
    write theData to fileId starting at writeWhere as «class utf8»
    
end appendToFile

May 23, 2013 8:10 PM in response to BDAqua

This version lets you input the folder list from a file.


Bug. Restriction!😊 You need read access to the folder to find the folder. Will have to enhance.



(* 

Place marker files in a list of folders so we can "see" what spotlight does.

How to use in SpotLight:
  Once your run this script, you may use spotlight to search for 
    SpotlightIndexedData
    SpotlightIndexedFolder 
  
  SpotlightIndexedData is inside the file.
  SpotlightIndexedFolder starts the file name. The file name contains the path.

Input:
 This script needs a list of folders to add the "SpotlightIndexedFolder ... " file.

 (1) 
  You need to modify the folderList variable below to contain the list of folders
  you want to add a file too. If for instance, you want to add files to 
  all folders in the root directory ( / ), use the find command to get the list.
  Macintosh-HD -> Applications -> Utilities -> Terminal
  # press return to run the command.
  find -x /   -type d -maxdepth 1 
  You will need to paste here then add the Applescript qutoes etc.The files are in
  Posix ( unix ) syntax.    You may use the ~ to represent your home folder 
  ( whoever is running this script ).  

  You may add folders you do not have write permission.  You will be prompted 
  for your addministrator password. Run as administrator.
  example:
  set folderList to ¬
        {¬
            "~/notfoundtoday", ¬
            "/private/var", ¬
            "~/Desktop"}
            
 (2)
 In case the list folderList is empty, this applescript will read the list from
 ~/Desktop/checkSpotLight-files.txt.  Place one folder per line.
 example ~/Desktop/checkSpotLight-files.txt.:
   /Users/mac/darwinports
   /Users/mac/Desktop  
   /private
   
 (3) 
 When ~/Desktop/checkSpotLight-files.txt isn't present, a promt for the file will appear.


Output for the first run.  Ouput will change slightly for second run.

            "~/notfoundtoday", ¬
            "/private/var", ¬
            "~/Desktop"

warning message since file "~/notfoundtoday" does not exist

prompt for admin password to access "/private/var"

~/spewChangePermissionList on  -Users-mac-.txt
  New log created on Wednesday, May 22, 2013 2:00:29 PM 
     --- debug on Wednesday, May 22, 2013 2:00:29 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  drwxr-xr-x   25 root  wheel  850 May 22 13:59 /private/var

~/Desktop/SpotlightIndexedFolder on  -Users-mac-Desktop.txt
  New log created on Wednesday, May 22, 2013 2:00:40 PM 
     --- debug on Wednesday, May 22, 2013 2:00:41 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  SpotlightIndexedData 

/private/var/SpotlightIndexedFolder on  -private-var.txt
  New log created on Wednesday, May 22, 2013 2:00:37 PM 
     --- debug on Wednesday, May 22, 2013 2:00:38 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  SpotlightIndexedData


Note:
on the second and subsequent runnings will will not get a prompt for administration password since the file /private/var/SpotlightIndexedFolder on  -private-var.txt will already exist.

Author: rccharles
    
For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run
      
      
For running shell commands see:
    http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
    
Copyright 2013 rccharles
    GNU General Public License 
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation,  version 3
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>.

    


 *)


on run
    
    global firstRunning
    
    -- Names of the folders in which to write a marker file.  Use Unix names path.
    -- find -x /   -type d -maxdepth 1 
    set folderList to ¬
        {}
    
    
    
    
    -- Write a message into the applescript editor event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    
    
    
    
    set actualCd to do shell script "cd ~;pwd"
    log "actualCd = " & actualCd
    
    
    -- check for alternate sources of the folder list
    if (count folderList) is equal to 0 then
        set inputName to ((path to desktop) as string) & "checkSpotLight-files.txt"
        tell application "Finder"
            if not (exists inputName) then
                set inputName to choose file with prompt "File contain the list of folders"
            end if
        end tell
        log "inputName = " & inputName
        
        set inputFile to open for access inputName
        try
            set theFolderData to read inputFile as «class utf8»
        on error errorMsg
            log "got error >" & errorMsg & "< "
            display dialog "Read error. >" & errorMsg & ¬
                "< Any data in file? " & inputName ¬
                giving up after 20
            return
        end try
        close access inputFile
        
        -- convert to an Applescript list
        -- allow ether lf or cr lineends.  ( not Windows friendly!  &  A bit of a hack )
        -- in an lf file there is no cr's.  In a cr file there is not lf's.
        set theFolderData to alterString(theFolderData, ASCII character 10, return)
        set folderList to textToList(theFolderData, return)
    end if
    
    log "folderList = " & folderList
    
    repeat with theFile in folderList
        
        log "theFile = " & theFile
        if length of theFile is not 0 then
            set firstRunning to ""
            
            set expandPath to alterString(theFile, "~", actualCd)
            log "expandPath = " & expandPath
            
            
            debug("SpotlightIndexedData ", expandPath, "SpotlightIndexedFolder")
        end if
    end repeat
    
    tell application "Finder"
        activate
        display dialog "Done.  You may now search for SpotlightIndexedData and SpotlightIndexedFolder in Spotlight" giving up after 5
    end tell
end run

-- ---------------------------------------------
(*
  Create or add to a log file

  based on log by James Reynolds, 12.18.2000, University of Utah
  called recursively.

  input: 
    text string message writen to file
     expanded posix path to file.  ( no ~ in string )
    file name
   
 *)

on debug(theMessage, thePath, theFileName)
    global firstRunning
    local theSize, startupDiskName, fileToLog, fileReference, appPath, changedPerms
    set lineEnd to ASCII character 10
    
    log "==> debug called"
    log "  theMessage = " & theMessage & " class is " & class of theMessage
    
    log "  thePath = " & thePath
    log "  theFileName = " & theFileName
    
    -- ----------------
    
    set savePerms to ""
    
    -- Does the folder exist?
    
    set classicPath to POSIX file thePath as string
    log "classicPath = " & classicPath
    tell application "Finder"
        if not (exists folder classicPath) then
            display dialog "Could not find folder \"" & thePath & ¬
                "\" ( or lack read permissions). Skipping. " giving up after 15
            return
        end if
    end tell
    
    set imbedPath to theFileName & " on  " & alterString(thePath, "/", "-") & ".txt"
    log "imbedPath = " & imbedPath
    
    set unixPathToLog to (thePath & "/" & imbedPath)
    log "unixPathToLog = " & unixPathToLog
    
    
    set fileToLog to POSIX file unixPathToLog as string
    log "fileToLog = " & fileToLog
    
    -- Can we write to this folder?
    
    try
        set fileReference to (open for access file fileToLog ¬
            with write permission)
    on error errorMsg
        log "got error >" & errorMsg & "< "
        
        -- log an attempted permission change on a folder
        set permFileName to "spewChangePermissionList"
        log "permFileName = " & permFileName & " class is " & permFileName
        
        -- check for recursive looping
        if theFileName is equal to permFileName then
            display dialog "This is bad.  Looks like a loop. thePath = " & thePath ¬
                & " theFileName = " & theFileName
            return
        end if
        
        set permFolder to path to home folder
        set unixPermFolder to POSIX path of permFolder
        log "unixPermFolder = " & unixPermFolder
        
        set permList to do shell script "ls -ld " & quoted form of thePath
        log "permList = " & permList & " class is " & class of permList
        
        set priorRunning to firstRunning -- probably "" anyway.
        -- keep track of the folders we plan on changing
        debug(permList, unixPermFolder, permFileName)
        log "... from debug"
        set firstRunning to priorRunning
        
        -- perserve permissions.
        set savePerms to do shell script "stat -f \"%Lp\" " & quoted form of thePath
        
        -- fix permissions if we can.
        try
            set cmd to "chmod  a+wx  " & quoted form of thePath
            log "cmd = " & cmd
            
            set fromUnix to do shell script cmd with administrator privileges
        on error errMsg
            log "chmod error...>" & errMsg & "< on " & thePath
            display dialog "Could not change permissions. >" & errorMsg & ¬
                "< on " & thePath ¬
                giving up after 20
            return
        end try
        set fileReference to (open for access file fileToLog ¬
            with write permission)
        
    end try
    
    log "firstRunning = " & firstRunning
    set theSize to (get eof fileReference)
    log "theSize = " & theSize
    
    if firstRunning = "" then
        
        -- New file to create?
        if theSize is equal to 0 then
            appendToFile(fileReference, "New log created on " & ((current date) as string) & " " & lineEnd)
            
            
            close access fileReference
            try
                -- file must be closed for finder to change creator type.
                tell application "Finder"
                    -- TextWrangler
                    set the creator type of the file fileToLog ¬
                        to "!Rch"
                end tell
                
            on error errorMsg
                display dialog "warning problem with setting creator type. >" & errorMsg & "< results for " & fileToLog giving up after 30
            end try
            
            set fileReference to (open for access file fileToLog ¬
                with write permission)
            
        end if
        appendToFile(fileReference, "   --- debug on " & ((current date) as string) & "   --- " & lineEnd)
        tell current application
            set appPath to path to current application
        end tell
        appendToFile(fileReference, "   --- current AppleScript is " & appPath & " ---" & lineEnd)
        
        set firstRunning to "running"
    end if
    
    
    appendToFile(fileReference, theMessage & lineEnd)
    
    close access fileReference
    
    -- restore directory to what permsissions it had.
    
    if savePerms is not equal to "" then
        try
            set cmd to "chmod  " & savePerms & " " & quoted form of thePath
            log "cmd = " & cmd
            
            set fromUnix to do shell script cmd with administrator privileges
        on error errMsg
            log "chmod error on restoring file ...>" & errMsg & "< on " & thePath
            display dialog "Could not change permissions on attempting retore. >" & errorMsg & ¬
                "< on " & thePath ¬
                giving up after 20
            return
        end try
    end if
    
    
    
end debug

-- ------------------------------------------------------
on alterString(thisText, delim, replacement)
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    try
        set resultList to every text item of thisText
        set text item delimiters to replacement
        set resultString to resultList as string
        set my text item delimiters to tid
    on error
        set my text item delimiters to tid
    end try
    return resultString
end alterString

-- ------------------------------------------------------
-- textToList was found here:
-- http://macscripter.net/viewtopic.php?id=15423

on textToList(thisText, delim)
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    
    try
        set resultList to every text item of thisText
        set my text item delimiters to tid
    on error
        set my text item delimiters to tid
    end try
    return resultList
end textToList

-- ----------------------------------------------------------

on appendToFile(fileId, theData)
    
    local theSize, writeWhere
    log "--> class is " & class of theData
    set theSize to (get eof fileId)
    set writeWhere to theSize + 1 as integer
    write theData to fileId starting at writeWhere as «class utf8»
    
end appendToFile

Jun 8, 2013 3:49 PM in response to rccharles

Here is how to get rid of the files:

Macintosh-HD -> Applications -> Utilities -> Terminal

# press return to run the command.

sudo find -x / \( -iname "SpotlightIndexedFolder on *.txt" \) -type f -print -exec ls -l {} \;


change ls -l to rm when you have enough courage. It should print out what is being deleted. I ran the command without -print. I have courage.



(* 

Place marker files in a list of folders so we can "see" what folders 
spotlight indexes.

How to use in SpotLight:
  Once your run this script, you may use spotlight to search for 
    SpotlightIndexedData
    SpotlightIndexedFolder 
  
  SpotlightIndexedData is inside the file.
  SpotlightIndexedFolder starts the file name. The file name contains the path.

Input:
 This script needs a list of folders to add the "SpotlightIndexedFolder ... " file.

 You may add folders you do not have write permission.  You will be prompted 
 for your addministrator password. Run as administrator.

 to gather a list of folders, you may use the terminal find command.

If for instance, you want to add files to 
  all folders in the root directory ( / ), use the find command to get the list.
  Macintosh-HD -> Applications -> Utilities -> Terminal
  # press return to run the command.
  find -x /   -type d -maxdepth 1 

 For all three cases, you may use the ~/ to represent your home folder 
  ( whoever is running this script ).  

 (1) 
  The folderList variable below to contain the list of folders
  you want to add a file too.  You will need to paste here then add the Applescript qutoes etc. The files are in
  Posix ( unix ) syntax.  

  example:
  set folderList to ¬
        {¬
            "~/notfoundtoday", ¬
            "/private/var", ¬
            "~/Desktop"}
            
 (2)
 In case the list folderList is empty, this applescript will read the list from
 ~/Desktop/list-SpotLight-files.txt.  Place one folder per line.

 example ~/Desktop/list-SpotLight-files.txt.:
   /Users/mac/darwinports
   ~/Desktop  
   /private
   
 (3) 
 When ~/Desktop/list-SpotLight-files.txt isn't present, a promt for the file will appear.


Output for the first run.  Ouput will change slightly for second run.

            ~/notfoundtoday
            /private/var
            ~/Desktop

warning message since file "~/notfoundtoday" does not exist

prompt for admin password to access "/private/var"


~/spewChangePermissionList on  -Users-mac-.txt
  New log created on Wednesday, May 22, 2013 2:00:29 PM 
     --- debug on Wednesday, May 22, 2013 2:00:29 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  drwxr-xr-x   25 root  wheel  850 May 22 13:59 /private/var

~/Desktop/SpotlightIndexedFolder on  -Users-mac-Desktop.txt
  New log created on Wednesday, May 22, 2013 2:00:40 PM 
     --- debug on Wednesday, May 22, 2013 2:00:41 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  SpotlightIndexedData 

/private/var/SpotlightIndexedFolder on  -private-var.txt
  New log created on Wednesday, May 22, 2013 2:00:37 PM 
     --- debug on Wednesday, May 22, 2013 2:00:38 PM   --- 
     --- current AppleScript is Ext-Remainder:Applications:AppleScript:Script Editor.app: ---
  SpotlightIndexedData


Note:
on the second and subsequent runnings will will not get a prompt for administration password since the file /private/var/SpotlightIndexedFolder on  -private-var.txt will already exist.

Author: rccharles
    
For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run
      
      
For running shell commands see:
    http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
    
Copyright 2013 rccharles
    GNU General Public License 
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation,  version 3
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>.

    
 *)


on run
    
    global firstRunning
    
    -- Names of the folders in which to write a marker file.  Use Unix names path.
    -- find -x /   -type d -maxdepth 1 
    set folderList to ¬
        {}
    
    
    
    
    -- Write a message into the applescript editor event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    
    
    
    
    set actualCd to do shell script "cd ~;pwd"
    log "actualCd = " & actualCd
    
    
    -- check for alternate sources of the folder list
    if (count folderList) is equal to 0 then
        
        set inputName to ((path to desktop) as string) & "list-SpotLight-files.txt"
        log "inputName = " & inputName
        tell application "Finder"
            if not (exists file inputName) then
                set inputName to choose file with prompt "File contain the list of folders"
                log "inputName = " & inputName
            end if
        end tell
        
        set inputFile to open for access inputName
        try
            set theFolderData to read inputFile as «class utf8»
        on error errorMsg
            close access inputFile
            log "got error >" & errorMsg & "< "
            display dialog "Read error. >" & errorMsg & ¬
                "< Any data in file? " & inputName ¬
                giving up after 20
            return
        end try
        close access inputFile
        
        -- convert to an Applescript list
        -- allow ether lf or cr lineends.  ( not Windows friendly!  &  A bit of a hack )
        -- in an lf file there is no cr's.  In a cr file there is not lf's.
        set theFolderData to alterString(theFolderData, ASCII character 10, return)
        set folderList to textToList(theFolderData, return)
    end if
    
    log "folderList = " & folderList
    
    repeat with theFile in folderList
        
        log "theFile = :" & theFile & ":"
        if length of theFile is not 0 then
            set firstRunning to ""
            
            set expandPath to alterString(theFile, "~/", actualCd & "/")
            log "expandPath = " & expandPath
            
            
            debug("SpotlightIndexedData ", expandPath, "SpotlightIndexedFolder")
        end if
    end repeat
    
    tell application "Finder"
        activate
        display dialog "Done.  You may now search for SpotlightIndexedData and SpotlightIndexedFolder in Spotlight" giving up after 5
    end tell
end run

-- ---------------------------------------------
(*
  Create or add to a log file

  based on log by James Reynolds, 12.18.2000, University of Utah
  called recursively.

  input: 
    text string message written to file
    expanded posix path to file.  ( no ~ in string )
    file name
   
 *)

on debug(theMessage, thePath, theFileName)
    global firstRunning
    local theSize, startupDiskName, fileToLog, fileReference, appPath, changedPerms
    
    log "==> debug called"
    log "  theMessage = " & theMessage & " class is " & class of theMessage
    
    log "  thePath = " & thePath
    log "  theFileName = " & theFileName
    
    -- ----------------
    set lineEnd to ASCII character 10
    set rootNeeded to ""
    set permFileName to "spewChangePermissionList"
    log "permFileName = " & permFileName & " class is " & permFileName
    
    -- Does the folder exist?
    -- convert to mac path
    set classicPath to POSIX file thePath as string
    log "classicPath = " & classicPath
    
    set imbedPath to theFileName & " on  " & alterString(thePath, "/", ">") & ".txt"
    log "imbedPath = " & imbedPath
    
    set unixPathToLog to (thePath & "/" & imbedPath)
    log "unixPathToLog = " & unixPathToLog
    
    -- conveert to mac file name
    set fileToLog to POSIX file unixPathToLog as string
    log "fileToLog = " & fileToLog
    
    -- Can we write to this folder?
    
    try
        set thisCmd to "touch " & quoted form of unixPathToLog
        log "thisCmd = " & thisCmd
        
        -- could have read only access to the folder
        do shell script thisCmd
        
        try
            set permList to do shell script "ls -ld " & quoted form of thePath
            
        on error errorMsg
            display dialog "Could not gather folder info.  >" & errorMsg & ¬
                "< on " & fileToLog ¬
                giving up after 20
            return
            
        end try
    on error errorMsg
        log "got error >" & errorMsg & "< "
        
        -- The folder could exist, but we might not be able to see it. 
        -- ... , but we might not be able to write a file to it.
        -- It's a file.
        
        -- check if it is a file < would be a user error if so >
        -- redundant, but we but out a more specific error message
        tell application "Finder"
            set fileExist to exists file classicPath
        end tell
        log "fileExist = " & fileExist
        if fileExist then
            -- its a file!
            display dialog "Looking for a folder, but it's a file!  The file is: " & classicPath ¬
                giving up after 20
            return
        end if
        
        -- Try harder.
        set thisCmd to "ls -ld " & quoted form of thePath
        log "thisCmd = " & thisCmd
        try
            -- could have read only access to the folder
            set permList to do shell script thisCmd
        on error errMsg
            log "Trying with administrator"
            try
                -- did not see because we do not have privileges
                set permList to do shell script thisCmd with administrator privileges
                
                
            on error errMsg
                log "ls -ld error...>" & errMsg & "< on " & thePath
                display dialog "Looking for a folder, but found.  >" & errorMsg & ¬
                    "< on " & thePath ¬
                    giving up after 20
                return
            end try
            
        end try
        
        -- verify we have a directory
        if first character of permList is not equal to "d" then
            display dialog "Looking for a folder, but found something  " & ¬
                " else on " & thePath & " file attributes " & permList ¬
                giving up after 20
            return
        end if
        
        set rootNeeded to "yes"
        
        -- log an attempted permission change on a folder
        
        set permFolder to path to home folder
        set unixPermFolder to POSIX path of permFolder
        log "unixPermFolder = " & unixPermFolder
        
        
        
        log "permList = " & permList & " class is " & class of permList
        
        set priorRunning to firstRunning -- probably "" anyway.
        -- keep track of the folders we plan on changing
        debug(permList, unixPermFolder, permFileName)
        log "... from debug"
        set firstRunning to priorRunning
        
        
        set thisCmd to "touch " & quoted form of unixPathToLog
        log "thisCmd = " & thisCmd
        
        -- could have read only access to the folder
        try
            do shell script thisCmd with administrator privileges
        on error errMsg
            log "touch error...>" & errMsg & "< on " & unixPathToLog
            
            display dialog "Looking for a folder, but found.  >" & errorMsg & ¬
                "< on " & unixPathToLog ¬
                giving up after 20
            return
        end try
        
    end try
    
    log "firstRunning = " & firstRunning
    
    if firstRunning = "" then
        log "First running case."
        
        -- get file size
        set thisCmd to "stat -f \"%z\" " & quoted form of unixPathToLog
        log "thisCmd = " & thisCmd
        if rootNeeded is equal to "yes" then
            set theSize to do shell script thisCmd with administrator privileges
        else
            set theSize to do shell script thisCmd
        end if
        set theSize to theSize as integer
        log "theSize = " & theSize
        
        -- New file?
        if theSize is equal to 0 then
            -- create first time headers
            appendToFile(unixPathToLog, "New log created on " & ((current date) as string) & " " & lineEnd, rootNeeded)
            
            if rootNeeded is equal to "yes" then
                try
                    set thisCmd to "/Developer/Tools/SetFile -c '!Rch' " & quoted form of unixPathToLog
                    log "thisCmd = " & thisCmd
                    set theResult to do shell script thisCmd with administrator privileges
                on error errorMsg
                    display dialog "warning problem with setting creator type. You need to have installed developer tools for the SetFile command.  >" & errorMsg & "< results for " & unixPathToLog giving up after 30
                end try
            else
                try
                    tell application "Finder"
                        -- TextWrangler
                        set the creator type of the file fileToLog ¬
                            to "!Rch"
                        
                    end tell
                on error errorMsg
                    display dialog "warning problem with setting creator type. >" & errorMsg & "< results for " & fileToLog giving up after 30
                end try
            end if
            
            
            
            
        end if
        appendToFile(unixPathToLog, "   --- debug on " & ((current date) as string) & "   --- " & lineEnd, rootNeeded)
        tell current application
            set appPath to path to current application
        end tell
        appendToFile(unixPathToLog, "   --- current AppleScript is " & appPath & " ---" & lineEnd, rootNeeded)
        
        set firstRunning to "running"
    end if
    
    
    appendToFile(unixPathToLog, theMessage & lineEnd, rootNeeded)
    
    
    
    
end debug

-- ------------------------------------------------------
on alterString(thisText, delim, replacement)
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    try
        set resultList to every text item of thisText
        set text item delimiters to replacement
        set resultString to resultList as string
        set my text item delimiters to tid
    on error
        set my text item delimiters to tid
    end try
    return resultString
end alterString

-- ------------------------------------------------------
-- textToList was found here:
-- http://macscripter.net/viewtopic.php?id=15423

on textToList(thisText, delim)
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    
    try
        set resultList to every text item of thisText
        set my text item delimiters to tid
    on error
        set my text item delimiters to tid
    end try
    return resultList
end textToList

-- ----------------------------------------------------------

on appendToFile(unixPathToLog, theData, rootNeeded)
    log "unixPathToLog = " & unixPathToLog
    log "--> class is " & class of theData
    
    set thisCmd to "echo -n " & quoted form of theData & " | cat >>" & quoted form of unixPathToLog
    log "thisCmd = " & thisCmd
    try
        -- could have read only access to the folder
        if rootNeeded is equal to "yes" then
            set fromUnix to do shell script thisCmd with administrator privileges
        else
            set fromUnix to do shell script thisCmd
        end if
    on error errMsg
        log " errMsg = " & errMsg
        
    end try
    
end appendToFile

Taming Spotlight

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.