Remove Keywords

Get help using and writing Nisus Writer Pro macros.
Post Reply
jb
Posts: 92
Joined: 2007-11-09 15:27:25

Remove Keywords

Post 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
User avatar
martin
Official Nisus Person
Posts: 5228
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Remove Keywords

Post 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.
jb
Posts: 92
Joined: 2007-11-09 15:27:25

Re: Remove Keywords

Post 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.
User avatar
martin
Official Nisus Person
Posts: 5228
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Remove Keywords

Post by martin »

No problem. I hope your universe brightens up soon!
User avatar
tbo
Posts: 12
Joined: 2005-12-11 06:45:07
Location: Munich, Germany
Contact:

Re: Remove Keywords

Post 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!
Thomas B.
Post Reply