アラートダイアログに簡易UIを実装して便利なダイアログ部品を整備する計画の一環として作成した、アラートダイアログにNSTextFieldを2つ+ラベル用のfield2つを作成して、ユーザーからの数値入力を取得するAppleScriptです。
スクリプトエディタ、ScriptDebuggerの両方で実行できますが、Script Menuから実行するとダイアログが最前面に表示されません。Script Menuから実行する場合にはAppleScript書類の状態で呼び出すのではなく、AppleScriptアプレットに書き出して呼び出すのがよいでしょう。
macOS 10.14上では本Scriptの実行は明確にメインスレッド上で行うことを要求されます。Control-Comand-Rで実行してください。
AppleScript名:アラートダイアログ上にTextField x 2を表示 v2 |
— Created 2019-05-02 by Takaaki Naganoya — 2019 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property NSView : a reference to current application’s NSView property NSAlert : a reference to current application’s NSAlert property NSTextField : a reference to current application’s NSTextField property NSRunningApplication : a reference to current application’s NSRunningApplication property theResult : 0 property returnCode : 0 set paramObj to {myMessage:"Keynoteオブジェクトの2次元詰め込み", mySubMessage:"詰め込み先の矩形サイズを数値で入力してください", mes1:"幅", mes1Default:"900", mes2:"高さ", mes2Default:"500"} set segRes to my simulateAndRetRect:paramObj –> {a1Res:900, a2Res:500} on simulateAndRetRect:paramObj –Receive Parameters set aMainMes to (myMessage of paramObj) as string –Main Message set aSubMes to (mySubMessage of paramObj) as string –Sub Message set mes1Label to (mes1 of paramObj) as string –Text Input field 1 Label set mes2Label to (mes2 of paramObj) as string –Text Input field 2 Label set aTextInputString to (mes1Default of paramObj) as string –Text Input field 1 Default value set bTextInputString to (mes2Default of paramObj) as string –Text Input field 2 Default value — Create a view set theView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 300, 60)) — create two input field and their labels pairs –NSTextFields for Input set aTextInput to makeNSTextField(100, 35, 140, 20, true, (aTextInputString), true, true) of me set bTextInput to makeNSTextField(100, 0, 140, 20, true, (bTextInputString), true, true) of me –Labels set a1TF to makeNSTextField(0, 35, 100, 20, false, (mes1Label), false, false) of me set a2TF to makeNSTextField(0, 0, 100, 20, false, (mes2Label), false, false) of me theView’s setSubviews:{a1TF, aTextInput, a2TF, bTextInput} — set up alert set theAlert to NSAlert’s alloc()’s init() tell theAlert its setMessageText:aMainMes its setInformativeText:aSubMes its addButtonWithTitle:"OK" its addButtonWithTitle:"Cancel" its setAccessoryView:theView end tell — show alert in modal loop NSRunningApplication’s currentApplication()’s activateWithOptions:0 my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true if (my returnCode as number) = 1001 then error number -128 set s1Val to (aTextInput’s integerValue()) as integer set s2Val to (bTextInput’s integerValue()) as integer return {a1Res:s1Val, a2Res:s2Val} end simulateAndRetRect: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: on makeNSTextField(xPos as integer, yPos as integer, myWidth as integer, myHeight as integer, editableF as boolean, setVal as string, backgroundF as boolean, borderedF as boolean) set aNSString to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(xPos, yPos, myWidth, myHeight)) aNSString’s setEditable:(editableF) aNSString’s setStringValue:(setVal) aNSString’s setDrawsBackground:(backgroundF) aNSString’s setBordered:(borderedF) return aNSString end makeNSTextField |
More from my site
(Visited 314 times, 1 visits today)