More clipboard tools - smart cut, smart copy.

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
loulesko
Posts: 89
Joined: 2008-01-20 11:08:35
Location: California
Contact:

More clipboard tools - smart cut, smart copy.

Post by loulesko » 2011-09-05 15:53:06

These macros take the selected text and cut or copy to the next available clipboard that's not the number 1 clipboard. If the clipboard doesn't exist, it creates a new one.

This code has been updated from its original based on Philip's suggestions below.

Smart Copy

Code: Select all

# Copy and save to next empty clipboard
# Thanks to Philip (phspaelti) from the forum who helped make this macro awesome.
#####
Define Command AddClipboard
   $n = 0
   While Clipboard Exists “Clipboard $n”
      $n +=1
   End
   New Clipboard “Clipboard $n”
   $newClipboard = Clipboard Index “Clipboard $n”
   Return $newClipboard
End
#####
$doc = Document.active
$doctext = $doc.text
$count = Clipboard Count
If $count < 2
$newClipboard = AddClipboard
$count = Clipboard Count
End
$n = 2
$range = TextSelection.activeRange
$text = $doctext.subtextInRange($range)
If !$text
Prompt ‘Nothing selected.’,’‘,’OK’
exit
End
# Go through clipboards to find an empty one.
While $n <= $count
$clipboardName = $n
$clipboardData = Read Clipboard $clipboardName
If !$clipboardData
Write Clipboard $text, $clipboardName
exit
End
$n += 1
End
# If there are no empty clipboards, create a new one.
$newClipboard = AddClipboard
Write Clipboard $text, $newClipboard
Smart Cut

Code: Select all

# Copy and save to next empty clipboard
# Thanks to Philip (phspaelti) from the forum who helped make this macro awesome.
#####
Define Command AddClipboard
   $n = 0
   While Clipboard Exists “Clipboard $n”
      $n +=1
   End
   New Clipboard “Clipboard $n”
   $newClipboard = Clipboard Index “Clipboard $n”
   Return $newClipboard
End
#####
$doc = Document.active
$doctext = $doc.text
$count = Clipboard Count
If $count < 2
$newClipboard = AddClipboard
$count = Clipboard Count
End
$n = 2
$range = TextSelection.activeRange
$text = $doctext.subtextInRange($range)
$doctext.deleteInRange($range) 
If !$text
Prompt ‘Nothing selected.’,’‘,’OK’
exit
End
# Go through clipboards to find an empty one.
While $n <= $count
$clipboardName = $n
$clipboardData = Read Clipboard $clipboardName
If !$clipboardData
Write Clipboard $text, $clipboardName
exit
End
$n += 1
End
# If there are no empty clipboards, create a new one.
$newClipboard = AddClipboard
Write Clipboard $text, $newClipboard
Last edited by loulesko on 2011-09-06 23:03:09, edited 2 times in total.

User avatar
phspaelti
Posts: 889
Joined: 2007-02-07 00:58:12
Location: Japan

Re: More clipboard tools - smart cut, smart copy.

Post by phspaelti » 2011-09-06 20:06:24

Hi Lou,
congratulations on your cool macros! :)

I did find one issue however. For users (like me) who have some clipboards that are not named according to the schema "Clipboard n" this can lead to an infinite loop which generates a never-ending list of clipboards. The problem is that as the Nisus Macro reference says: "When accessed by index, clipboards are ordered alphabetically by name."

A lesser problem is that you can't truly be sure that the a user with 5 clipboards doesn't already have a clipboard named "Clipboard 6". Code for solving both of these issues could be written using the Define New Commands feature:

Code: Select all

Define Command AddClipboard
	$n = 0
	While Clipboard Exists “Clipboard $n”
		$n +=1
	End
	New Clipboard “Clipboard $n”
	$newClipboard = Clipboard Index “Clipboard $n”
	Return $newClipboard
End
This code will return the index of the newly created clipboard so you can use it like this:

Code: Select all

$newClipboard = AddClipboard
Best
philip

User avatar
loulesko
Posts: 89
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: More clipboard tools - smart cut, smart copy.

Post by loulesko » 2011-09-06 23:01:40

Philip,

Awesome suggestion and good catch on the bug. Many thanks for the help. I updated the post with the new code which now works flawlessly.

All the best
Lou

Post Reply