Dead Code/[toy] bookmarker

[플러터] windows bookmarker - URI execution

2025. 3. 13.



안드로이드 앱에서는 대충 launcher를 사용하면 알아서 잘 실행이 되던데, windows에서는 대상 uri에 따라서 실행 방식을 좀 구분을 해줘야한다.

 

ⓐ 웹 URL : path 텍스트가 https 혹은 http로 시작하는 경우

ⓑ 실행 파일 : ⓐ가 아니고, path텍스트가 .exe 혹은 .bat로 텍스트가 끝나는 경우

ⓒ folder : ⓐ 또는 ⓑ가 아니고, 해당 이름으로 폴더 경로가 있는 경우

 

 

chatGPT가 짜준 초안을 좀 수정을 했는데, 끝까지 말썽인건 exe나 bat 파일 등 프로그램을 실행하는 경우이다. cmd를 powershell로 변경해서 일단 내가 사용하는 프로그램들에 문제가 없도록 하긴 했는데, 그때 그때 약간 손을 좀 봐줘야할 것 같다.

 

텍스트로 강제 입력한 URI 값이 아래 경우에 해당하지 않는 경우, 아무것도 되지 않는다는 단점은 있다. 특히.. 옵션값을 붙여야 하는 경우는 좀 애매한데, 그냥 bat 파일을 만들고 해당 파일을 실행하는 것으로 생각했다.

 

이런 제한 사항을 내 머리가 얼마나 기억할지는 의문이다.

 

  void _launch(String path) async {
      // ✅ 웹 URL 실행
    if (path.startsWith('http://') || path.startsWith('https://')) {
 
      final Uri uri = Uri.parse(path);
      try {
        await launchUrl(uri, mode: LaunchMode.externalApplication);
      } catch (e) {
        debugPrint("Could not launch URL: $path - Error: $e");
      }
    }

      // ✅ (.EXE .bat, .cmd) 실행
    else if (path.endsWith('.exe') ||
        path.endsWith('.bat') ||
        path.endsWith('.cmd')) {
 
      try {
        ProcessResult result = await Process.run(
          'powershell',
          ['-Command', 'Start-Process', '"$path"'],
          runInShell: true,
        );

        if (result.exitCode != 0) {
          debugPrint("Execution failed: ${result.stderr}");
        } else {
          debugPrint("Execution started: ${result.stdout}");
        }
      } catch (e) {
        debugPrint("Could not launch EXE/BAT/CMD: $path - Error: $e");
      }
      // ✅ 폴더 경로 실행 (탐색기에서 열기)
    } else if (Directory(path).existsSync()) {
 
      try {
        await Process.run('explorer', [path], runInShell: true);
      } catch (e) {
        debugPrint("Could not open folder: $path - Error: $e");
      }
    } else {
      debugPrint("Unsupported path: $path");
    }
  }

 

 

 

끝.