Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for clipboardprepend.cs

(view source code of clipboardprepend.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	internal class ClipboardPrepend
  10. 	{
  11. 		static readonly string progver = "1.00";
  12.  
  13.  
  14. 		[STAThread]
  15. 		static int Main( string[] args )
  16. 		{
  17. 			if ( args.Length > 1 || ( args.Length == 1 && Console.IsInputRedirected ) || args.Contains( "/?" ) )
  18. 			{
  19. 				return ShowHelp( );
  20. 			}
  21.  
  22. 			string text;
  23. 			if ( args.Length == 1 )
  24. 			{
  25. 				text = args[0];
  26. 			}
  27. 			else if ( Console.IsInputRedirected )
  28. 			{
  29. 				text = Console.In.ReadToEnd( );
  30. 			}
  31. 			else
  32. 			{
  33. 				return ShowHelp( );
  34. 			}
  35.  
  36. 			if ( Clipboard.ContainsText( ) )
  37. 			{
  38. 				string cliptext = Clipboard.GetText( );
  39. 				Clipboard.SetText( text + cliptext );
  40. 			}
  41. 			else
  42. 			{
  43. 				Clipboard.SetText( text );
  44. 			}
  45. 			return 0;
  46. 		}
  47.  
  48.  
  49. 		public static int ShowHelp( params string[] errmsg )
  50. 		{
  51. 			#region Error Message
  52.  
  53. 			if ( errmsg.Length > 0 )
  54. 			{
  55. 				List<string> errargs = new List<string>( errmsg );
  56. 				errargs.RemoveAt( 0 );
  57. 				Console.Error.WriteLine( );
  58. 				Console.ForegroundColor = ConsoleColor.Red;
  59. 				Console.Error.Write( "ERROR:\t" );
  60. 				Console.ForegroundColor = ConsoleColor.White;
  61. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  62. 				Console.ResetColor( );
  63. 			}
  64.  
  65. 			#endregion Error Message
  66.  
  67.  
  68. 			#region Help Text
  69.  
  70. 			/*
  71. 			ClipboardPrepend.exe,  Version 1.00
  72. 			Send text to clipboard, prepending it to existing text in clipboard
  73.  
  74. 			Usage:    CLIPBOARDAPPEND.EXE  text
  75.  
  76. 			or:       somecommand  |  CLIPBOARDAPPEND.EXE
  77.  
  78. 			Where:    text          text to be prepended to clipboard
  79. 			          somecommand   command whose output will be prepended to clipboard
  80.  
  81. 			Notes:    If the clipboard contains data other than text, it is overwritten.
  82. 			          Return code (\"ErrorLevel\") -1 in case of (command line) errors,
  83. 			          otherwise 0.
  84.  
  85. 			Written by Rob van der Woude
  86. 			https://www.robvanderwoude.com
  87. 			*/
  88.  
  89. 			#endregion Help Text
  90.  
  91.  
  92. 			#region Display Help Text
  93.  
  94. 			Console.Error.WriteLine( );
  95.  
  96. 			Console.Error.WriteLine( "ClipboardPrepend.exe,  Version {0}", progver );
  97.  
  98. 			Console.Error.WriteLine( "Send text to clipboard, prepending it to existing text in clipboard" );
  99.  
  100. 			Console.Error.WriteLine( );
  101.  
  102. 			Console.Error.Write( "Usage:    " );
  103. 			Console.ForegroundColor = ConsoleColor.White;
  104. 			Console.Error.WriteLine( "CLIPBOARDPREPEND.EXE  text" );
  105. 			Console.ResetColor( );
  106.  
  107. 			Console.Error.WriteLine( );
  108.  
  109. 			Console.Error.Write( "or:       " );
  110. 			Console.ForegroundColor = ConsoleColor.White;
  111. 			Console.Error.WriteLine( "somecommand  |  CLIPBOARDPREPEND.EXE" );
  112. 			Console.ResetColor( );
  113.  
  114. 			Console.Error.WriteLine( );
  115.  
  116. 			Console.Error.Write( "Where:    " );
  117. 			Console.ForegroundColor = ConsoleColor.White;
  118. 			Console.Error.Write( "text" );
  119. 			Console.ResetColor( );
  120. 			Console.Error.WriteLine( "          text to be prepended to clipboard" );
  121.  
  122. 			Console.ForegroundColor = ConsoleColor.White;
  123. 			Console.Error.Write( "          somecommand" );
  124. 			Console.ResetColor( );
  125. 			Console.Error.WriteLine( "   command whose output will be prepended to clipboard" );
  126.  
  127. 			Console.Error.WriteLine( );
  128.  
  129. 			Console.Error.WriteLine( "Notes:    If the clipboard contains data other than text, it is overwritten." );
  130.  
  131. 			Console.Error.WriteLine( "          Return code (\"ErrorLevel\") -1 in case of (command line) errors," );
  132.  
  133. 			Console.Error.WriteLine( "          otherwise 0." );
  134.  
  135. 			Console.Error.WriteLine( );
  136.  
  137. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  138.  
  139. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  140.  
  141. 			#endregion Display Help Text
  142.  
  143.  
  144. 			return -1;
  145. 		}
  146. 	}
  147. }

page last uploaded: 2021-01-27