Add Comment to a File -- Desktop/Finder/PathFinder

I have been trying to figure out if it is possible to add a comment to a file that is selected on/in the Desktop, Finder, or Pathfinder. I would like to trigger a macro to ask for user input and apply it as a comment to the selected file or a selection of files. Can anyone give me a few suggestions if this is possible.

I do have the Path Finder » Set Tags of Selected Items to Red to build upon so I think with some modification this macro could be tweaked to accomplish what I want.

Better yet though is if it would check to see if the file was selected on the Desktop, Finder or PathFinder. Finder could be excluded as PathFinder is my file manager of choice.

Thanks for any help!

Hey Gregory,

You can’t do this directly with Path Finder – however you can get a list of items selected in Path Finder, turn them into aliases, and operate on them using the Finder.

This AppleScript will place the given comment in the Get-Info comment of every selected item in the Finder.

------------------------------------------------------------
set theComment to "Your commentary is unnecessary and irrelevant!"

tell application "Finder"
  set finderSelectionList to selection as alias list
  if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
  
  repeat with i in finderSelectionList
    set comment of i to theComment
  end repeat
  
end tell
------------------------------------------------------------

This AppleScript will place the given comment in the Get-Info comment of every selected item in Path Finder.

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/12/20 16:10
# dMod: 2015/12/20 16:16 
# Appl: Path Finder & Finder
# Task: Add comments to selected items in Path Finder
# Tags: @Applescript, @Script, @Path_Finder, @Add, @Comments, @Selection
------------------------------------------------------------

set theComment to "Your commentary is unnecessary and irrelevant!"

tell application "Path Finder"
  set pfSelectionList to selection
  if pfSelectionList ≠ missing value then
    repeat with i in pfSelectionList
      set (contents of i) to POSIX path of (contents of i)
    end repeat
  else
    error "Nothing is selected in Path Finder!"
  end if
end tell

repeat with i in pfSelectionList
  set (contents of i) to alias POSIX file (contents of i)
end repeat

tell application "Finder"
  repeat with i in pfSelectionList
    set comment of i to theComment
  end repeat
end tell

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

You can use a Prompt for User Input action to collect the text for the comment.

You can get the text from a Keyboard Maestro variable into AppleScript easily:

https://wiki.keyboardmaestro.com/AppleScript

-Chris

You can set a comment on a file using the Set File Attribute action.

Use the Prompt for User Input action to get the comment, and see the Working with the Finder Selection topic for working with the Finder's selection.

3 Likes

It is truly amazing the incredible breadth of features provided by KM. :astonished:

1 Like

Thanks Chris and Peter, I have not had any time yet to put this together but will try over the holiday. These features are new to me so I will read/play so that I understand how variables are created and passed.

Cheers!

Thanks again for the help! I could not have done this without your suggestions and scripts.

This is what I have come up with for the Pathfinder portion of what I was trying to do.

Now i would like to add to this if possible. Is there any way to check and see if the file/files are selected on the Desktop or in PathFinder and branch to appropriate routine.

Any suggestions would be welcome......Greg

Hey Greg,

You mean the Finder or Path Finder?

The Desktop can be accessed from either one.

-Chris

I found this Script which does nearly what I need it to do. As I do have no programming skills I'm asking If someone could help me modify the script (pathfinder part of it) to append to the comments if there are already comments in the file.

Thanks for any help!

Hey @Umberto,

This ought to do it.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/12/20 16:10
# dMod: 2021/10/14 10:18
# Appl: Path Finder & Finder
# Task: Add comments to selected items in Path Finder
#     : Append new comment if there is an existing comment.
# Tags: @Applescript, @Script, @Path_Finder, @Add, @Comments, @Selection
--------------------------------------------------------

set newComment to "Your commentary is unnecessary and irrelevant!"

tell application "Path Finder"
   set pfSelectionList to selection
   if pfSelectionList ≠ missing value then
      repeat with selectedItem in pfSelectionList
         set (contents of selectedItem) to POSIX path of (contents of selectedItem)
      end repeat
   else
      error "Nothing is selected in Path Finder!"
   end if
end tell

repeat with selectedItem in pfSelectionList
   set (contents of selectedItem) to alias POSIX file (contents of selectedItem)
end repeat

tell application "Finder"
   repeat with theItem in pfSelectionList
      
      set oldItemComment to theItem's comment
      
      if oldItemComment ≠ "" then
         set newComment to oldItemComment & space & newComment
         set theItem's comment to newComment
      else
         set theItem's comment to newComment
      end if
      
   end repeat
end tell

--------------------------------------------------------
1 Like

Hi Chris,

Works as expected :slight_smile:

Thanks for taking your Time and and your help!

Umberto

1 Like

Hi @ccstone,
Is possible to write the comment and not use the one on the code with a dialog box or other. With this I can write different comments per file.

Thanks
Bruno

Hey Bruno,

Sure. Search Google for "Applescript display dialogue".

Note the "default answer" parameter in the dictionary. You need that in order to be able to insert text into the dialogue.

-Chris

Hi Chris,
I don't know code... I've tried but I was unable to find the solution. Thanks for your help.

Bruno

As @ccstone has said, Google is your friend here.

In his AppleScript above, the comment text is an AppleScript variable called newComment

The first line of script sets this variable to "Your commentary is unnecessary and irrelevant!"

set newComment to "Your commentary is unnecessary and irrelevant!"

So, you can replace that one line, with one that instead asks for some text for the Variable and this will do that:

set newComment to the text returned of (display dialog "Please type a comment" default answer "")

This will set the AppleScript variable newComment to whatever you type into the dialog box.

2 Likes

@Zabobon,
Thank you! works perfectly :star_struck:
This community is amazing... :wave:

Bruno

1 Like

there's a small inconvenient... it doesn't replace an previous comment... sorry to bother again :worried:

Anyway to do this?

Bruno

The script is deliberately designed to add your comment to anything already there:

      set oldItemComment to theItem's comment
      
      if oldItemComment ≠ "" then
         set newComment to oldItemComment & space & newComment
         set theItem's comment to newComment
      else
         set theItem's comment to newComment
      end if
  • Save the current comment if there is one
  • If item already has a comment, add the new comment to the end of the old then apply it all to the item
  • If it doesn't, apply just the new comment to the item

If you want to overwrite any/all comments with your new one then try changing the repeat loop to:

repeat with theItem in pfSelectionList
    set theItem's comment to newComment
end repeat
2 Likes

As @Nige_S has just written, the last version of the AppleScript is designed to append the new comment rather than replace it. There are earlier versions in Post #2 that replace the comments.

2 Likes

Thank you all! :wave:

The following script works perfectly for me to replace comment:

set newComment to the text returned of (display dialog "Please type a comment" default answer "")

set theComment to ""

tell application "Path Finder"
	set pfSelectionList to selection
	if pfSelectionList ≠ missing value then
		repeat with i in pfSelectionList
			set (contents of i) to POSIX path of (contents of i)
		end repeat
	else
		error "Nothing is selected in Path Finder!"
	end if
end tell

repeat with i in pfSelectionList
	set (contents of i) to alias POSIX file (contents of i)
end repeat

tell application "Finder"
	repeat with i in pfSelectionList
		set comment of i to theComment
	end repeat
end tell

tell application "Finder"
	repeat with i in pfSelectionList
		set comment of i to theComment
	end repeat
end tell

tell application "Finder"
	repeat with theItem in pfSelectionList
		
		set oldItemComment to theItem's comment
		
		if oldItemComment ≠ "" then
			set newComment to oldItemComment & space & newComment
			set theItem's comment to newComment
		else
			set theItem's comment to newComment
		end if
		
	end repeat
end tell
1 Like