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 で値を取得


-

0 件のコメント:

コメントを投稿