アラートダイアログ上にPathcontrolを表示して、ファイルパス選択をドラッグ&ドロップで受け付けるAppleScriptの改良版です。
実行中にアピアランスのDark Mode/Light Modeへの変更が行われたときの対応を追加してみました。
実行途中でアピアランスの変更が行われると、OSが提供している部品をそのまま利用している分にはOSの管理下で色変更が行われますが、あとから色指定している部品はそのままです(↑)。上記の(↑)ように、割と違和感があるというか、実用上困る(そもそもアピアランス変更をそんな時に行う方がどうかしているとは思うのですが)ので、対処してみました。
部品はひととおり作ってあったので、組み合わせただけです。アピアランスの変更Notificationを受信する部品や、アピアランステーマを判定する部品などです。
部品の組み合わせ方にも些細なノウハウがあるので、一応やっておいたことには意義があると思っています。
AppleScript名:アラートダイアログ上にpath control x2を表示 v2 |
— Created 2019-08-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 NSColor : a reference to current application’s NSColor property NSTextField : a reference to current application’s NSTextField property NSPathControl : a reference to current application’s NSPathControl property NSUserDefaults : a reference to current application’s NSUserDefaults property NSRunningApplication : a reference to current application’s NSRunningApplication property NSDistributedNotificationCenter : a reference to current application’s NSDistributedNotificationCenter property theResult : 0 property returnCode : 0 property resList : {} property aPathControl : missing value property bPathControl : missing value set paramObj to {myMessage:"ファイルの入出力フォルダ選択", mySubMessage:"どれか選択してください。", fromPathMes:"移動元:", toPathMes:"移動先:"} my performSelectorOnMainThread:"chooseTwoPath:" withObject:paramObj waitUntilDone:true return resList –> {fromPathRes:"/Users/me/Desktop/keyn1.png", toPathRes:"/Users/me/Desktop/scriptmenu1.png"} on chooseTwoPath:paramObj set aMainMes to myMessage of paramObj set aSubMes to mySubMessage of paramObj set fromLabel to fromPathMes of paramObj set toLabel to toPathMes of paramObj —Dark Mode Notification受信開始 NSDistributedNotificationCenter’s defaultCenter()’s addObserver:me selector:"darkModeChanged:" |name|:"AppleInterfaceThemeChangedNotification" object:(missing value) — create a view set theView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 600, 60)) — create two path control & text field set aPathControl to NSPathControl’s alloc()’s initWithFrame:(current application’s NSMakeRect(100, 35, 700, 20)) set bPathControl to NSPathControl’s alloc()’s initWithFrame:(current application’s NSMakeRect(100, 0, 700, 20)) –Set path control’s bg colors set {aCol, bCol} to pathControlSrtringColor() of me aPathControl’s setBackgroundColor:(aCol) bPathControl’s setBackgroundColor:(bCol) set aHome to current application’s |NSURL|’s fileURLWithPath:(current application’s NSHomeDirectory()) –initial dir aPathControl’s setURL:aHome bPathControl’s setURL:aHome set a1TF to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 35, 100, 20)) set a2TF to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 100, 20)) a1TF’s setEditable:false a2TF’s setEditable:false a1TF’s setStringValue:fromLabel a2TF’s setStringValue:toLabel a1TF’s setDrawsBackground:false a2TF’s setDrawsBackground:false a1TF’s setBordered:false a2TF’s setBordered:false theView’s setSubviews:{a1TF, aPathControl, a2TF, bPathControl} — 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 (aPathControl’s |URL|’s |path|()) as string set s2Val to (bPathControl’s |URL|’s |path|()) as string set resList to {fromPathRes:s1Val, toPathRes:s2Val} –Notification受信停止 NSDistributedNotificationCenter’s defaultCenter()’s removeObserver:me |name|:"AppleInterfaceThemeChangedNotification" object:(missing value) end chooseTwoPath: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: –ダークモードの判定。ダークモード時:true、ライトモード時:falseが返る on retLIghtOrDark() set curMode to (NSUserDefaults’s standardUserDefaults()’s stringForKey:"AppleInterfaceStyle") as string return (curMode = "Dark") as boolean end retLIghtOrDark –aMaxValを最大値とする数値でNSColorを作成して返す on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer) set aRedCocoa to (redValue / aMaxVal) as real set aGreenCocoa to (greenValue / aMaxVal) as real set aBlueCocoa to (blueValue / aMaxVal) as real set aAlphaCocoa to (alphaValue / aMaxVal) as real set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa return aColor end makeNSColorFromRGBAval –Notification handler on darkModeChanged:(aNotification) set {aCol, bCol} to pathControlSrtringColor() of me aPathControl’s setBackgroundColor:(aCol) bPathControl’s setBackgroundColor:(bCol) end darkModeChanged: on pathControlSrtringColor() set dMode to retLIghtOrDark() of me –Dark mode:true if dMode = false then –Light Mode set aCol to (NSColor’s cyanColor()) set bCol to (NSColor’s yellowColor()) else –Dark Mode set aCol to (makeNSColorFromRGBAval(0, 96, 65, 255, 255) of me) set bCol to (makeNSColorFromRGBAval(93, 92, 0, 255, 255) of me) end if return {aCol, bCol} end pathControlSrtringColor |