teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

コードの修正

2019/10/16 00:23

投稿

nemui_00
nemui_00

スコア5

title CHANGED
File without changes
body CHANGED
@@ -21,20 +21,18 @@
21
21
  <title></title>
22
22
  <script type="text/javascript">
23
23
  function inputData(arg1) {
24
- if (arg1 != null) {
24
+ var obj = new ActiveXObject("WScript.Shell");
25
- var winname = "データ入力画面";
25
+ obj.AppActivate("データ入力画面");
26
- var result = ActivateAndSendKeys(winname, arg1, 500);
27
- if (result) {
26
+ sleep(1000);
27
+ //キー・コードを送る。
28
- result = ActivateAndSendKeys(winname, "{ENTER}", 2000);
28
+ obj.SendKeys(arg1);
29
- }
30
- if (result) {
29
+ sleep(2000);
31
- result = ActivateAndSendKeys(winname, "{F5}", 900);
32
- }
33
- if (result) {
34
- result = ActivateAndSendKeys(winname, "{ENTER}", 900);
30
+ obj.SendKeys("{ENTER}");
35
- }
31
+ sleep(900);
32
+ obj.SendKeys("{F5}");
33
+ sleep(2000);
34
+ obj.SendKeys("{ENTER}");
36
- return result;
35
+ obj = null;
37
- }
38
36
  }
39
37
  function inputData2(arg1) {
40
38
  var obj = new ActiveXObject("WScript.Shell");
@@ -50,27 +48,6 @@
50
48
  obj.SendKeys("{ENTER}");
51
49
  obj = null;
52
50
  }
53
- function ActivateAndSendKeys(strTitle, strKey, intWait) {
54
- var obj = new ActiveXObject("WScript.Shell");
55
- var result = false;
56
- //3回試行する。
57
- for (var i = 1; i < 3; i++) {
58
- if(obj.AppActivate(strTitle)){
59
- //intWaitミリ秒待つ。
60
- sleep(intWait);
61
- //キー・コードを送る。
62
- obj.SendKeys(strKey);
63
- sleep(500);
64
- //成功を意味するTrueを返し、ループを抜ける。
65
- result = true;
66
- break;
67
- } else {
68
- sleep(1000);
69
- }
70
- }
71
- obj = null;
72
- return result;
73
- }
74
51
  function sleep(waitMsec) {
75
52
  var startMsec = new Date();
76
53
  // 指定ミリ秒間だけループさせる

3

補足情報追加

2019/10/16 00:23

投稿

nemui_00
nemui_00

スコア5

title CHANGED
File without changes
body CHANGED
@@ -9,8 +9,8 @@
9
9
  どうしたらよいのでしょうか?
10
10
 
11
11
  ### 該当のソースコード
12
- 以下のhtmlファイルをIEで開き、データ入力画面というファイル名のテキストファイルをメモ帳で開いた状態で、
12
+ 以下のhtmlファイルをIEで開き、データ入力画面というファイル名のテキストファイルをメモ帳で開いた状態で、ブラウザのbuttonを押すと、事象を確認できます。
13
- ブラウザのボタンを押すと事象を確認ます。
13
+ またbutton2はRun新規に開いたやり方です。これだとうくいくようです。
14
14
 
15
15
  ```html
16
16
  <!DOCTYPE html>
@@ -23,19 +23,33 @@
23
23
  function inputData(arg1) {
24
24
  if (arg1 != null) {
25
25
  var winname = "データ入力画面";
26
- var result = ActivateAndSendKeys(winname, arg1, 3000);
26
+ var result = ActivateAndSendKeys(winname, arg1, 500);
27
27
  if (result) {
28
- result = ActivateAndSendKeys(winname, "{ENTER}", 1000);
28
+ result = ActivateAndSendKeys(winname, "{ENTER}", 2000);
29
29
  }
30
30
  if (result) {
31
- result = ActivateAndSendKeys(winname, "{F5}", 1000);
31
+ result = ActivateAndSendKeys(winname, "{F5}", 900);
32
32
  }
33
33
  if (result) {
34
- result = ActivateAndSendKeys(winname, "{ENTER}", 1000);
34
+ result = ActivateAndSendKeys(winname, "{ENTER}", 900);
35
35
  }
36
36
  return result;
37
37
  }
38
38
  }
39
+ function inputData2(arg1) {
40
+ var obj = new ActiveXObject("WScript.Shell");
41
+ obj.Run("notepad");
42
+ sleep(1000);
43
+ //キー・コードを送る。
44
+ obj.SendKeys(arg1);
45
+ sleep(2000);
46
+ obj.SendKeys("{ENTER}");
47
+ sleep(900);
48
+ obj.SendKeys("{F5}");
49
+ sleep(2000);
50
+ obj.SendKeys("{ENTER}");
51
+ obj = null;
52
+ }
39
53
  function ActivateAndSendKeys(strTitle, strKey, intWait) {
40
54
  var obj = new ActiveXObject("WScript.Shell");
41
55
  var result = false;
@@ -46,7 +60,7 @@
46
60
  sleep(intWait);
47
61
  //キー・コードを送る。
48
62
  obj.SendKeys(strKey);
49
- sleep(100);
63
+ sleep(500);
50
64
  //成功を意味するTrueを返し、ループを抜ける。
51
65
  result = true;
52
66
  break;
@@ -67,17 +81,17 @@
67
81
  <body>
68
82
  <form id="form1">
69
83
  <div>
70
- <button type="submit" onclick="inputData('test');">button</button>
84
+ <button type="submit" onclick="inputData('12345');">button</button>
85
+ <button type="submit" onclick="inputData2('12345');">button2</button>
71
86
  </div>
72
87
  </form>
73
88
  </body>
74
89
  </html>
75
-
76
90
  ```
77
91
 
78
92
  ### 試したこと
79
93
 
80
- AppActivateでなくRunで新規に画面を起動した場合、うまくいくようです。
94
+ AppActivateでなくRunで新規に画面を起動した場合(button2のやり方)、うまくいくようです。
81
95
 
82
96
  ### 補足情報(FW/ツールのバージョンなど)
83
97
 

2

詳細に記載

2019/10/16 00:17

投稿

nemui_00
nemui_00

スコア5

title CHANGED
File without changes
body CHANGED
@@ -9,50 +9,70 @@
9
9
  どうしたらよいのでしょうか?
10
10
 
11
11
  ### 該当のソースコード
12
+ 以下のhtmlファイルをIEで開き、データ入力画面というファイル名のテキストファイルをメモ帳で開いた状態で、
13
+ ブラウザのボタンを押すと、事象を確認できます。
12
14
 
13
- ```javascript
14
- function inputData(arg1) {
15
- if (arg1 != null) {
16
- var winname = "データ入力画面";
17
- var result = ActivateAndSendKeys(winname, arg1, 5000);
18
- if (result) {
19
- result = ActivateAndSendKeys(winname, "{ENTER}", 1000);
20
- }
21
- if (result) {
22
- result = ActivateAndSendKeys(winname, "{F5}", 1000);
23
- }
24
- if (result) {
25
- result = ActivateAndSendKeys(winname, "{ENTER}", 1000);
26
- }
27
- return result;
28
- }
29
- }
30
- function ActivateAndSendKeys(strTitle, strKey, intWait) {
31
- var obj = new ActiveXObject("WScript.Shell");
32
- var result = false;
33
- //3回試行する。
34
- for (var i = 1; i < 3; i++) {
35
- if(obj.AppActivate(strTitle)){
36
- //intWaitミリ秒待つ。
37
- sleep(intWait);
38
- //キー・コードを送る。
39
- obj.SendKeys(strKey);
40
- sleep(100);
41
- //成功を意味するTrueを返し、ループを抜ける。
42
- result = true;
43
- break;
44
- } else {
45
- sleep(1000);
46
- }
47
- }
48
- obj = null;
49
- return result;
50
- }
51
- function sleep(waitMsec) {
52
- var startMsec = new Date();
53
- // 指定ミリ秒間だけループさせる
54
- while (new Date() - startMsec < waitMsec);
55
- }
15
+ ```html
16
+ <!DOCTYPE html>
17
+
18
+ <html xmlns="http://www.w3.org/1999/xhtml">
19
+ <head>
20
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
21
+ <title></title>
22
+ <script type="text/javascript">
23
+ function inputData(arg1) {
24
+ if (arg1 != null) {
25
+ var winname = "データ入力画面";
26
+ var result = ActivateAndSendKeys(winname, arg1, 3000);
27
+ if (result) {
28
+ result = ActivateAndSendKeys(winname, "{ENTER}", 1000);
29
+ }
30
+ if (result) {
31
+ result = ActivateAndSendKeys(winname, "{F5}", 1000);
32
+ }
33
+ if (result) {
34
+ result = ActivateAndSendKeys(winname, "{ENTER}", 1000);
35
+ }
36
+ return result;
37
+ }
38
+ }
39
+ function ActivateAndSendKeys(strTitle, strKey, intWait) {
40
+ var obj = new ActiveXObject("WScript.Shell");
41
+ var result = false;
42
+ //3回試行する。
43
+ for (var i = 1; i < 3; i++) {
44
+ if(obj.AppActivate(strTitle)){
45
+ //intWaitミリ秒待つ。
46
+ sleep(intWait);
47
+ //キー・コードを送る。
48
+ obj.SendKeys(strKey);
49
+ sleep(100);
50
+ //成功を意味するTrueを返し、ループを抜ける。
51
+ result = true;
52
+ break;
53
+ } else {
54
+ sleep(1000);
55
+ }
56
+ }
57
+ obj = null;
58
+ return result;
59
+ }
60
+ function sleep(waitMsec) {
61
+ var startMsec = new Date();
62
+ // 指定ミリ秒間だけループさせる
63
+ while (new Date() - startMsec < waitMsec);
64
+ }
65
+ </script>
66
+ </head>
67
+ <body>
68
+ <form id="form1">
69
+ <div>
70
+ <button type="submit" onclick="inputData('test');">button</button>
71
+ </div>
72
+ </form>
73
+ </body>
74
+ </html>
75
+
56
76
  ```
57
77
 
58
78
  ### 試したこと

1

文法の修正

2019/10/15 23:33

投稿

nemui_00
nemui_00

スコア5

title CHANGED
@@ -1,1 +1,1 @@
1
- javascriptでActiveXObjectを使用し、SendKeysとsleepを交互に行う処理を行いたい
1
+ javascriptでActiveXObjectを使用し、SendKeysとsleepを交互に行いたい
body CHANGED
File without changes