アラートダイアログ上にTextViewを表示して、指定のテキストを閲覧するAppleScriptです。
Xcode上でNSAlert.hを調べ、アラートダイアログに指定できる各種オプションを指定してみました。NSWindowLevelの変更は効いていないみたいですけれども。
AppleScript名:アラートダイアログ上にTexViewを表示_ヘルプ付き_半透明_Float_SuppressionButtonつき |
— Created 2019-07-09 by Takaaki Naganoya — 2019 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property |NSURL| : a reference to current application’s |NSURL| property NSFont : a reference to current application’s NSFont 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 NSTextView : a reference to current application’s NSTextView property NSScrollView : a reference to current application’s NSScrollView property NSUserDefaults : a reference to current application’s NSUserDefaults property NSRunningApplication : a reference to current application’s NSRunningApplication property NSModalPanelWindowLevel : a reference to current application’s NSModalPanelWindowLevel property returnCode : 0 property supState : false set asStr to do shell script "cal 2019" set paramObj to {myMessage:"Main Message", mySubMessage:"Sub information", mes1:(asStr), mesWidth:450, mesHeight:200, fontName:"Courier", fontSize:11.0, suppressionMes:"Some Option"} –my dispTextViewWithAlertdialog:paramObj–for debug my performSelectorOnMainThread:"dispTextViewWithAlertdialog:" withObject:paramObj waitUntilDone:true return (my supState) on dispTextViewWithAlertdialog:paramObj –Receive Parameters set aMainMes to (myMessage of paramObj) as string –Main Message set aSubMes to (mySubMessage of paramObj) as string –Sub Message set mesStr to (mes1 of paramObj) as string –Text Input field 1 Label set aWidth to (mesWidth of paramObj) as integer –TextView width set aHeight to (mesHeight of paramObj) as integer –TextView height set aFontName to (fontName of paramObj) as string –TextView font name set aFontSize to (fontSize of paramObj) as real –TextView font size set aSupMes to (suppressionMes of paramObj) as string –Suppression Button Titile –Detect Dark Mode set dMode to retLIghtOrDark() of me if dMode = true then set tvCol to 1 set tvAlpha to 0.8 set bCol to 0.1 set bAlpha to 0.8 set contentCol to (NSColor’s cyanColor()) else set tvCol to 0 set tvAlpha to 0.1 set bCol to 1 set bAlpha to 0.5 set contentCol to (NSColor’s grayColor()) end if — Create a TextView with Scroll View set aScroll to NSScrollView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) set aView to NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) set aColor to NSColor’s colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:(tvAlpha) aView’s setRichText:true aView’s useAllLigatures:true aView’s setTextColor:(contentCol) aView’s setFont:(NSFont’s fontWithName:aFontName |size|:aFontSize) aView’s setDrawsBackground:false aView’s setBackgroundColor:aColor aView’s setOpaque:(false) aView’s setString:mesStr aScroll’s setDocumentView:aView –aScroll’s setBorderType:(current application’s NSBezelBorder) –aScroll’s setBorderType:(current application’s NSGrooveBorder) –aScroll’s setBorderType:(current application’s NSLineBorder) aScroll’s setBorderType:(current application’s NSNoBorder) –Ruler –aScroll’s setHasHorizontalRuler:(true) –aScroll’s setRulersVisible:(true) aView’s enclosingScrollView()’s setHasVerticalScroller:true set anImage to current application’s NSImage’s imageNamed:(current application’s NSImageNameComputer) — set up alert set theAlert to NSAlert’s alloc()’s init() tell theAlert its setIcon:(anImage) –for Messages its setMessageText:(aMainMes) its setInformativeText:(aSubMes) –for Buttons its addButtonWithTitle:"OK" its addButtonWithTitle:"Cancel" –Add Accessory View its setAccessoryView:(aScroll) –for Help Button its setShowsHelp:(true) its setDelegate:(me) its setAlertStyle:0 —-0…2 its setShowsSuppressionButton:(true) –「今後このメッセージを表示しない」チェックボックスを表示 set suppressionB to its suppressionButton set myWindow to its |window| end tell myWindow’s setOpaque:(false) myWindow’s setBackgroundColor:(NSColor’s colorWithCalibratedWhite:(bCol) alpha:(bAlpha)) myWindow’s setLevel:(NSModalPanelWindowLevel) suppressionB’s setTitle:(aSupMes) suppressionB’s setState:(true) —default state — 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 my supState to (suppressionB’s state) as boolean end dispTextViewWithAlertdialog: on doModal:aParam set (my returnCode) to aParam’s runModal() end doModal: on alertShowHelp:aNotification display dialog "Help Me!" buttons {"OK"} default button 1 with icon 1 return false –trueを返すと親ウィンドウ(アラートダイアログ)がクローズする end alertShowHelp: –ダークモードの判定。ダークモード時:true、ライトモード時:falseが返る。System Eventsを使っていないのは、macOS 10.14以降対策(承認を取得しなくてもいいように) on retLIghtOrDark() set curMode to (current application’s NSUserDefaults’s standardUserDefaults()’s stringForKey:"AppleInterfaceStyle") as string return (curMode = "Dark") as boolean end retLIghtOrDark |
More from my site
(Visited 88 times, 1 visits today)