11using System ;
2+ using System . IO ;
23using System . Windows ;
34using System . Text . RegularExpressions ;
4- using System . Runtime . InteropServices ;
5+ using Microsoft . CodeAnalysis . Scripting ;
56
67namespace UserInputMacro
78{
@@ -10,16 +11,12 @@ namespace UserInputMacro
1011 /// </summary>
1112 public partial class App : Application
1213 {
13- [ DllImport ( "Kernel32.dll" ) ]
14- private static extern bool AttachConsole ( int processId ) ;
15-
16- const int ATTACH_PARENT_PROCESS = - 1 ;
17-
1814 private async void Application_StartupAsync ( object sender , StartupEventArgs e )
1915 {
2016 try {
2117 // when no arguments are specified, execute main window
2218 if ( e . Args . Length == 0 ) {
19+ AppEnvironment . GetInstance ( ) . IsConsoleMode = false ;
2320 var window = new MainWindow ( ) ;
2421 window . Show ( ) ;
2522 return ;
@@ -32,25 +29,33 @@ private async void Application_StartupAsync( object sender, StartupEventArgs e )
3229 Match argsChacker = scriptArgPattern . Match ( e . Args [ 0 ] ) ;
3330
3431 if ( argsChacker . Success ) {
35- await ScriptExecuter . ExecuteAsync ( argsChacker . Groups [ "scriptPath" ] . Value ) ;
32+ string filePath = argsChacker . Groups [ "scriptPath" ] . Value ;
33+ if ( File . Exists ( filePath ) ) {
34+ await ScriptExecuter . ExecuteAsync ( filePath ) ;
35+ }
36+ else {
37+ CommonUtil . WriteToConsole ( "[File Error]" + Environment . NewLine + "'" + filePath + "' is not found." ) ;
38+ }
3639 }
3740 else {
3841 Usage ( ) ;
3942 }
4043 }
44+ catch ( CompilationErrorException ex ) {
45+ CommonUtil . WriteToConsole ( "[Compile Error]" + Environment . NewLine + ex . Message ) ;
46+ }
4147 catch ( Exception ex ) {
42- Console . WriteLine ( ex ) ;
48+ CommonUtil . HandleException ( ex ) ;
4349 }
4450
4551 Current . Shutdown ( ) ;
4652 }
4753
4854 private void Usage ( )
4955 {
50- AttachConsole ( ATTACH_PARENT_PROCESS ) ;
51- Console . WriteLine ( "Usage: UserInputMacro <option>" ) ;
52- Console . WriteLine ( "[option list]" ) ;
53- Console . WriteLine ( "script=<scirpt path>: Command line mode and only execute script" ) ;
56+ CommonUtil . WriteToConsole ( "Usage: UserInputMacro <option>" + Environment . NewLine +
57+ "[option]" + Environment . NewLine +
58+ "-script=<scirpt path>: Command line mode and only execute script" ) ;
5459 }
5560 }
5661}
0 commit comments