LoginSignup
18
27

More than 5 years have passed since last update.

サクラエディタでPowerShellコマンドを実行し、入力補助機能として使用するための3行マクロ

Last updated at Posted at 2016-09-16

概要

サクラエディタで選択中の文字列をPowerShellのコマンドとして読み取り、結果値に置換します。

実装

invoke-expression.js
//PowerShellコマンドを実行し結果を出力
Editor.ExecCommand('powershell -command "' + Editor.GetSelectedString(0) + '"', 3);

//余分な改行を削除
Editor.Right(0);
Editor.DeleteBack(0);

使用方法

1.invoke-expression.jsをサクラエディタのマクロに登録する
※詳細はサクラエディタでマクロの登録を参照

2.サクラエディタにて実行したいPowerShellコマンドを選択し、invoke-expression.jsを実行する

活用例

1.簡易電卓

実行前
1 + 2
実行後
3

2.文字列生成マクロ

実行前
'X'*10
実行後
XXXXXXXXXX

3.連番マクロ(基本)

実行前
1..10
実行後
1
2
3
4
5
6
7
8
9
10

4.連番マクロ(応用)

実行前
1..10 | %{$_.ToString('00') + ':'}
実行後
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:

5.文字列置換

実行前
'2016年9月16日'.Replace('年','/').Replace('月','/').Replace('日','')
実行後
2016/9/16

6.テキストファイル読込

sample.txt
@echo off
@echo on
set <Name>=<Value>
set /a <Name>=<Formula>
for /f "tokens=*" %%a in('<Command>') do set <Name>=%%a
powershell -ExecutionPolicy RemoteSigned -File <Path>

上記ファイルがC:\workに配置されているとして

実行前
type C:\work\sample.txt | ?{$_ -like 'set*'}
実行後
set <Name>=<Value>
set /a <Name>=<Formula>

まとめ

PowerShellは、一般にはサーバ管理用の言語というイメージが強いと思いますが、ある程度慣れれば日常業務でも本当に便利に活用できるのでおすすめです。

18
27
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
27