MacOS – Apple Script for copying path and name and pasting it in Spotlight Comment box Mac OS X 10.9.5 (and 10.11.4)

applescriptfindermacosspotlight

I have a bunch of files to rename from someone else that sit inside folders and subfolders, and would like to copy the path and file name to the Spotlight comment of each file before I do anything with them.

The script in answer to this question for adding the file name to the Spotlight comments works great, but I don't have the skills to modify it to include the path. I have tried replacing a few items with path or POSIX path in a meaningful way, but can't get it to work, nor do I find the commands elsewhere. Any help is appreciated.

Best Answer

Building upon jackjr300's answer, you can add the path to the comment with this modified script:

set input to choose file with prompt "Select files (to put the file name into the Spotlight Comment)" with multiple selections allowed
tell application "Finder"
    repeat with i in input
        tell item i to try
            set tName to (get name)
            set tPath to POSIX path of i -- get the path of the file
            set comment to tName & " - " & tPath -- this set the comment to the name of the file, and delete the existing comment
            set name to "Done_" & tName
        end try
    end repeat
end tell

The changes to the original script are these two lines:

set tPath to POSIX path of i -- get the path of the file
set comment to tName & " - " & tPath -- this set the comment to the name of the file, and delete the existing comment

The first line sets tPath to a string containing the path to i. In this case i is the file being examined.

The second line appends the path to the file's comment using the segment: & " - " & tPath. The section between the two double-quotes can be changed to something other than a hyphen.