Power Automate DesktopでExcelのデータからInDesignの索引を作成

Power Automate DesktopでExcelのデータからInDesignの索引を作るフローを作成してみました。
実行動画 https://www.youtube.com/watch?v=aYVaGJkQFgQ

実行は自己責任でお願いします（途中で自動でのキー操作が有るので）

以下のテキスト（Robin）をフローの編集画面に張り付けて下さい。
~~~
/# ExcelのデータからInDesignの索引を作成
更新：2022/12/21#/
Display.ShowMessageDialog.ShowMessage Message: $'''ExcelのデータからInDesignの索引を作成します。
索引を作成したいInDesignのドキュメントを開いた状態にして下さい。

Excelの例（索引文字　読み）
猫　　　　ねこ
犬　　　　いぬ
ペンギン
熊　　　　くま
''' Icon: Display.Icon.Question Buttons: Display.Buttons.YesNo DefaultButton: Display.DefaultButton.Button1 IsTopMost: True ButtonPressed=> ButtonPressed
IF ButtonPressed = $'''No''' THEN
    EXIT Code: 0
END
IF (UIAutomation.IfWindow.IsNotOpenByTitleClass Title: $'''''' Class: $'''indesign''') THEN
    Display.ShowMessageDialog.ShowMessage Message: $'''InDesignが開いていないようです。''' Icon: Display.Icon.Information Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed
    EXIT Code: 0
END
Scripting.RunVBScript VBScriptCode: $'''Dim app : Set app = CreateObject(\"InDesign.Application\") \'Target the last version of InDesign that was launched with admin privileges
Dim MyDocumentCountNumber : MyDocumentCountNumber = app.documents.count()
WScript.StdOut.Write(MyDocumentCountNumber) \'Output
Set app = Nothing \'Release reference''' ScriptOutput=> VBScriptOutput
IF VBScriptOutput = 0 THEN
    Display.ShowMessageDialog.ShowMessage Message: $'''InDesignのドキュメントが開かれていないようです。''' Icon: Display.Icon.Information Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True
    EXIT Code: 0
END
Display.SelectFileDialog.SelectFile Title: $'''Select Excel file''' IsTopMost: True CheckIfFileExists: False SelectedFile=> SelectedFile ButtonPressed=> ButtonPressed
IF ButtonPressed = $'''Cancel''' THEN
    EXIT Code: 0
END
Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: SelectedFile Visible: False ReadOnly: False Instance=> ExcelInstance
Excel.ReadFromExcel.ReadAllCells Instance: ExcelInstance ReadAsText: True FirstLineIsHeader: False RangeValue=> ExcelData
Excel.CloseExcel.Close Instance: ExcelInstance
UIAutomation.SetWindowState.SetStateByTitleClass Title: $'''''' Class: $'''indesign''' State: UIAutomation.WindowState.Minimized
/# 「検索と置換」の検索条件の初期化
#/
Scripting.RunVBScript VBScriptCode: $'''Dim app : Set app = CreateObject(\"InDesign.Application\") \'Target the last version of InDesign that was launched with admin privileges
app.findTextPreferences = 1851876449 \'NothingEnum.NOTHING
Set app = Nothing \'Release reference'''
IF (UIAutomation.IfWindow.IsNotOpenByTitleClass Title: $'''Windows PowerShell''' Class: $'''''') THEN
    System.RunApplication.RunApplicationAndWaitToLoad ApplicationPath: $'''powershell.exe''' WindowStyle: System.ProcessWindowStyle.Maximized Timeout: 0
END
# 最小化されていたウィンドウに入力すると上手くいかない時がある
MouseAndKeyboard.SendKeys.FocusAndSendKeysByTitleClass Title: $'''Windows PowerShell''' Class: $'''''' TextToSend: $'''$app = New-Object -ComObject InDesign.Application{Return}''' DelayBetweenKeystrokes: 0 SendTextAsHardwareKeys: False
LOOP FOREACH CurrentItem IN ExcelData
    # 索引文字を検索
    MouseAndKeyboard.SendKeys.FocusAndSendKeysByTitleClass Title: $'''Windows PowerShell''' Class: $'''''' TextToSend: $'''# Find text %CurrentItem[0]%{Return}''' DelayBetweenKeystrokes: 0 SendTextAsHardwareKeys: False
    Scripting.RunPowershellScript Script: $'''&{
    $app = New-Object -ComObject InDesign.Application #Target the last version of InDesign that was launched with admin privileges
    $MyScriptLanguage = 1246973031 # ScriptLanguage.JAVASCRIPT
    $MyArgumentsArray = @(\"%CurrentItem[0]%\") # Create array
    $MySource = ` # Line breaks in expressions
@\"
app.findTextPreferences.findWhat = arguments[0];
var mySearchResultArray = app.activeDocument.findText();
if(mySearchResultArray.length > 0){
	app.activeDocument.select(mySearchResultArray[0]);
}
mySearchResultArray.length
\"@
    Write-Output $app.doScript($MySource, $MyScriptLanguage, $MyArgumentsArray) # Run ExtendScript & get output to variable
}
[gc]::Collect() # GC''' ScriptOutput=> PowershellOutput
    # 索引文字が見つかった場合
    IF PowershellOutput >= 1 THEN
        MouseAndKeyboard.SendKeys.FocusAndSendKeysByTitleClass Title: $'''Windows PowerShell''' Class: $'''''' TextToSend: $'''$app.menuActions.itemByID(78082).invoke(){Return}''' DelayBetweenKeystrokes: 0 SendTextAsHardwareKeys: False
        IF CurrentItem.ColumnsCount > 1 THEN
            # 読み有り
            MouseAndKeyboard.SendKeys.FocusAndSendKeysByTitleClass Title: $'''新規ページ参照''' Class: $'''''' TextToSend: $'''{Alt}({S})%CurrentItem[1]%{Alt}({L}){Return}''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False
        ELSE
            # 読み無し
            MouseAndKeyboard.SendKeys.FocusAndSendKeysByTitleClass Title: $'''新規ページ参照''' Class: $'''''' TextToSend: $'''{Alt}({L}){Return}''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False
        END
    END
END
MouseAndKeyboard.SendKeys.FocusAndSendKeysByTitleClass Title: $'''Windows PowerShell''' Class: $'''''' TextToSend: $'''[gc]::Collect(){Return}''' DelayBetweenKeystrokes: 0 SendTextAsHardwareKeys: False
UIAutomation.FocusWindow.FocusByTitleClass Title: $'''''' Class: $'''indesign'''
Display.ShowMessageDialog.ShowMessage Message: $'''フローが終了しました。''' Icon: Display.Icon.Information Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True
~~~