å°ãåã¾ã§JScript on WSH 5.6ã§ã¡ãã£ã¨ãããã¼ã«ãæ¸ãã¦ãã¦*1ããã®ä¸ã§ãã¡ã¤ã«é¸æãã¤ã¢ãã°ã表示ããããã¨èª¿ã¹ã¦ã¿ãããå¹¾ã¤ãæ¹æ³ããã£ããã©ã©ãã決å®æã«æ¬ ããã¨ãããä¸é·ä¸çã¨ããããæ£ç´å¾®å¦ã ã£ãã
åãæ¢ããå¤æããæ¹æ³ãã¾ã¨ãã¦ã¿ãã
å®é¨ç°å¢ã¯Windows XP Pro SP2ã®Windows Scripting Host 5.6ã§ãInternet Explorer 6 SP3ã
ãUserAccounts.CommonDialogãã¨ããCOMã³ã³ãã¼ãã³ãã使ãæ¹æ³
ãæ軽ã«ä½¿ç¨ã§ããæ¹æ³ãä½ãWindows XPã§ãã使ããªããXPããå¤ãã¦ãæ°ããã¦ããã¡ãWindows Serverãªã·ãªã¼ãºã¨ãããã¡ããããã©ããããã®ã³ã³ãã¼ãã³ããXPã§å°å ¥ããããã©Vistaã§åé¤ããããããã
var dialog = WScript.CreateObject("UserAccounts.CommonDialog"); dialog.Filter = "Text Files|*.txt|Csv Files|*.csv|All Files|*.*"; dialog.InitialDir = "."; if (!dialog.ShowOpen()) { WScript.Echo("ãã¡ã¤ã«ã¯é¸æãããªãã£ãã¿ããã â¦â¦ã"); WScript.Quit(); } WScript.Echo("é¸æããã®ã¯ãããï¼\n\"" + dialog.FileName + "\"");
ã³ã¢ã³ãã¤ã¢ãã°ã³ã³ããã¼ã« (Comdlg32.ocxï¼ã使ãæ¹æ³
Comdlg32.ocxãå¿ è¦ãªã®ãããã¯ãVisual BasicãªããVB 6.0ã®ã©ã³ã¿ã¤ã ãã¤ã³ã¹ãã¼ã«å¿ è¦ããã模æ§ãã©ã¤ã»ã³ã¹çãªåé¡ããããããããªãã
var dialog = WScript.CreateObject("MSComDlg.CommonDialog"); dialog.MaxFileSize = 256; dialog.Filter = "Text Files|*.txt|Csv Files|*.csv|All Files|*.*"; dialog.InitDir = "."; dialog.CancelError = true; try { dialog.ShowOpen(); } catch (e) { WScript.Echo("ãã¡ã¤ã«ã¯é¸æãããªãã£ãã¿ããã â¦â¦ã"); WScript.Quit(); } WScript.Echo("é¸æããã®ã¯ãããï¼\n\"" + dialog.FileName + "\"");
COMçµç±ã§Internet Explorerã使ãæ¹æ³
ãã·ã³ã«ãã£ã¦ã¯å°ãéããããããªããä½ããIE使ã£ã¦ãããã
å人çã«ã¯ãIEã¨ãã®ãã¼ã¸ã§ã³ã®éãã«è¦ãããããå¯è½æ§ãããããã ã¨æãã¦ãããã©ããã®è¾ºãã¯æªç¢ºèªã
// FIXME 2011/02/19 ç¾å¨ã¯ãã®ã³ã¼ãã§ã¯åä½ããªãï¼ // è¡åãæªããã©ã³ã¡ã³ãã¢ã¦ãã /* var ie = WScript.CreateObject("InternetExplorer.Application"); ie.Navigate2("about:<html><body><input type='file' id='fileDialog'></body></html>"); while (ie.Busy || ie.ReadyState != 4) { WScript.sleep(10); } ie.Document.all.fileDialog.click(); var fname = ie.Document.all.fileDialog.value; ie.Quit(); if (fname == "") { WScript.Echo("ãã¡ã¤ã«ã¯é¸æãããªãã£ãã¿ããã â¦â¦ã"); } else { WScript.Echo("é¸æããã®ã¯ãããï¼\n\"" + fname + "\""); } */
2011/02/19 追è¨
ä¸ã®ã³ã¼ãã ãããã¤ã®éã«ãæå ã®ç°å¢ï¼Windows XP SP3 + InternetExplorer 6ï¼ã§åä½ããªããªã£ã¦ããã
ã©ããInternetExplorer.Application::Navigate2ã§ã¿ã°ä»ãã§ãã«ããã«ããã¦ããé¨åã§å¤±æãã¦ãããããªã®ã§ããããæç´ããã¦ã¿ãã以ä¸ã®ã³ã¼ãã¯Windows XP SP3ä¸ã®InternetExplorer 6ã7ã§åä½ããã
var ie, body, dialog, fname; ie = WScript.CreateObject("InternetExplorer.Application"); ie.Navigate("about:blank"); body = ie.document.getElementsByTagName("body")[0]; body.innerHTML = "<input type=\'file\' id=\'fileDialog\'>"; while (ie.Busy || ie.ReadyState != 4) { WScript.sleep(10); } dialog = ie.document.all.fileDialog; dialog.click(); fname = dialog.value; ie.Quit(); if (fname == "") { WScript.Echo("ãã¡ã¤ã«ã¯é¸æãããªãã£ãã¿ããã â¦â¦ã"); } else { WScript.Echo("é¸æããã®ã¯ãããï¼\n\"" + fname + "\""); }
InternetExplorer 8ã®å ´åã¯æ´ã«æç´ããå¿ è¦ã ãã¨ããã®ããã¤ã¢ãã°ããç´æ¥åå¾ã§ãããã¡ã¤ã«ãã¹ã¯ãã»ãã¥ãªãã£å¯¾çã®çºã«å½ã®ãã¹ã«ãªã£ã¦ããã®ã *2ã
ãã®æåã¸ã®å¯¾çã¨ãã¦ãä¾ãã°textareaè¦ç´ ãç¨æãã¦ããããã¤ã¢ãã°ã§åå¾ãããã¡ã¤ã«ãã¹ãå é¨ã§ãã£ããtextareaã«ã³ããããââã¨ãã£ãæ¹æ³ãããã
(function() { var READYSTATE_COMPLETE = 4; var OLECMDID_COPY = 12; var OLECMDID_PASTE = 13; var OLECMDID_SELECTALL = 0x11; var OLECMDEXECOPT_DODEFAULT = 0; var ie, body, dialog, fname, text; ie = WScript.CreateObject("InternetExplorer.Application"); ie.Navigate("about:blank"); body = ie.document.getElementsByTagName("body")[0]; body.innerHTML = "<input type=\'file\' id=\'fileDialog\'><textarea id=\'text\'></textarea>"; while (ie.Busy || ie.ReadyState !== READYSTATE_COMPLETE) { WScript.sleep(10); } dialog = ie.document.all.fileDialog; dialog.click(); fname = dialog.value; if (fname === "") { WScript.Echo("ãã¡ã¤ã«ã¯é¸æãããªãã£ãã¿ããã â¦â¦ã"); } else { dialog.focus(); ie.ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT); ie.ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT); text = ie.document.all.text; text.focus(); ie.ExecWB(OLECMDID_PASTE, OLECMDEXECOPT_DODEFAULT); fname = text.value; WScript.Echo("é¸æããã®ã¯ãããï¼\n\"" + fname + "\""); } text = undefined; dialog = undefined; body = undefined; ie.Quit(); ie = undefined; })();
ãã®ã³ã¼ãã¯Windows XP SP3ä¸ã®InternetExplorer 8ã§åä½ãããInternetExplorer 6ã7ã§ãåé¡ãªãåä½ãããããªã®ã§ãè¤æ°ã®ãã¼ã¸ã§ã³ã«ã¦ä½¿ãå¿ è¦ã®ããã¹ã¯ãªãããæ¸ãå ´åã¯ãã®ã³ã¼ãããã¼ã¹ã«ãã¦ããããããããªãã
以ä¸ã追è¨çµããã
ãã¡ã¤ã«é¸æãã¤ã¢ãã°è¡¨ç¤ºç¨ã®ã¢ããªãå¥éç¨æããæ¹æ³
- ãã¡ã¤ã«é¸æãã¤ã¢ãã°ã表示ãã¦ãé¸æããããã¡ã¤ã«ã®ãã¹ãæ¨æºåºåã«æ¸ãåºãã ãã®ã¢ããªãç¨æãã¦ããã
- ã¹ã¯ãªãããããã®ã¢ããªãèµ·åããæ¨æºåºåçµç±ã§ãã¡ã¤ã«ãã¹ãåå¾ãã¦è²ã ã¨æ´»ç¨ããã
å¥éã¢ããªãç¨æããªãã¨ãããªãæãã¹ãã¼ããããªãã¦ä¸ã¤é¢åãã ãã©ãã¤ãã£ãã³ã¼ããªã¢ããªãç¨æã§ãããæ¯è¼çåºãç¯å²ã§ä½¿ãããããããªãæãé åçããã
ã¾ãã¯ã¢ããªã®ã½ã¼ã¹ãC89ç¸å½ã®Cè¨èªï¼Windows APIã§æ¸ãã¦ããã
/** * select_file3\.exe -(o|s) "[:FILTER:]" [:DEFEXT:] * $ select_file3.exe -o "Csv file|*.csv|All files|*.*" csv * * MinGWã§ãã«ãããå ´å㯠-lshell32 -lcomdlg32 ãå¿ è¦ */ #include <assert.h> #include <stdlib.h> #include <string.h> #include <tchar.h> #include <windows.h> #include <shellapi.h> #include <winnls.h> #ifndef UNICODE /** charæååãWCHARã«å¤æãã */ static LPWSTR atow(LPCSTR src) { LPWSTR buf; int dst_size, rc; rc = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0); if (rc == 0) { return NULL; } dst_size = rc + 1; buf = (LPWSTR) malloc(sizeof(WCHAR) * dst_size); if (buf == NULL) { return NULL; } rc = MultiByteToWideChar(CP_ACP, 0, src, -1, buf, dst_size); if (rc == 0) { free(buf); return NULL; } buf[rc] = L'\0'; return buf; } #endif /* ndef UNICODE */ /** WCHARæååãcharã«å¤æãã */ static LPSTR wtoa(LPCWSTR src) { LPSTR buf; int dst_size, rc; rc = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL); if (rc == 0) { return NULL; } dst_size = rc + 1; buf = (LPSTR) malloc(dst_size); if (buf == NULL) { return NULL; } rc = WideCharToMultiByte(CP_ACP, 0, src, -1, buf, dst_size, NULL, NULL); if (rc == 0) { free(buf); return NULL; } buf[rc] = '\0'; return buf; } /** ãªãã·ã§ã³å¼æ°ã解æãã */ static LPTSTR *parse_args(LPCTSTR args_t, int *argc) { LPCWSTR args_w; LPWSTR *argv_w; if (args_t[0] == _T('\0')) { *argc = 0; return NULL; } #ifdef UNICODE args_w = args_t; #else /* å¼æ°æååãWCHARã«å¤æ */ args_w = (LPCWSTR) atow(args_t); if (args_w == NULL) { return NULL; } #endif /* ãã¼ã¹ããï¼WCHARç¨ã®APIããç¨æããã¦ããªã模æ§ï¼ */ argv_w = CommandLineToArgvW(args_w, argc); #ifdef UNICODE return argv_w; #else free((void *) args_w); if (argv_w == NULL) { return NULL; } { LPSTR *argv_c = NULL; int i, j; /* ãã¼ã¹çµæã¯WCHARãªã®ã§ãcharåã«å¤æããå¿ è¦ããã */ argv_c = (LPSTR *) malloc(sizeof(argv_c[0]) * (*argc + 1)); if (argv_c == NULL) { goto DONE; } for (i = 0; i < *argc; ++i) { argv_c[i] = wtoa(argv_w[i]); if (argv_c[i] == NULL) { for (j = 0; j < i; ++j) { free(argv_c[j]); } free(argv_c); argv_c = NULL; goto DONE; } } argv_c[i] = NULL; DONE: (void) LocalFree((HLOCAL) argv_w); return argv_c; } #endif } /** ãªãã·ã§ã³å¼æ°é¨åã®é åãéæ¾ãã */ static void free_args(int argc, LPTSTR *argv) { #ifdef UNICODE (void) argc; (void) LocalFree((HLOCAL) argv); #else int i; for (i = 0; i < argc; ++i) { free(argv[i]); } free(argv); #endif } /** æååãæ¨æºåºåã«æ¸ãåºã */ static BOOL print_str(LPCTSTR str) { HANDLE hstdout; LPSTR str_c; DWORD size, written_byte; BOOL rc; assert(str != NULL); hstdout = GetStdHandle(STD_OUTPUT_HANDLE); if (hstdout == INVALID_HANDLE_VALUE) { return FALSE; } #ifdef UNICODE str_c = wtoa(str); if (str_c == NULL) { return FALSE; } #else str_c = (LPSTR) str; #endif size = (DWORD) strlen(str_c); written_byte = 0; rc = WriteFile(hstdout, (LPCVOID) str_c, size, &written_byte, NULL); if (rc == FALSE) { goto DONE; } assert(size == written_byte); rc = FlushFileBuffers(hstdout); DONE: #ifdef UNICODE free(str_c); #endif return rc; } /* ãã¡ã¤ã«é¸æãã¤ã¢ãã°ã表示ãã * mode=0 open * mode=1 save */ static BOOL show_dialog(HWND hwnd, LPCTSTR filter, LPTSTR file, DWORD maxfile, LPCTSTR defext, int mode) { OPENFILENAME ofn; BOOL rc; memset(&ofn, 0, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFilter = filter; ofn.lpstrFile = file; ofn.nMaxFile = maxfile; ofn.lpstrDefExt = defext; if (mode == 0) { ofn.lpstrTitle = _T("ãã¡ã¤ã«ãéã"); ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; rc = GetOpenFileName(&ofn); } else if (mode == 1) { ofn.lpstrTitle = _T("ååãä»ãã¦ä¿å"); ofn.Flags = OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; rc = GetSaveFileName(&ofn); } else { return FALSE; } return rc; } #define NELEMS(ary) (sizeof(ary) / sizeof((ary)[0])) #define EXIT_ERROR (EXIT_FAILURE + 1) /** ã¡ã¤ã³ã«ã¼ãã³ */ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { static TCHAR buf[512]; int argc, mode; LPTSTR *argv, filter; LPCTSTR defext; size_t filter_size, i; BOOL rc; (void) hInstance, (void) hPrevInstance, (void) lpCmdLine, (void) nCmdShow; /* Win32ã¢ããªã§ã¯ãå¼æ°æååã¯ãã¼ã¹ããã¦ããªãã®ã§ããã¼ã¹ãã */ argc = ~0; argv = parse_args(GetCommandLine(), &argc); if (argv == NULL) { return EXIT_ERROR; } if (argc < 3 || argc > 4) { free_args(argc, argv); return EXIT_ERROR; } /* 第3å¼æ°ãç¡ãå ´åã¯ç©ºæåã§è£å®ãã */ defext = (argc == 3) ? _T("") : argv[3]; /* 第1å¼æ°ã®å½¢å¼ããã§ã㯠*/ if (_tcscmp(argv[1], _T("-o")) == 0) { mode = 0; } else if (_tcscmp(argv[1], _T("-s")) == 0) { mode = 1; } else { free_args(argc, argv); return EXIT_ERROR; } /* 第2å¼æ°ã¯ãã®ã¾ã¾ã§ã¯ä½¿ããªãã®ã§å¤æãã */ filter_size = _tcslen(argv[2]) + 2; filter = (LPTSTR) malloc(sizeof(TCHAR) * filter_size); if (filter == NULL) { free_args(argc, argv); return EXIT_ERROR; } for (i = 0; i < filter_size; ++i) { filter[i] = (argv[2][i] == _T('|')) ? _T('\0') : argv[2][i]; if (argv[2][i] == _T('\0')) { filter[i+1] = _T('\0'); break; } } /* ãã¤ã¢ãã°ã表示ããé¸æããããã¡ã¤ã«ã®ãã¹ãæ¨æºåºåã«æ¸ãåºã */ rc = show_dialog(NULL, filter, buf, (DWORD) NELEMS(buf), defext, mode); if (rc != FALSE) { (void) print_str(buf); } free(filter); free_args(argc, argv); return (rc != FALSE) ? EXIT_SUCCESS : EXIT_FAILURE; }
ã¡ãªã¿ã«Cï¼C++ã§æ¸ãå ´åã¯ä»¥ä¸ã«æ³¨æã
- ã³ã³ã½ã¼ã«ã¢ããªã ã¨ãã¤ã¢ãã°è¡¨ç¤ºä¸ã«ã³ãã³ãããã³ãããè¦ããã¾ã¾ã«ãªãããããå«ãªãWin32ã¢ããªã«ãããã¨ã
- ãã¹ã«æ¥æ¬èªãå«ã¾ãã¦ããå ´åã¯ãæ¨æºåºåã¸ã®åºåã¯ãã«ããã¤ãæååãããªãã¨NGãªæ¨¡æ§ãVisual C++ã使ãå ´åã¯ãããã¸ã§ã¯ãã®è¨å®ã§ãã«ããã¤ãæååã使ãããã«ããæ¹ãç¡é£ã
ä¸ã®ã½ã¼ã¹ããselect_file.exeã¨ããååã§ã¢ããªããã«ãããå ´åãJScriotå´ã®ã³ã¼ãã¯ãããªæãã«ãªãã
var shell = WScript.CreateObject("WScript.Shell"); try { var proc = shell.Exec("select_file.exe -o \"Text Files|*.txt|Csv Files|*.csv|All Files|*.*\" txt"); } catch (e) { eprintln("error: " + e.message); WScript.Quit(); } var fname = proc.StdOut.ReadAll(); while (proc.Status == 0) { WScript.Sleep(10); } if (proc.ExitCode == 0) { WScript.Echo("é¸æããã®ã¯ãããï¼\n\"" + fname + "\""); } else if (proc.ExitCode == 1) { WScript.Echo("ãã¡ã¤ã«ã¯é¸æãããªãã£ãã¿ããã â¦â¦ã"); } else { WScript.Echo("ã¨ã©ã¼çºçï¼"); }
ãã¡ã¤ã«é¸æãã¤ã¢ãã°è¡¨ç¤ºç¨ã®COMã³ã³ãã¼ãã³ããèªä½ããæ¹æ³
- ãã¡ã¤ã«é¸æãã¤ã¢ãã°ã表示ãã¦ãé¸æããããã¡ã¤ã«ã®ãã¹ãåå¾ããCOMã³ã³ãã¼ãã³ããèªä½ããã
- èªä½ããCOMã³ã³ãã¼ãã³ããã·ã¹ãã ã«ç»é²ããã
- ã¹ã¯ãªãããããã®ã³ã³ãã¼ãã³ããå©ãããã¡ã¤ã«ãã¹ãåå¾ãã¦è²ã ã¨æ´»ç¨ããã
ãã£ããã¨ããªãããããæ°ããªãã®ã§ãµã³ãã«ã³ã¼ãã¯ç¡ãã
COMã³ã³ãã¼ãã³ããä½ãã®ãé¢åãã*3ãã·ã¹ãã ã«ç»é²ããå¿ è¦ãããç¹ã¯ãå ´åã«ãã£ã¦ã¯ãã¤ãã¹ããã
ãã¡ã¤ã«é¸æãã¤ã¢ãã°è¡¨ç¤ºç¨ã«PowerShellã®ã¹ã¯ãªããã使ç¨ããæ¹æ³ï¼2012/11/25ï¼
ããã¡ã¤ã«é¸æãã¤ã¢ãã°è¡¨ç¤ºç¨ã®ã¢ããªãå¥éç¨æããæ¹æ³ãã®å¤å½¢çã¨ãã¦ããã®ã¢ããªãPowerShellã®ã¹ã¯ãªããã§æ¸ãã¦ãã¾ãæ¹æ³ãããã
ãã¨ãã°ãããªã¹ã¯ãªããã
Set-StrictMode -version Latest Add-Type -assemblyName System.Windows.Forms $dialog = New-Object System.Windows.Forms.OpenFileDialog $dialog.Filter = 'Text Files|*.txt|Csv Files|*.csv|All Files|*.*' $dialog.InitialDirectory = '.' [void] $dialog.ShowDialog() $dialog.FileName
ãã¤ã¢ãã°ã§ä½ããããã¡ã¤ã«ãé¸æããããã®ãã«ãã¹ãåºåããé¸æããªãã£ãå ´åã¯ç©ºç½è¡ãåºåããã
ä¸è¨ã¹ã¯ãªãããå®è¡ããWSHå´ã¯ãããªæãã
var fname; fname = WScript.CreateObject('WScript.Shell'). Exec('powershell -Sta -File select_file.ps1'). StdOut.ReadLine(); if (fname === '') { WScript.Echo('ãã¡ã¤ã«ã¯é¸æãããªãã£ãã¿ããã â¦â¦ã'); } else { WScript.Echo('é¸æããã®ã¯ãããï¼\n"' + fname + '"'); }
PowerShellã¯Windows 7以éãªãããã©ã«ãã§ã¤ã³ã¹ãã¼ã«ããã¦ããããã¡ãã£ã¨æéã ãã©Windows XPãVistaã«ã¤ã³ã¹ãã¼ã«ã§ããã.NET Frameworkçµç±ã§ãã¡ã¤ã«é¸æãã¤ã¢ãã°ã表示ããã®ã容æããå¤ãç°å¢ããã¾ãèæ ®ããå¿ è¦ããªããã°ãä»å¾ã¯PowerShellãç©æ¥µçã«ä½¿ç¨ãã¦ãããã ããã
ãã ãJScriptãVBScriptã§æ¸ãããæ¢åã®ã¹ã¯ãªãããå ¨ã¦PowerShellã§æ¸ãç´ãã®ã¯ç¾å®çã§ã¯ãªããã¨ãå¤ãã®ã§ããã®å ´åã¯ãã®ä¾ã®ããã«ä¸é¨ãPowerShellã§è£å®ããã¹ã¿ã¤ã«ãæå¹ã ã¨æãã