SSブログ

なんとなくデジタル時計(アナログ付き)を作ってみた [AppleScript辞書はつくれるか?]

デスクトップで活躍する(?)時計が欲しかったので作ってみました。


(二番煎じか?)


   image200519a.jpg


script AppDelegate

property parent : class "NSObject"

property theWindow : missing value

    

    global mainView

    global updateTimer

    global theOldDate, theOldTime

    global dateObj1, dateObj2, dateObj3, dateObj4

    global analogClock1, analogClock2, analogClock3

    global oldHours, oldMinutes, oldSeconds

    

    on awakeFromNib() --|Nib(ウインドウなど)が表示される前に処理されます|

        theWindow's setFrameAutosaveName_("mainWindow") --ウインドウの位置や大きさを記憶します

    end awakeFromNib

    

on applicationWillFinishLaunching_(aNotification) --|Nibが表示命令が出された直後に処理されます|

        set theSize to current application's NSMakeSize(300, 120) --サイズ値処理

tell theWindow --ウインドウに対してまとめて処理をします

            setContentSize_(theSize) --サイズを変更

            setMaxSize_(theSize) --最大サイズを変更

            setMinSize_(theSize) --最小サイズを変更

            setBackgroundColor_(current application's class "NSColor"'s clearColor)

                                         --(ウインドウの)バックグラウンドカラーを透明に

            setTitlebarAppearsTransparent_(true) --タイトルバーの背景を透明にする(今回は意味が無い)

            setStyleMask_(0)

         --タイトルバーの形式(0:バー無し 1: バー有り +2:クローズボタンON +4:ミニマムボタンON +8:リサイズボタンON)|

            setTitle_("時計") --タイトルバー内のタイトル変更(今回は意味が無い)

            setMovableByWindowBackground_(true)

                               --ウインドウのバックグラウンドをドラッグすることで移動が可能になる

        end tell

        set mainView to theWindow's contentView() --ウインドウ直のビュー(コンテンツビュー)を取得

        set {redColor, greenColor, blueColor,alphaColor} to {0.8, 0.85, 0.87, 0.8}

        set aColor to current application's class "NSColor"'s ¬

             colorWithCalibratedRed_green_blue_alpha_(redColor, greenColor, blueColor, alphaColor)

        mainView's setBackgroundColor_(aColor) --ビューの背景色を変更

        

        set {theOldDate, theOldTime} to {"",""}

        

        (* デジタルパーツ *)

       --|日付部分の背面表示部(白)|

        set theRect to current application's NSMakeRect(12,80, 250, 32) --矩形値処理

        set dateObj1 to current application's class "NSTextField"'s alloc()'s ¬

                                         initWithFrame_(theRect) --TextFieldのインスタンス作成

        tell dateObj1

            setEditable_(false) --編集を不可に

            setStringValue_("") --表示文字指定(ここで文字を入れてしまうと、なぜか影が残ってしまう)

            setDrawsBackground_(false) --背景色を非表示

            setBordered_(false) --枠を非表示

            setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:24.0)

                                                                                   --書体を指定

            setTextColor_(current application's class "NSColor"'s whiteColor) --文字の色を指定

            setAlignment_(1) --文字の位置を変更(NSTextAlignmentRight テキストを右揃えに(= 1) (iOSは2)

        end tell

        mainView's addSubview_(dateObj1) --作成したテキストフィールドを設置

         --|日付部分の前面表示部(黒)|

        set theRect to current application's NSMakeRect(10, 83, 250, 32)

        set dateObj2 to current application's class "NSTextField"'s alloc()'s ¬

                                                          initWithFrame_(theRect) --日付部分の影

        tell dateObj2

            setEditable_(false)

            setStringValue_("")

            setDrawsBackground_(false)

            setBordered_(false)

            setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:24.0)

            setTextColor_(current application's class "NSColor"'s blackColor)

            setAlignment_(1)

        end tell

        mainView's addSubview_(dateObj2)

        --|時間部分の前面表示部(白)|

        set theRect to current application's NSMakeRect(51, 0, 250, 68)

        set dateObj3 to current application's class "NSTextField"'s alloc()'s ¬

                                                                  initWithFrame_(theRect)

        tell dateObj3

            setEditable_(false)

            setStringValue_("")

            setDrawsBackground_(false)

            setBordered_(false)

            setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:48.0)

            setTextColor_(current application's class "NSColor"'s whiteColor)

            setAlignment_(1)

        end tell

        mainView's addSubview_(dateObj3)

        --|時間部分の前面表示部(黒)|

        set theRect to current application's NSMakeRect(50, 3, 250, 68)

        set dateObj4 to current application's class "NSTextField"'s alloc()'s ¬

                                                                       initWithFrame_(theRect)

        tell dateObj4

            setEditable_(false)

            setStringValue_("")

            setDrawsBackground_(false)

            setBordered_(false)

            setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:48.0)

            setTextColor_(current application's class "NSColor"'s blackColor)

            setAlignment_(1)

        end tell

        mainView's addSubview_(dateObj4)

        

        (* アナログパーツ *)

       --|ベース面(他と差別化するためビューを新しく作成して重ねています)|

        set theRect to current application's NSMakeRect(3,3,110,110)

        set sabView1 to current application's class "NSView"'s alloc()'s initWithFrame_(theRect)

        mainView's addSubview_(sabView1)

        sabView1's setAlphaValue_(0.0)

        

        --|文字盤(暗い色の円だけ。文字盤と書いていながら文字は無し)|

        set anImage to current application's class "NSImage"'s alloc()'s ¬

                initWithSize_(current application's NSMakeSize(110,110)) --画像のインスタンスを作成

        anImage's lockFocus() --作成した画像に書き込みをすると宣言

            set theNSBezierPath to current application's class "NSBezierPath"'s bezierPath()

                                                               --線や塗り用のパスのインスタンスを作成

            theNSBezierPath's appendBezierPathWithOvalInRect_(current application's ¬

                                                    NSMakeRect(5,5,100,100)) --楕円形(円)を作る

            set aColor to current application's class "NSColor"'s ¬

                                 colorWithCalibratedRed_green_blue_alpha_(0.0, 0.0, 0.0, 0.05)

            aColor's |set|() --色の指定

            theNSBezierPath's fill() --パスや指定色に従って塗りをする

        anImage's unlockFocus() --画像に書き込みを終了すると宣言

        

        --|短針(時)[背景の文字盤と同じ画像インスタンスに書き込みます]|

        anImage's lockFocus()

            set theNSBezierPath to current application's class "NSBezierPath"'s bezierPath()

            set aColor to current application's class "NSColor"'s ¬

                                 colorWithCalibratedRed_green_blue_alpha_(0.8, 0.5, 0.5, 1.0)

            theNSBezierPath's moveToPoint_(current application's NSMakePoint(55,55))

                                                                             --線の最初の点を指定

            theNSBezierPath's lineToPoint_(current application's NSMakePoint(55,85))

                                        --線と続きの点を指定(これ以降指示をしないので自動的に終点になる)

            aColor's |set|()

            theNSBezierPath's setLineWidth_(4) --線の太さを指定

            theNSBezierPath's stroke() --パスや指定色に従って線を描く

        anImage's unlockFocus()

        set analogClock1 to current application's class "NSImageView"'s alloc()'s ¬

                          initWithFrame_(current application's NSMakeRect(0,0,110,110))

                               --画像を表示するためのイメージビューのインスタンスを作成

        tell analogClock1

            setEditable_(false)

            setImageFrameStyle_(0) --スレームスタイル(無し)

            setImageAlignment_(0) --イメージ位置(中央)

            setImageScaling_(0) --イメージの拡大縮小指示(原寸)

            setImage_(anImage) --表示するイメージの指定

        end tell

        sabView1's addSubview_(analogClock1)

        set thePoint to current application's NSMakePoint(55,55)

        analogClock1's translateOriginToPoint_(thePoint) --イメージの原点(中心点)の変更

 

        --|長針(分)|

        set anImage to current application's class "NSImage"'s alloc()'s ¬

                              initWithSize_(current application's NSMakeSize(110,110))

                                                                                                           --新しくイメージインスタンスを作成

        anImage's lockFocus()

        set theNSBezierPath to current application's class "NSBezierPath"'s bezierPath()

        set aColor to current application's class "NSColor"'s ¬

                           colorWithCalibratedRed_green_blue_alpha_(0.3, 0.5, 1.0, 1.0)

        theNSBezierPath's moveToPoint_(current application's NSMakePoint(55,55))

        theNSBezierPath's lineToPoint_(current application's NSMakePoint(55,100))

        aColor's |set|()

        theNSBezierPath's setLineWidth_(3)

        theNSBezierPath's stroke()

        anImage's unlockFocus()

        set analogClock2 to current application's class "NSImageView"'s alloc()'s ¬

                               initWithFrame_(current application's NSMakeRect(0,0,110,110))

        tell analogClock2

            setEditable_(false)

            setImageFrameStyle_(0)

            setImageAlignment_(0)

            setImageScaling_(0)

            setImage_(anImage)

        end tell

        sabView1's addSubview_(analogClock2)

        set thePoint to current application's NSMakePoint(55,55)

        analogClock2's translateOriginToPoint_(thePoint)

        --|秒針|

        set anImage to current application's class "NSImage"'s alloc()'s ¬

                                  initWithSize_(current application's NSMakeSize(110,110))

        anImage's lockFocus()

        set theNSBezierPath to current application's class "NSBezierPath"'s bezierPath()

        set aColor to current application's class "NSColor"'s ¬

                               colorWithCalibratedRed_green_blue_alpha_(0.3, 0.5, 0.5, 0.5)

        theNSBezierPath's moveToPoint_(current application's NSMakePoint(55,55))

        theNSBezierPath's lineToPoint_(current application's NSMakePoint(55,110))

        aColor's |set|()

        theNSBezierPath's setLineWidth_(2)

        theNSBezierPath's stroke()

        anImage's unlockFocus()

        set analogClock3 to current application's class "NSImageView"'s alloc()'s ¬

                              initWithFrame_(current application's NSMakeRect(0,0,110,110))

        tell analogClock3

            setEditable_(false)

            setImageFrameStyle_(0)

            setImageAlignment_(0)

            setImageScaling_(0)

            setImage_(anImage)

        end tell

        sabView1's addSubview_(analogClock3)

        set thePoint to current application's NSMakePoint(55,55)

        analogClock3's translateOriginToPoint_(thePoint)

        --

        set {oldHours, oldMinutes, oldSeconds} to {0.0, 0.0, 0.0}

        anImage's autorelease()

               --貼り終わったので描画用で使った一時的なインスタンスを自動的に解放する指示

                           --(使い終わる前に指示すると痛い目にあう)

        set updateTimer to current application's class "NSTimer"'s ¬

                       scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_¬

                       (0.2, me, "timerMain", missing value, true)

             --NSTimerを使用して連続した動作を可能にする((Interval)0.2秒おきに、

                       --(target)このスクリプト内[me]の、(selecter)指定のハンドラに、(userInfo)送るデータがあれば送るが

                       --今回は無し[missing value]、(repeats)繰り返しするか否か)

        sabView1's setAlphaValue_(1.0)

                         -- 一時的に不透明度を0にしていた時計のビューを不透明度 1.0 (100%)にして表示させる

        

end applicationWillFinishLaunching_

    

    on timerMain() --Timerで繰り返し動作する部分

        set {theDate, theTime} to my theDateText() --日時と時間のテキストを取得

        if theOldTime = theTime then return --日時に変更がなければ、これ以降の処理は無駄なので戻る

        (* デジタル-時間*)

        set theOldTime to theTime

        dateObj3's setStringValue_(theTime) --デジタル部分の時間を書き換え

        dateObj4's setStringValue_(theTime)

        (* アナログ *)

        set {hours: hh, minutes: mm, seconds: ss} to current date --あえて別に時間(時:分:秒)を取得

        --短針

        set r to ((oldHours / 12) + (oldMinutes / 60 / 12)) * 360

                                              --(前の角度)針の角度を計算(時と分の値を影響させた角度)

        analogClock1's rotateByAngle_(r)

                             --以前の針の角度を戻す(この角度変更は加算式なので一度リセットしなければならない)

        set r to 0 -(((hh / 12) + (mm / 60 / 12)) * 360) --新しい角度を計算

        analogClock1's rotateByAngle_(r) --新しい角度に変更する

        --長針

        set r to oldMinutes / 60 * 360

        analogClock2's rotateByAngle_(r)

        set r to 0 -(mm / 60  * 360)

        analogClock2's rotateByAngle_(r)

        --秒針

        set r to oldSeconds / 60 * 360

        analogClock3's rotateByAngle_(r)

        set r to 0 -(ss / 60 * 360)

        analogClock3's rotateByAngle_(r)

        --古い時間として記録

        set {oldHours, oldMinutes, oldSeconds} to {hh, mm, ss}

        

        (* デジタル-日付 *)

        if theOldDate = theDate then return --日付に変更しなければ何もしないで戻る

        set theOldDate to theDate

        dateObj1's setStringValue_(theDate)

        dateObj2's setStringValue_(theDate)

    end timerMain

    

    on theDateText() --|日時テキストを一括作成|

        set currentTime to current date

        set theDate to ((year of currentTime) as text) & "" & (month of currentTime as number) ¬

                                                              & "" & (day of currentTime) & ""

        set theDate to theDate & " (" & (text (weekday of (current date) as number) of ¬

                                                                            "日月火水木金土") & ")"

        set theTime to time string of currentTime

        return {theDate, theTime}

    end theDateText

    

    on applicationShouldTerminateAfterLastWindowClosed_(sender)

        return true

    end applicationShouldTerminateAfterLastWindowClosed_

on applicationShouldTerminate_(sender)

        try

            tell updateTimer to invalidate()

        end try

return current application's NSTerminateNow

end applicationShouldTerminate_

 

end script


 


nice!(0)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。