@@ -38,6 +38,7 @@ from processes running on the same host as VSCode.
38
38
### Python example
39
39
40
40
``` py
41
+ import json
41
42
import requests
42
43
from pathlib import Path
43
44
from tempfile import gettempdir
@@ -67,39 +68,45 @@ that you have a function `actions.key` that presses the given key (eg
67
68
[ talon] ( https://talonvoice.com/ ) ):
68
69
69
70
``` py
70
- port_file_path = Path(gettempdir()) / " vscode-port"
71
- original_contents = port_file_path.read_text()
72
-
73
- # Issue command to VSCode telling it to update the port file. Because only
74
- # the active VSCode instance will accept keypresses, we can be sure that
75
- # the active VSCode instance will be the one to write the port.
76
- if is_mac:
77
- actions.key(" cmd-shift-alt-p" )
78
- else :
79
- actions.key(" ctrl-shift-alt-p" )
80
-
81
- # Wait for the VSCode instance to update the port file. This generally
82
- # happens within the first millisecond, but we give it 3 seconds just in
83
- # case.
84
- start_time = time.monotonic()
71
+ import json
72
+ import requests
73
+ import time
74
+ from pathlib import Path
75
+ from tempfile import gettempdir
76
+
77
+ port_file_path = Path(gettempdir()) / " vscode-port"
78
+ original_contents = port_file_path.read_text()
79
+
80
+ # Issue command to VSCode telling it to update the port file. Because only
81
+ # the active VSCode instance will accept keypresses, we can be sure that
82
+ # the active VSCode instance will be the one to write the port.
83
+ if is_mac:
84
+ actions.key(" cmd-shift-alt-p" )
85
+ else :
86
+ actions.key(" ctrl-shift-alt-p" )
87
+
88
+ # Wait for the VSCode instance to update the port file. This generally
89
+ # happens within the first millisecond, but we give it 3 seconds just in
90
+ # case.
91
+ start_time = time.monotonic()
92
+ new_contents = port_file_path.read_text()
93
+ while original_contents == new_contents:
94
+ time.sleep(0.001 )
95
+ if time.monotonic() - start_time > 3.0 :
96
+ raise Exception (" Timed out waiting for VSCode to update port file" )
85
97
new_contents = port_file_path.read_text()
86
- while original_contents == new_contents:
87
- time.sleep(0.001 )
88
- if time.monotonic() - start_time > 3.0 :
89
- raise Exception (" Timed out waiting for VSCode to update port file" )
90
- new_contents = port_file_path.read_text()
91
-
92
- port = json.loads(new_contents)[" port" ]
93
-
94
- response = requests.post(
95
- f " http://localhost: { port} /execute-command " ,
96
- json = {
97
- " commandId" : " some-command-id" ,
98
- " args" : [" some-argument" ],
99
- },
100
- timeout = (0.05 , 3.05 ),
101
- )
102
- response.raise_for_status()
98
+
99
+ port = json.loads(new_contents)[" port" ]
100
+
101
+ response = requests.post(
102
+ f " http://localhost: { port} /execute-command " ,
103
+ json = {
104
+ " commandId" : " some-command-id" ,
105
+ " args" : [" some-argument" ],
106
+ },
107
+ timeout = (0.05 , 3.05 ),
108
+ )
109
+ response.raise_for_status()
103
110
```
104
111
105
112
## Known issues
0 commit comments