Skip to content

Instantly share code, notes, and snippets.

@kinuasa
Last active March 6, 2025 13:36
Show Gist options
  • Save kinuasa/dc1436e8614aeb883a97d4fa0fbb0c3b to your computer and use it in GitHub Desktop.
Save kinuasa/dc1436e8614aeb883a97d4fa0fbb0c3b to your computer and use it in GitHub Desktop.
名前を付けて保存ダイアログを操作して図の圧縮を行うPowerShellスクリプト 関連Webサイト:https://note.com/kinuasa/n/n1bc40e528212
Param(
[parameter(Mandatory=$true)][ValidateSet("XLMAIN", "OpusApp", "PPTFrameClass")][string]$ClassName,
[parameter(Mandatory=$true)][string]$DocumentTitle,
[parameter(Mandatory=$true)][string]$SaveFilePath,
[parameter(Mandatory=$true)][string]$ResolutionItem
)
$source = @"
using System;
using System.Threading;
using System.Windows.Automation;
namespace UIAutTools
{
public class Element
{
public static void SavePicCompressedFile(string className, string documentTitle, string saveFilePath, string resolutionItem)
{
Thread.Sleep(200);
//名前を付けて保存ダイアログ取得
AutomationElement elmApp = null;
PropertyCondition cndAppCollection = new PropertyCondition(AutomationElement.ClassNameProperty, className, PropertyConditionFlags.IgnoreCase);
foreach (AutomationElement app in AutomationElement.RootElement.FindAll(TreeScope.Children, cndAppCollection))
{
if (app.Current.Name.Contains(documentTitle))
{
elmApp = app;
break;
}
}
if (elmApp == null) { return; }
AndCondition cndSaveAsDialog = new AndCondition(new Condition[] { new PropertyCondition(AutomationElement.NameProperty, "名前を付けて保存"), new PropertyCondition(AutomationElement.ClassNameProperty, "#32770") });
AutomationElement elmSaveAsDialog = elmApp.FindFirst(TreeScope.Subtree, cndSaveAsDialog);
if (elmSaveAsDialog == null) { return; }
//ツール(L) -> 「図(画像)の圧縮」項目選択
AndCondition cndToolMenuItem = new AndCondition(new Condition[] { new PropertyCondition(AutomationElement.NameProperty, "ツール(L)"), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem) });
AutomationElement elmToolMenuItem = elmSaveAsDialog.FindFirst(TreeScope.Subtree, cndToolMenuItem);
InvokePattern iptnToolMenuItem = elmToolMenuItem.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
iptnToolMenuItem.Invoke();
AndCondition cndContextMenu = new AndCondition(new Condition[] { new PropertyCondition(AutomationElement.ClassNameProperty, "#32768"), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Menu) });
AutomationElement elmContextMenu = AutomationElement.RootElement.FindFirst(TreeScope.Children, cndContextMenu);
PropertyCondition cndPicCompressMenuItem = new PropertyCondition(AutomationElement.NameProperty, "図の圧縮(C)..."); //Excel
AutomationElement elmPicCompressMenuItem = elmContextMenu.FindFirst(TreeScope.Children, cndPicCompressMenuItem);
if (elmPicCompressMenuItem == null)
{
cndPicCompressMenuItem = new PropertyCondition(AutomationElement.NameProperty, "図の圧縮(P)..."); //Word
elmPicCompressMenuItem = elmContextMenu.FindFirst(TreeScope.Children, cndPicCompressMenuItem);
}
if (elmPicCompressMenuItem == null)
{
cndPicCompressMenuItem = new PropertyCondition(AutomationElement.NameProperty, "画像の圧縮(C)..."); //PowerPoint
elmPicCompressMenuItem = elmContextMenu.FindFirst(TreeScope.Children, cndPicCompressMenuItem);
}
InvokePattern iptnPicCompressMenuItem = elmPicCompressMenuItem.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
iptnPicCompressMenuItem.Invoke();
Thread.Sleep(200);
//画像の圧縮ダイアログ取得
AndCondition cndPicCompressDialog = new AndCondition(new Condition[] { new PropertyCondition(AutomationElement.NameProperty, "画像の圧縮"), new PropertyCondition(AutomationElement.ClassNameProperty, "NUIDialog") });
AutomationElement elmPicCompressDialog = elmApp.FindFirst(TreeScope.Subtree, cndPicCompressDialog);
if (elmPicCompressDialog == null) { return; }
//解像度ラジオボタン選択
PropertyCondition cndResRadioGroup = new PropertyCondition(AutomationElement.ClassNameProperty, "NetUIRadioGroup");
AutomationElement elmResRadioGroup = elmPicCompressDialog.FindFirst(TreeScope.Subtree, cndResRadioGroup);
PropertyCondition cndResRadioButton = new PropertyCondition(AutomationElement.ClassNameProperty, "NetUIRadioButton");
foreach (AutomationElement elmResRadioButton in elmResRadioGroup.FindAll(TreeScope.Subtree, cndResRadioButton))
{
if (elmResRadioButton.Current.Name.Contains(resolutionItem))
{
SelectionItemPattern sptnResRadioButton = elmResRadioButton.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
sptnResRadioButton.Select();
break;
}
}
//OKボタン(画像の圧縮ダイアログ)クリック
AndCondition cndPicCompressOKButton = new AndCondition(new Condition[] { new PropertyCondition(AutomationElement.NameProperty, "OK"), new PropertyCondition(AutomationElement.ClassNameProperty, "NetUIButton") });
AutomationElement elmPicCompressOKButton = elmPicCompressDialog.FindFirst(TreeScope.Subtree, cndPicCompressOKButton);
InvokePattern iptnPicCompressOKButton = elmPicCompressOKButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
iptnPicCompressOKButton.Invoke();
//ファイル名入力
AndCondition cndFileNameEditControl = new AndCondition(new Condition[] { new PropertyCondition(AutomationElement.NameProperty, "ファイル名:"), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit) });
AutomationElement elmFileNameEditControl = elmSaveAsDialog.FindFirst(TreeScope.Subtree, cndFileNameEditControl);
ValuePattern vptnFileNameEditControl = elmFileNameEditControl.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
vptnFileNameEditControl.SetValue(saveFilePath);
//保存(S)ボタンクリック
AndCondition cndSaveButton = new AndCondition(new Condition[] { new PropertyCondition(AutomationElement.NameProperty, "保存(S)"), new PropertyCondition(AutomationElement.ClassNameProperty, "Button") });
AutomationElement elmSaveButton = elmSaveAsDialog.FindFirst(TreeScope.Subtree, cndSaveButton);
InvokePattern iptnSaveButton = elmSaveButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
iptnSaveButton.Invoke();
}
}
}
"@
Add-Type -TypeDefinition $source -ReferencedAssemblies("UIAutomationClient", "UIAutomationTypes")
[UIAutTools.Element]::SavePicCompressedFile($ClassName, $DocumentTitle, $SaveFilePath, $ResolutionItem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment