Specific Keywords (Metadata)

I use Bridge to append Metadata to the Keywords field. I would like the script below to get a file count of the PDFs containing ONLY the Keyword “correction” on selected folders but I’m having issues in how to get the keyword “correction”. The results I’m getting are (null).

Also another issue I’m having that Excel not always opens the text file. If I quit Excel, relaunch it works but sometimes I get error “Microsoft Excel got an error: Can’t continue open text file.” number -1708.

Thank you in advance:)

set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with multiple selections allowed without invisibles
set results to ""

repeat with i from 1 to (count target_folder)
	set thisFolder to (POSIX path of item i of target_folder)
	
	--Find & count all PDFs in the folders selected that keyword is "correction"
	set fileCount to do shell script "mdls -name kMDItemKeywords -raw " & quoted form of thisFolder
	
	-->> HOW DO I GET THE KEYWORD "corrections"?
	if thisFolder contains "correction" then
		display dialog "NNNXX"
	end if
	set results to (results & "" & tab & "CORRECTS=" & tab & fileCount & return)
	
end repeat


--write results to a txt file
set theFilePath to (path to desktop folder as text) & "PDF File Count.txt"
set theFile to open for access file theFilePath with write permission
try
	set eof of theFile to 0
	--write results to file theFilePath
	write results to theFile
	close access theFile
on error
	close access theFile
end try

display dialog "done"


--Open the txt file in Excel
tell application "Microsoft Excel"
	activate
	set display alerts to false -- 2004 dictionary says it only applies to VB macros, but it actually supresses most dialogs while running scripts
	set screen updating to true -- By setting this to false you can greatly improve speed
	open text file filename "PDF File Count.txt"
	
end tell

I also tried

	set fileCount to do shell script "mdls -name kMDItemKeywords = ('corrections')" & quoted form of thisFolder

I get this error:
error "sh: -c: line 0: syntax error near unexpected token (' sh: -c: line 0: mdls -name kMDItemKeywords = (‘corrections’)’

Hello.

You have to use mdfind to find all files with an attribute, mdls, just returns the attributes of one file.

Here is a little example snippet. There are several walkthroughs on using mdfind out there, most notably Adam Bell’s article but I have also read an article over at MacDevCenter that I thought well of: The Power of mdfind

set foundFiles to paragraphs of (do shell script "mdfind -onlyin / '  kMDItemContentType == com.apple.applescript.script && kMDItemTextContent == \"*" & soughtText & "*\"[c]'")

Thanks McUsrII. The articles are really good & I kind of understand it but not much. I changed the script based on your snipet & the article. the files I’m testing are on my desktop & it contains 11 pdfs which 6 of them have keywords “correction” But the mdfind is finding all files that are on my desktop & the ones on the test folder. The results I’m looking is total of files with the keyword correction = 6.

set target_folder to choose folder with prompt "Choose target folders containing only PDFs to count files" with multiple selections allowed without invisibles
set results to ""

repeat with i from 1 to (count target_folder)
	set thisFolder to (POSIX path of item i of target_folder)
	-->> From MacUser -->>set fileCount to paragraphs of (do shell script "mdfind -onlyin / '  kMDItemContentType == com.apple.applescript.script && kMDItemTextContent == \"*" & "*\"[c]'")
	
	
	set fileCount to paragraphs of (do shell script "mdfind -onlyin / '  kMDItemKeywords == correction'") & quoted form of thisFolder
	set results to (results & "" & tab & "CORRECTS=" & tab & fileCount & return)
	
end repeat

set theFilePath to (path to desktop folder as string) & "PDF File Count.txt"
set theFile to open for access file theFilePath with write permission
try
	set eof of theFile to 0
	--write results to file theFilePath
	write results to theFile
	close access theFile
on error
	close access theFile
end try

Hello.

I guess it must be this way, if “correction” is a user defined keyword:

Grab all the files with that keyword using mdfind.

Iterate over the files with mdls, and filter out any files, whose value of “correction” is not 6.

There should be several methods residing in Code Exchange, regarding extracting values from mdls. :slight_smile:

Have a look at post #35 in this thread: MacScripter / Generic display of Spotlight Metadata for file(s)

Edit

I think you are able to just use the mdls raw on your own, and create a grep pattern that contains both the keyword, and the number. It should be easy to both make it and verify it in a Terminal window. :slight_smile: