TOC
Before reading
Please read the following first.
- This document has been translated from Japanese to English using DeepL Translate
- This extension is for AutoHotkey users who use VSCode
- Currently no pull requests are being accepted
- Bug reports and feature requests are accepted here. I'll respond to you immediately or by the next day, so please feel free to do so. But look at the known issues first.
News
Update
Overview
This extension provides a way to execute VSCode's commands from AutoHotkey and commands to access VSCode's context information (e.g. Caret position, etc.).
The mechanism is simple: just execute the command with the same name as the string stored in the clipboard.
Therefore, it should be possible to use it not only with AutoHotkey, but also with other automation tools (such as AutoIt) that can handle the clipboard and keystrokes.
The biggest advantage of this extension would be the ability to directly execute commands that do not have a shortcut key set. Setting shortcut keys can be very tedious, as they must not conflict with other keys. Moreover, there are a surprisingly large number of commands for which no shortcuts have been set.
It is very convenient but may require a little knowledge to use. If you have any questions, please ask them here. (Github account required).
By default, all commands can be executed, but if you are concerned about security, you may want to set operate-from-autohotkey.allowCommands.
Installation
This extension works on VSCode 1.46.0 and above.
- Install AutoHotkey. Since some commands use AutoHotkey scripts (e.g. Acc.ahk) to get information, you will need to install it even if you are using other automation tools
- Press
Ctrl + P
, type ext install zero-plusplus.vscode-operate-from-autohotkey
- Include this library in your scripts. For v2, here
About Acc.ahk
Acc.ahk
is required when using some commands that retrieve information not provided by VSCode, such as caret coordinates. The original file already has a broken link and you can get a copy of it here.
Then place it in the path set in operate-from-autohotkey.externalLibrary.acc.
Note: AutoHotkeyV1 is required to run Acc.ahk. I have explained this in the Installation, but I will explain it again.
Using
The library described in the third of the Installation defines the ExecuteVsCodeCommand
function to execute the command. The usage is the same for both AutoHotkey V1 and V2. However, V2 is written for the latest version, and is likely to not work with older versions. In addition, as explained here, may encounter problems that do not occur in V1.
It is very easy to use, just pass the command name as shown in the following. To find out the name of the command, look here.
; Move the cursor to the right
ExecuteVsCodeCommand("cursorRight")
If you want to execute multiple commands, separate them with ,(comma)
. The spaces before and after are optional.
Note that if you execute a command that affects the clipboard, it will exit without executing any subsequent commands. This is because it uses the changes in the clipboard to detect the end of the command.
; Move the cursor to the right and down
ExecuteVsCodeCommand("cursorRight, cursorDown")
If you want to repeat the same command, do the following.
; Move the cursor to the right five times
ExecuteVsCodeCommand("cursorRight:5")
These can be combined. The use of continuation sections will make it easier to read.
; Move the cursor to the right five times, then move down
ExecuteVsCodeCommand("
(Join,
cursorRight:5
cursorDown
)")
The second parameter specifies the amount of time before the timeout (in milliseconds). 0
(or false
) disables the timeout. Normally, you do not need to specify this parameter, but if you are using a command that requires user input, as in the following example, you should disable it.
; git.commit prompts for a commit message. Disable the timeout in this case, since it will time out before you can enter it.
ExecuteVsCodeCommand("git.commit", false)
How ExecuteVsCodeCommand works and how to modify it
Look at the source code for ExecuteVsCodeCommand. I have written a detailed description of how it works in the comments.
It is useful to be able to maintain it yourself in case there are any problems.
The following is the answer to the modification example (for V1) in the comments.
ExecuteVsCodeCommandEx(commandName, timeout_ms := 1000) {
if (IsObject(commandName)) {
return ExecuteVsCodeCommand(StrJoin(commandName, ","), timeout_ms)
}
return ExecuteVsCodeCommand(commandName, timeout_ms)
}
StrJoin(array, delimiter) {
string := ""
for i, value in array {
string .= value . delimiter
}
return RTrim(string, delimiter)
}
Settings
The following settings will be added in settings.json.
Name |
Description |
operate-from-autohotkey.allowCommands |
Allowed command names. Can be used as a wildcard by adding a trailing * . By default, all commands are allowed.
Normally, you don't need to set this, but if you are using a script created by someone else, you can improve security by setting it.
Default: [ "*" ]
|
operate-from-autohotkey.hideError |
Whether to notify the user of an error when a command fails to execute.
Default: false
|
operate-from-autohotkey.externalLibrary.acc |
The absolute path of the AutoHotkey external library required by some commands. See details here.
Default: "C:\Program Files\AutoHotkey\lib\Acc.ahk"
|
Commands
The following commands will be added.
Name |
Description |
operate-from-autohotkey.executeCommand |
Executes a command with the contents of the clipboard. This is the core command of this extension. Only one shortcut key is provided: Shift+Ctrl+Alt+F12 |
operate-from-autohotkey.copy.context.caret |
Copy the position (1-base) of the current caret in "lineNumber:columnNumber" format. |
operate-from-autohotkey.copy.context.caret.line |
Copy the line number (1-base) of the current caret. |
operate-from-autohotkey.copy.context.caret.column |
Copy the column number (1-base) of the current caret. |
operate-from-autohotkey.copy.context.caret.coordinates |
[Require Acc.ahk] Copy the coordinates of the caret with respect to the screen in "x,y" format. |
operate-from-autohotkey.copy.context.caret.coordinates.x |
[Require Acc.ahk] Copy the x-coordinates of the caret with respect to the screen. |
operate-from-autohotkey.copy.context.caret.coordinates.y |
[Require Acc.ahk] Copy the y-coordinates of the caret with respect to the screen. |
operate-from-autohotkey.copy.context.selection |
Copy the range (1-base) of the current selection in "startLineNumber:startColumnNumber:endLineNumber:endColumnNumber" format. |
operate-from-autohotkey.copy.context.selection.start |
Copy the starting position (1-base) of the current selection in "lineNumber:columnNumber" format.
Synonymous with operate-from-autohotkey.copy.context.caret
|
operate-from-autohotkey.copy.context.selection.start.line |
Copy the starting line number (1-base) of the current selection.
Synonymous with operate-from-autohotkey.copy.context.caret.line
|
operate-from-autohotkey.copy.context.selection.start.column |
Copy the starting column number (1-base) of the current selection.
Synonymous with operate-from-autohotkey.copy.context.caret.column
|
operate-from-autohotkey.copy.context.selection.end |
Copy the ending position (1-base) of the current selection in "lineNumber:columnNumber" format. |
operate-from-autohotkey.copy.context.selection.end.line |
Copy the ending line number (1-base) of the current selection. |
operate-from-autohotkey.copy.context.selection.end.column |
Copy the ending column number (1-base) of the current selection. |
operate-from-autohotkey.copy.context.selection.text |
Copy the text of the current selection. |
operate-from-autohotkey.copy.context.file |
Copy the currently opened file in "filePath:lineNumber:columnNumber" format. |
operate-from-autohotkey.copy.context.file.path |
Copy the currently opened file path. |
operate-from-autohotkey.copy.context.file.length |
Copy the currently opened file length. |
operate-from-autohotkey.copy.context.file.eol |
Copy the currently opened End Of Line (EOL) of the file. Value that can be gotten are "`n" or "`r`n" . |
operate-from-autohotkey.copy.context.json |
Copy context information in JSON format.
Besides being able to retrieve a lot of information at once, you can also retrieve information on all selections of the multi-caret.
|
operate-from-autohotkey.copy.context.json.pretty |
Copy context information in human-readable JSON format.
Besides being able to retrieve a lot of information at once, you can also retrieve information on all selections of the multi-caret.
|
operate-from-autohotkey.copy.context.flattenJson |
Copy context information in flatten JSON format.
Besides being able to retrieve a lot of information at once, you can also retrieve information on all selections of the multi-caret.
|
operate-from-autohotkey.copy.context.flattenJson.pretty |
Copy context information in human-readable flatten JSON format.
Besides being able to retrieve a lot of information at once, you can also retrieve information on all selections of the multi-caret.
|
Known issues
- V2 only bug. I have confirmed that Out of memory occurs in versions
a119
-a122
when the commands are executed consecutively using hotkeys. However, it does not occur in a100
, which is closer in specification to v1.
Development support
Over the next few years, I will be developing a variety of extension to AutoHotkey.
If you like, Github sponsor and support me. Note that there is no reward. This is because I believe that the biggest reward is to provide new versions faster by prioritizing development.