2021年3月28日日曜日

フォルダを監視 » ファイル追加 » 通知(tost) » FileMakerスクリプトを実行

フォルダを監視
ファイルが追加された » 通知(tost) » FileMakerスクリプトを実行



動作検証
Windows 10
FileMaker Pro 19
PowerShell 5.1
PowerShell 7

サンプルファイル
FileSystemWatcher.fmp12
FileSystemWatcherTost.ps1
FileSystemWatcherTost.bat

FileSystemWatcherTost.ps1
$targetDirectory = "D:\!TEMP\test" ;<#監視するディレクトリ#>
$LogFilePath="D:\!TEMP\log.txt"; <#ログファイル#>

Add-Type -AssemblyName System.Web;
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null;

$contentTemplate = @' 
<toast duration="long" launch="fmp://$/ToastNotification" activationType="protocol">
    <!-- long:25sec, short:7sec -->
    <visual>
        <binding template="ToastGeneric">
            <text id="1">{changeType}</text>
            <text id="2">{data}</text>
            <text id="3">{path}</text>
        </binding>
    </visual>
    <actions>
        <action activationType="protocol" content="Run FileMaker Script" arguments="fmp://$/FileSystemWatcher?script=receive(json)&amp;$arg={arg}" />
    </actions>
</toast>
'@;


<# フォルダを監視する,ファイルを監視する,サブフォルダを監視する #>
    $watcher = New-Object System.IO.FileSystemWatcher ;
    $watcher.Path = $targetDirectory; <#監視するディレクトリ#>
    $watcher.Filter = "*.*" 
    $watcher.IncludeSubdirectories = $true; <#サブディレクトリを監視#>
    $watcher.EnableRaisingEvents = $true; 
    
    Set-Content $LogFilePath -value "" -Encoding utf8 ;<#ログファイル初期化#>
<#イベント発生時#>
    $action = { $path = $Event.SourceEventArgs.FullPath 
                $changeType = $Event.SourceEventArgs.ChangeType 
                $logline = "$(Get-Date -Format G), $changeType, $path" 
                Add-content $LogFilePath -value $logline -Encoding UTF8 
                Write-Host $logline; 
                $arg=ConvertTo-Json @{dateTime=$(Get-Date -Format G);changeType="$changeType";path=$path};
                $arg=$arg -replace "`r","";
                $arg=[System.Web.HttpUtility]::UrlEncode($arg,[System.Text.Encoding]::UTF8);
                $arg=$arg -replace "\+","%20";
                $content=$contentTemplate `
                -replace "{data}",$(Get-Date -Format G)`
                -replace "{changeType}",$changeType`
                -replace "{path}",$path`
                -replace "{arg}",$arg;
                

                $AppId = '{6D809377-6AF0-444B-8957-A3773F02200E}\FileMaker\FileMaker Pro 19\FileMaker Pro.exe';
                #$AppId = New-Guid;
                $xml =[Windows.Data.Xml.Dom.XmlDocument]::new(); $xml.LoadXml($content);
                $Toast =[Windows.UI.Notifications.ToastNotification]::new($xml);
                [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppID).Show($Toast);


              }     
<#監視するイベント#>
$watcherCreated= Register-ObjectEvent $watcher "Created" -Action $action 
#$watcherChanged= Register-ObjectEvent $watcher "Changed" -Action $action 
#$watcherDeleted= Register-ObjectEvent $watcher "Deleted" -Action $action 
#$watcherRenamed= Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}
<#
    $watcherCreated.StopJob();
    $watcherChanged.StopJob();
    $watcherDeleted.StopJob();
    $watcherRenamed.StopJob();
#>

監視するフォルダを指定
1行目
$targetDirectory = "D:\!TEMP\test" ;<#監視するディレクトリ#>

ログファイル指定
2行目
$LogFilePath="D:\!TEMP\log.txt"; <#ログファイル#>

FileMakerのスクリプトを指定
20行目あたり
<action activationType="protocol" content="Run FileMaker Script" arguments="fmp://$/{ファイル名}?script={スクリプト名}&amp;$arg={arg}" />
例:
ファイル名:FileSystemWatcher.fmp12
スクリプト名:receive(json) の場合
<action activationType="protocol" content="Run FileMaker Script" arguments="fmp://$/FileSystemWatcher?script=receive(json)&amp;$arg={arg}" />

監視するファイルの拡張子を指定
29行目あたり
$watcher.Filter = "*.*" 
例:csvファイルの場合
$watcher.Filter = "*.csv" 

起動方法 

FileSystemWatcherTost.bat
cd %~dp0
powershell -windowstyle Minimized -NoProfile -ExecutionPolicy Unrestricted -File .\FileSystemWatcherTost.ps1
FileSystemWatcherTost.ps1 を同じ場所に FileSystemWatcherTost.bat を保存しダブルクリックで実行。
非表示 -windowstyle Hidden
最小化 -windowstyle Minimized
最大化 -windowstyle Maximized
通常  -windowstyle Normal


FileMakerスクリプト例

変数:$arg で値を取得


-

2021年3月22日月曜日

【メモ】jsonから名前(キー)を取得, json 集計等 |PowerShell

 $ERROR.Clear();

$records=ConvertFrom-Json '

[

{"101":"","102":"12000","103":"12000"},{"101":"","102":"12000","103":"12000"},{"101":"","102":"12000","103":"12000"},

{"101":"","102":"12000","103":"12000"},{"101":"","102":"12000","103":"12000"},{"101":"","102":"12000","103":"12000"},

{"101":"","102":"12000","103":"12000"},{"101":"","102":"12000","103":"12000"},{"101":"","102":"12000","103":"12000","104":999}

]

'

#json 集計等

$sum=$records|Measure-Object -Property "*" -Sum ;


ConvertTo-Json $sum


#jsonから名前(キー)を取得

$records | ForEach-Object{$_| Get-Member -MemberType Properties | Select-Object -ExpandProperty Name}| Sort-Object -Unique

2021年3月15日月曜日

Power Automate Desktop のフローを「バッチファイル」「ショートカット」「FileMaker」などから実行

2022/12/01 追記
Power Automate バージョン:226.139.22312(あたり) 以降は、最下部の修正を適用してください。

Power Automate Desktop のフローを実行

コマンドプロンプト
バッチファイル
ショートカット
FileMaker などから実行するための PowerShellスクリプト

動作検証
Wiundows 10
Power Automate Desktop 2.5.39.21056
PowerShell 5.1・7.1.3

ファイルダウンロード ¥0
PAD_aid.zip
https://fm-aid.stores.jp/items/604f73e6243860052a8c9134


準備

WinAppDriver をインストール
WindowsApplicationDriver_1.2.1.msi(2021/03/15現在)をダウンロードしてインストール

-
WinAppDriver を利用するには、「開発者モード」にする必要があります。
「設定」- 更新とセキュリティー」-「開発者向け」



KickPADFlow.ps1 の設定・確認


6行目 Power Automate Desktop のコンソールウインドウの名前を確認。
違う場合は合わせてください。
$PADWindowName='Power Automate Desktop (プレビュー)'


7行目 PAD.Console.Host.exe のパスの確認。異なる場合は修正
$PADConsoleHostExePath="C:\Program Files (x86)\Power Automate Desktop\PAD.Console.Host.exe";

8行目 WinAppDriver.exe のパスの確認。異なる場合は修正
$WinAppDriverExePath='C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe';WinAppDriver.exe

9行目 ポートが競合しエラーになる場合は、変更してください
$WinAppDriverPort=4727;

19行目 WinAppDriver.exe のコンソールを隠したい場合、
-windowstyle を Minimized や、Hidden に設定
Start-Process -windowstyle 'Normal' -FilePath $WinAppDriverExePath -ArgumentList $WinAppDriverPort;  



実行方法

ダウンロードしたファイルを任意のフォルダへ展開