0

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.

3
  • What information, and in what form, do you want in the Comment of the Get Info sheet? If you want the fully qualified pathname of the file you can use the following statement within the tell item i to try block: set thePathname to POSIX path of i – user3439894 May 13 '16 at 11:52
  • The exact form of the info is not so critical, I'm just going to give them a completely different name and move them out of their defining subfolders into one single folder, so adding some way of telling where the file came from as an error control will work. The script below from Graham with your modification works great. – groo May 13 '16 at 14:14
  • Based on what you said in your comment, I'd suggest just setting the fully qualified pathname of each file into it's Comment field as it will contain the original name of the file as well as its location at the time of running the script. It will be easier to parse that later knowing the comment is in the form of /path/to/filename vs. having both the qualified pathname and the filename separately. – user3439894 May 13 '16 at 15:57
0

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.

2
  • Did you test that code before posting it? I ask because it errors out for me. You might want to change set tPath to thePathname to POSIX path of i to just: set tPath to POSIX path of i – user3439894 May 13 '16 at 13:05
  • script works great, thanks a lot both of you! Also for the explanations, I'm totally new to code and would love learning this eventually. – groo May 13 '16 at 13:52

You must log in to answer this question.

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