Page 1 of 1

Remove Keywords

Posted: 2009-10-25 06:25:37
by jb
A demon entered while I was asleep and added several keywords to about 400 rtf files. Keywords as in Finder keywords. Of course I want to get rid of these, since they reflect a dark and strange view of the universe. These keywords are editable via File > Properties, of course. How difficult would it be to write a macro to perform a batch operation to remove keywords?
Thanks

Re: Remove Keywords

Posted: 2009-10-26 15:47:09
by martin
The macro isn't too hard, but it's not terribly efficient. It works by opening up each file in a NWP window, altering the keywords, and then resaving the whole file back out. It operates on all files in a single folder you select at the start, but doesn't descend into subfolders.

Code: Select all

$fileExts = Array.new('rtf', 'zrtf')	# which file types to process

# ask user what folder to batch process
$folderPath = Choose Folder
If ! $folderPath # user cancelled
	Exit
End

# collect all files of the proper type
$fileNames = File.namesInFolderAtPath($folderPath)
$filePaths = Array.new
ForEach $name in $fileNames
	$ext = $name.filePathExtension
	If -1 != $fileExts.indexOfValue($ext) 
		$path = $folderPath.filePathByAppendingComponent($name)
		$isFolder = File.isFolderAtPath($path)
		If ! $isFolder
			$filePaths.appendValue($path)
		End
	End
End

# make sure the user knows how many files will be processed
$fileCount = $filePaths.count
If 0 == $fileCount
	Exit 'No files of the correct type found in that folder.'
End
$choice = Prompt "Are you sure you want to process $fileCount files?", '', 'Process', 'Cancel'
If 'Process' != $choice
	Exit
End

# process file
ForEach $filePath in $filePaths
	Open $filePath	
	$doc = Document.active
	$doc.setProperty('keywords', '')
	Save
	$doc.close
End
The line that starts "$doc.setProperty" changes the keybwords to the empty string, which clears the property. Let me know if you have any questions.

Re: Remove Keywords

Posted: 2009-10-26 16:51:39
by jb
I'm glad you can say it's not too hard, because it looks like quite a bit of work.

Thank you very much, Martin.

This will make the job much easier.

Re: Remove Keywords

Posted: 2009-10-26 17:43:38
by martin
No problem. I hope your universe brightens up soon!

Re: Remove Keywords

Posted: 2009-12-28 00:38:44
by tbo
Thanks Martin for the macro. I used it to make it work the other way round (inserting a keyword). Now the doc manager finds the required docs via smart filter. I replaced the empty ' ' with '[keyword]' -- and there you are. Thanks a lot!