透明のNSTextView+NSWindowで時計を表示するAppleScriptです。ありものを組み合わせて作ってみました。
テキストビュー+ボタンをつくる ScriptにTimerを組み合わせたぐらいです。macOS標準装備のスクリプトエディタ上でそのまま動きますし、Script Debugger上でも動作確認ずみです。
この手のプログラムは初心者が割と作りたくなるものの、作ってもそれほど役に立たない上に、「まあ、こんなもんか」という程度にしかなりません。初心者向けといえば初心者向けですが、記述量がそれほど少なくないのが困りものです(Xcode上でAppleScriptでCocoa-Appを書いたほうがずっと短く書けることでしょう)。
それもこれも、実際に動かしてみないとわからないことでしょう。Safariで表示中の表だけCSVに書き出すScriptなどと比べると、ものすごく面白くありません。
AppleScript名:テキストビュー+ボタンを作成(フォント指定)_+Timer |
— Created 2019-10-02 by Takaaki Naganoya — 2019 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property NSFont : a reference to current application’s NSFont property NSColor : a reference to current application’s NSColor property NSTimer : a reference to current application’s NSTimer property NSScreen : a reference to current application’s NSScreen property NSButton : a reference to current application’s NSButton property NSWindow : a reference to current application’s NSWindow property NSSplitView : a reference to current application’s NSSplitView property NSTextView : a reference to current application’s NSTextView property NSWindowController : a reference to current application’s NSWindowController property NSTitledWindowMask : a reference to current application’s NSTitledWindowMask property NSFloatingWindowLevel : a reference to current application’s NSFloatingWindowLevel property NSBackingStoreBuffered : a reference to current application’s NSBackingStoreBuffered property windisp : false property aView : missing value set aFontName to "Arial-Black" set aWidth to 350 set aHeight to 120 set aTitle to "Clock Test" –Window Title set aButtonMSG to "OK" –Button Title set aRes to checkExistenceOfFont(aFontName) of me if aRes = false then display dialog "There is no <" & aFontName & "> font. Select another one." –No font return end if set dispStr to "" set paramObj to {myWidth:aWidth, myHeight:aHeight, myTitle:aTitle, myBMes:aButtonMSG, myTimeOut:180, myFontID:aFontName, myFontSize:48} –my dispTextView:aRecObj my performSelectorOnMainThread:"dispTextView:" withObject:paramObj waitUntilDone:true on dispTextView:aRecObj set aWidth to myWidth of aRecObj as integer set aHeight to myHeight of aRecObj as integer set aTitle to myTitle of aRecObj as string set aButtonMSG to myBMes of aRecObj as string set timeOutSecs to myTimeOut of aRecObj as integer set fontID to myFontID of aRecObj as string set fontSize to myFontSize of aRecObj as integer set dispStr to "" –Make Timer set aTimer to NSTimer’s scheduledTimerWithTimeInterval:1 target:me selector:"idleHandler:" userInfo:(missing value) repeats:true — Text View Background color set aColor to NSColor’s colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:0.0 set (my windisp) to true –Text View+Scroll Viewをつくる set aView to NSTextView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, aHeight)) aView’s setRichText:true aView’s useAllLigatures:true aView’s setTextColor:(NSColor’s magentaColor()) aView’s setFont:(NSFont’s fontWithName:fontID |size|:fontSize) aView’s setBackgroundColor:aColor aView’s setAlphaValue:1.0 aView’s setEditable:false –aView’s enclosingScrollView()’s setHasVerticalScroller:true –Buttonをつくる set bButton to (NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aWidth, 40))) bButton’s setTitle:aButtonMSG bButton’s setTarget:me bButton’s setAction:("clicked:") –SplitViewをつくる set aSplitV to NSSplitView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aHeight, aWidth)) aSplitV’s setVertical:false aSplitV’s addSubview:aView aSplitV’s addSubview:bButton aSplitV’s setNeedsDisplay:true –WindowとWindow Controllerをつくる set aWin to makeWinWithView(aSplitV, aWidth, aHeight, aTitle, 1.0) aWin’s makeKeyAndOrderFront:(missing value) set wController to NSWindowController’s alloc() wController’s initWithWindow:aWin aWin’s makeFirstResponder:aView aView’s setString:dispStr wController’s showWindow:me set aCount to timeOutSecs * 10 –timeout seconds * 10 repeat aCount times if (my windisp) = false then exit repeat end if delay 0.1 set aCount to aCount – 1 end repeat aTimer’s invalidate() –Stop Timer my closeWin:aWin end dispTextView: –Button Clicked Event Handler on clicked:aSender set (my windisp) to false end clicked: –make Window for Input on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV) set aScreen to NSScreen’s mainScreen() set aFrame to {{0, 0}, {aWinWidth, aWinHeight}} set aBacking to NSTitledWindowMask –NSBorderlessWindowMask set aDefer to NSBackingStoreBuffered — Window set aWin to NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) aWin’s setBackgroundColor:(NSColor’s clearColor()) aWin’s setTitle:aTitle aWin’s setDelegate:me aWin’s setDisplaysWhenScreenProfileChanges:true aWin’s setHasShadow:true aWin’s setIgnoresMouseEvents:false aWin’s setLevel:(NSFloatingWindowLevel) aWin’s setOpaque:false aWin’s setAlphaValue:alphaV –append aWin’s setReleasedWhenClosed:true aWin’s |center|() –aWin’s makeKeyAndOrderFront:(me) — Set Custom View aWin’s setContentView:aView return aWin end makeWinWithView –close win on closeWin:aWindow aWindow’s |close|() end closeWin: –指定PostScript名称のフォントがコンピューター上に存在するかどうかチェック on checkExistenceOfFont(fontName as string) if fontName = "" then return false set aFont to NSFont’s fontWithName:fontName |size|:9.0 if aFont = missing value then return false else return true end if end checkExistenceOfFont –タイマー割り込み on idleHandler:aSender set mesStr to time string of (current date) aView’s setString:mesStr end idleHandler: |
More from my site
(Visited 242 times, 1 visits today)
Alan says:
The script is great, but it will not close, when the button is clicked. To quit the script, I needed to force quit ScriptDebugger. How can I change your script to allow a quit action when the user clicks a button?
Takaaki Naganoya says:
You seems to run this script with Script Debugger. SD can not receive user’s click event by dynamic generated UI element.
You can receive click by runnning this script by Script Editor.
Each tools has pros and cons. SD has deluxe debugging feature and some kind of weakness on the other hand.
ASOC depends on the runtime environment. So, I had to write the target and verificated runtime environment.
It is a headache now….
Storm Hampusson says:
Thank you for posting this example. I’ve edited this script so that the hours, minutes and seconds strings are on separate lines, which works OK. But what code would I have to insert to decrease the line spacing between those three lines? Thanks in advance.
Takaaki Naganoya says:
You can make hours, minutes and seconds field in one object. If you want so, you can make them as NSAttributedString object. It can accept line spacings.
The easiest way is the separation. Make three NSTextView and locate them closer.
Storm Hampusson says:
Thank you for the advice, Naganoya-san. Your website is one of the best AppleScript resources on the internet :)
Takaaki Naganoya says:
To continue this blog, I wish my brand-new ebook written in English make some sales. I hope so.
–Switch Control with AppleScript
https://piyomarusoft.booth.pm/items/2042555