Reply to topic  [ 3 posts ] 
More clipboard tools - smart cut, smart copy. 
Author Message
User avatar

Joined: 2008-01-20 11:08:35
Posts: 89
Location: California
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:
# 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:
# 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.



2011-09-05 15:53:06
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
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:
$newClipboard = AddClipboard


Best

_________________
philip


2011-09-06 20:06:24
Profile
User avatar

Joined: 2008-01-20 11:08:35
Posts: 89
Location: California
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


2011-09-06 23:01:40
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software