Script to write into Spotlight notes

Dear All,

I am looking for some help here. I am looking for a script that is able to extract a certain number of characters from the names my files and paste them into the spotlight comments field of this file.

For clarity:
I have a bunch of files which represent my spendings throughout the year. I referenced them to my account statements and give every spending a certain string of name with a locked number of characters (e.g: 2017-0801-500)
I had this string of name in front of every file I kept in the finder. Now, when switching to DTPO, I’d like to get rid of this 13-character-string and paste it directly into the spotlight-comment-field of the file. Is there any chance someone can help me out with a little script? Unfortunately, I have no clue of scripting at all, so I need some advisory… :wink:

Thanks a lot in advance and hope someone is able to help out here…

Best from Germany,

Sure thing. Is that “code” is consistently prefixing the filename?

Hey Bluefrog,
yes, the code is always in front of the filename, devided by an underscore (_) from the rest of the filename. The files are located in only one folder, so the script should just affect one folder or maybe just pre-selected files.

Are you only using an underscore between the “code” and the rest of the name?

Yes, only an underscore.

I am approaching this as if you’re wanting to learn something (ie. I’m leaving out my fancy tricks), so here’s an approach with a little bit of error checking too…

tell application id "DNtp"
	set sel to (selection as list)
	if sel is not {} then -- If there are selected files, proceed.
		repeat with thisRecord in (selection as list) -- Loop through the files in the selection
			set fName to (name of thisRecord as string)
			try -- This lets you attempt this process but fail gracefully
				if (character 14 of fName) is "_" then
					-- The first 13 characters are your code, but you have an underscore at the end of it, so we check if the 14th character is an underscore. 
					-- This could occur in other files, but since this is for your single purpose, it's less likely to be an issue.
					set code to characters 1 thru 13 of fName -- Get the first 13 characters for the code.
					set newName to characters 15 thru -1 of fName -- Get character 15 through the end of the name for the new filename.
					-- code and newName are lists of characters at this point, so they need to be turned into (coerced to) a string to be usable for this script.
					set code to code as string
					set newName to newName as string
					-- Put the code in the Spotlight comments
					-- But let's preserve any current comments first
					set currentComments to comment of thisRecord -- This will work, even if there are no comments on the file.
					-- We can even check if the code has already been added and don't add it again if it's found.
					if currentComments does not contain code then
						set comment of thisRecord to (code & return & currentComments)
						-- You can obviously reorder the comment, or even add other strings.
					else -- This else section isn't required. It's a nicety to show how log message can be used, and just some more basic error-checking.
						log message fName info code & " already in the comments."
					end if
				end if
				-- Finally, we set the name of the file
				set name of thisRecord to newName
			on error -- If there is an issue, do this…
				return -- Instead of an alert, we're just going to ignore it.
			end try
		end repeat
	else -- Nothing is selected, so send a message
		display alert "Please select a file to process."
	end if
end tell

I commented this pretty liberally so you should be able to paste this into Script Editor, press Script > Compile, and see the comments plainly.

This will work on selected files in DEVONthink.

Note: For debugging and learning purposes, I suggest commenting out the try - end try sections so you can watch it error. Cheers!

1 Like

Hey Bluefrog,

I was just able to test the script. Thanks a lot, it works perfect. Thanks a lot for this big time-changer. Just one quick question:

What is necessary to advance this script a little bit and just check for the first underscore (_) in the code and let the script cut it there? I am asking because I totally forgot that I have some codes which are longer than the 13 characters, sometimes 14 up to 17 characters. :wink:

So I was wondering if it might help if the script checks for the first underscore and divides the code there from the filename. Because this might be the perfect setup.

Best and thanks.

Details, details.

Are you certain you have no files with more than the one underscore in the name?

That’s why i said first underscore :slight_smile:

Then you could use this…

set characterOffset to offset of "_" in t -- Locate the first instance of an underscore in the name.

if characterOffset is not 0 then
-- Note: This does NOT validate anything before the underscore. "myfile_has_no_code" would also match.
set code to characters 1 thru (characterOffset - 1) of t as string -- Get the characters before the underscore.
set newName to characters (characterOffset + 1) thru -1 of t as string -- Get the characters after the underscore.
1 Like