2017年4月18日火曜日

FileMaker cmd.exe dirコマンドを実行して戻り値を取得。あれこれ。


■クリップボード経由
FileMakerスクリプト:
フィールド設定 [cmd::Result; ""]
コピー [選択; cmd::Result]
Event を送信 [「aevt」; 「odoc」; "cmd /c dir \"C:\Windows\" /b /a-d|clip"]
Loop
スクリプト一時停止/続行 [間隔(秒): 1]
貼り付け [選択; cmd::Result]
Exit Loop If [ not IsEmpty ( cmd::Result )]
End Loop
フィールドへ移動 []

PowerShell|fmpプロトコル を使用
スクリプト:FireCommand に引数 1 を指定して実行。
FileMakerスクリプト:
スクリプト名:FireCommand
If [ not IsEmpty ( $Return )]
フィールド設定 [cmd::Result; $return]
フィールドへ移動 []
現在のスクリプト終了 []
Else If [Get ( スクリプト引数 ) = 1]
変数を設定 [$Script; 値:Quote ( Substitute (
"$Rsult= cmd /c dir \"C:\Windows\" /b /a-d  ;
$return=$Rsult -join \"%0A\" ;
Start-Process ('fmp://$/" & Get ( ファイル名 ) & "?script=FireCommand&$Return='+$return);
" ; ¶ ; "" ) )]
Event を送信 [「aevt」; 「odoc」; "powershell -Command " & $Script ]
End If

プラグイン:ScriptMakerPS を使用
FileMakerスクリプト:
フィールド設定 [cmd::Result; SMPS_Exe( "cmd /c dir \"C:\Windows\" /b /a-d" )]


FileMakerの補助も出来る ProcessMakerTL を使用
※ProcessMakerTL.exe を起動しておく必要があります。
FileMakerスクリプト:
URL から挿入 [選択; ダイアログなし; cmd::Result; "http://127.0.0.1:8081/?target=cmd&Arguments=" & GetAsURLEncoded ( "/c dir \"C:\Windows\" /b /a-d" )]

2017年4月15日土曜日

PowerShell, SetForegroundWindow, SendWait

PowerShell, SetForegroundWindow, SendWait

Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class FW {
     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     public static extern bool SetForegroundWindow(IntPtr hWnd);
      [DllImport("user32.dll")]
     public static extern IntPtr GetForegroundWindow();
  }
"@
$hWnd =$null
$FGhWnd =$null
add-type -AssemblyName System.Windows.Forms
$process_list = get-process -Name powershell
 foreach($process in $process_list){
$hWnd=$process.MainWindowHandle
[void][bool][FW]::SetForegroundWindow($hWnd)
Start-Sleep -m 10
$FGhWnd=[FW]::GetForegroundWindow()
if($hWnd -eq $FGhWnd){break}
}
if(($hWnd -eq $FGhWnd ) -and ( $hWnd-ne $null)){
[System.Windows.Forms.SendKeys]::SendWait("こんにちわ{ENTER}");
#[System.Windows.Forms.SendKeys]::SendWait("ipconfig{ENTER}");
"Sent"
}
$hWnd
$FGhWnd
$process.Name
$process.id

2017年4月2日日曜日

自作のC#コンソールアプリをMac OS で動かして、FileMakerで使う。

自作のC#コンソールアプリをMac OS で動かして、FileMakerで使う。

C#コンソールアプリをMac OS Xで使用するには、「Mono」が必要です。
Mono for Mac OS X
http://www.mono-project.com/download/#download-mac

VisualStudio 2015 で、コンソールアプリを作成。
引数を受け取って、"Hello World!" と 引数を返す、カンタンなモノ。

using System;
namespace HelloWorld
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World! {0}", args[0]);
}
}
}

対象のフレームワークは、「.Net Framework 4.5」でビルド。
HelloWorld.exe ができました。

Macの /Applications フォルダに入れます。(任意の場所でもかまいません。)
/Applications/HelloWorld.exe

AppleScriptの作成。


do shell script "/Library/Frameworks/Mono.framework/Versions/4.8.0/bin/mono-sgen32 /Applications/HelloWorld.exe こんにちわ!"
結果:
"Hello World! こんにちわ!
が返ってきます。


以下でもイケると思ったのですが、monoコマンドが無いとエラーになります。
do shell script "mono /Applications/HelloWorld.exe こんにちわ!"
以下のようにフルパスが、必要です。
do shell script "/Library/Frameworks/Mono.framework/Versions/4.8.0/bin/mono-sgen32 ...
ちなみに、「mono-sgen64」とすると、64bitで動作するようです。
do shell script "/Library/Frameworks/Mono.framework/Versions/4.8.0/bin/mono-sgen64 ...
※※VisualStudio 2015 で、プラットフォームのターゲット:Any CPUにした場合。

FileMakerに組み込み:

以下のようなAppleScriptになるようにFileMakerのスクリプトを組みます。

set varResult to (do shell script " /Library/Frameworks/Mono.framework/Versions/4.8.0/bin/mono-sgen32 \"/Applications/HelloWorld.exe\" \"こんにちわ!\nこんばんわ。\" ")
 set field "Result" of current record to varResult as text

FileMakerのファイル(例):
ファイル名:AppleScript
テーブル名:AppleScript
フィールド:Target|C#で作ったコンソールアプリのフルパスを設定
フィールド:Argument|引数を設定
フィールド:Result|結果

FileMakerのスクリプト(例):
スクリプト名:Run_AppleScript
フィールドへ移動 []
変数を設定 [$ShellScript; 値:
Let([
Script=
"
/Library/Frameworks/Mono.framework/Versions/4.8.0/bin/mono-sgen32 {Target} {arg}
"
];
Substitute ( Script ;
["`" ; "\""]
; ["{Target}" ; Quote ( AppleScript::Target )]
; ["{arg}" ; Quote ( Substitute ( AppleScript::Argument ; ¶ ; "\n" ) )]
)
)
変数を設定 [$AppleScript; 値:
Let([
Script=
"
set varResult to (do shell script {ShellScript})¶
set field `Result` of current record to varResult as text
"
];
Substitute ( Script ;
["`" ; "\""]
; ["{ShellScript}" ; Quote ( $ShellScript )] )
)
AppleScript を実行 [$AppleScript]

フィールド:Target「/Applications/HelloWorld.exe」
フィールド:Argument「こんにちわ!こんばんわ。」

とにゅうりょくし、スクリプト名:Run_AppleScript を実行。
フィールド:Result に
Hello World! こんにちわ!こんばんわ。
が入ります。


おまけ!
FileMaker補助アプリケーション「ProcessMakerTL(プロセス メーカー ティー エル)」を使った場合、FileMakerのスクリプト「URLから挿入」で同じことができるようになります。ターゲットアプリのパスをWinとMacで切り替えれば、Windowsでも戻り値の取得が出来ます。
ProcessMakerTL(プロセス メーカー ティー エル)
https://sites.google.com/site/processmakertl/