ソースは下にあります
◎ 『テキスト早打ち』
文字数を選び、数字かアルファベットまたは平仮名の中から選択したゲームモードを決定し、早打ちタイムを競います。
・設定画面
・ゲーム開始前のカウントダウン
・結果
・Nextで次のゲーム設定へ
という流れです。
use AppleScript version "2.7" use scripting additions use framework "Foundation" use framework "AppKit" global debugMode set debugMode to true --デバグモードがtrueではログが表示されます。アプリケーションで保存する際はfalseをお勧めします。 global theWindow property questionLine : 3 property questionLevel : 1 property minQuestionLine : 1 property maxQuestionLine : 10 property recordTime : -1 global stage1, textField1, textField2, textField3, textField4, textField4, textField5, button1, button2 global stage2, textField6, textField7, slider1, popupButton1, button3 global randomText, questionText global startTime global updateTimer1 global closeFlg set closeFlg to false ---ウインドウを閉じたときのスクリプトない通知用。falseはまだ閉じてないの意味 if debugMode then --デバグモードの状態を表示 log "debugMode : ON" else log "debugMode : Off" end if my performSelectorOnMainThread:"mainMakeObject:" withObject:(missing value) waitUntilDone:true log "End" on mainMakeObject:inputObj -- メインルーチン (* 基本のウインドウを作成 *) set aTitle to "test window" set theRect to current application's NSMakeRect(0, 0, 300, 450) my makeNewWindow_(aTitle, theRect) -- 一度、テンプレートで作った後に... tell theWindow -- -- ...変更部分を指示 setMinSize_(current application's NSMakeSize(300, 450)) setMaxSize_(current application's NSMakeSize(300, 450)) |center|() end tell
(* ウインドウ上に貼るオブジェクトのサブルーチンへ *) my makeObject()
(* スクリプトが終了してしまうとログが表示されなくなるので、ここを使ってウインドウが閉じるまで終了を延期をする *) if debugMode then repeat if closeFlg then my closeWin:theWindow exit repeat end if delay 0.2 end repeat end if end mainMakeObject: on makeNewWindow_(aTitle, theRect) set aScreen to current application's class "NSScreen"'s mainScreen() set aStyle to 7 set theWindow to current application's class "NSWindow"'s alloc() theWindow's initWithContentRect:theRect styleMask:aStyle backing:false defer:false screen:aScreen tell theWindow setBackgroundColor_(current application's class "NSColor"'s colorWithCalibratedRed:0.95 green:0.95 blue:0.95 alpha:1.0) setTitle_(aTitle) setDelegate_(me) setDisplaysWhenScreenProfileChanges_(true) setReleasedWhenClosed_(true) orderFront_(me) end tell set wController to current application's class "NSWindowController"'s alloc() tell wController initWithWindow_(theWindow) showWindow_(me) end tell end makeNewWindow_ (* 配置する各オブジェクトを配置する *) on makeObject() set windowRect to theWindow's contentView's frame() set {{windowX, windowY}, {windowWidth, windowHeight}} to windowRect try ----- make new NSView (stage1-play view) set stage1 to current application's class "NSView"'s alloc()'s initWithFrame:windowRect stage1's setBackgroundColor:(current application's class "NSColor"'s lightGrayColor()) theWindow's contentView()'s addSubview:stage1 ----- make new NSTextFiels (Time View text) set theRect to current application's NSMakeRect(0, 360, 300, 50) set textField1 to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell textField1 setSelectable_(false) setBordered_(false) setStringValue_("00:00.00") setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:46.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentCenter) end tell stage1's addSubview:textField1 ----- make new NSTextFiels (Question text) set theRect to current application's NSMakeRect(50, 280, 200, 50) set textField2 to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell textField2 setSelectable_(false) setBordered_(true) setStringValue_("?") setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:36.0) setTextColor_(current application's class "NSColor"'s colorWithCalibratedRed:0.2 green:0.0 blue:0.8 alpha:1.0) setDrawsBackground_(true) setBackgroundColor_(current application's class "NSColor"'s whiteColor()) setAlignment_(current application's NSTextAlignmentCenter) end tell stage1's addSubview:textField2 ----- make new NSTextFiels (NSTextFieldArrow mark) set theRect to current application's NSMakeRect(120, 245, 60, 30) set aTextField to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell aTextField setSelectable_(false) setBordered_(false) setStringValue_("⬇︎") setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:24.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentCenter) end tell stage1's addSubview:aTextField ----- make new NSTextFiels (Answer Input Field) set theRect to current application's NSMakeRect(50, 190, 200, 50) set textField3 to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell textField3 setEditable_(true) setBordered_(true) setStringValue_("") setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:36.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(true) setBackgroundColor_(current application's class "NSColor"'s whiteColor()) setAlignment_(current application's NSTextAlignmentCenter) end tell stage1's addSubview:textField3 ----- make New NSButton 1 (Enter) set theRect to current application's NSMakeRect(80, 60, 140, 30) set button1 to current application's class "NSButton"'s alloc()'s initWithFrame:theRect tell button1 setEnabled_(false) setBezelStyle_(1) setButtonType_(0) setTitle_("Enter") setTarget_(me) setAction_("buttonAction1:") setKeyEquivalent_(ASCII character 13) end tell stage1's addSubview:button1 ----- make New NSButton 2 (Next Game) set theRect to current application's NSMakeRect(80, 30, 140, 30) set button2 to current application's class "NSButton"'s alloc()'s initWithFrame:theRect tell button2 setEnabled_(false) setBezelStyle_(1) setButtonType_(0) setTitle_("Next >") setTarget_(me) setAction_("buttonAction2") end tell stage1's addSubview:button2 ----- make new NSTextFiels (print flg) set theRect to current application's NSMakeRect(80, 80, 140, 210) set textField4 to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell textField4 setSelectable_(false) setBordered_(false) setStringValue_("") setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:120.0) setTextColor_(current application's class "NSColor"'s redColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentCenter) setAlphaValue_(0.8) end tell stage1's addSubview:textField4 ----- make new NSTextFiels (recordTime text) set theRect to current application's NSMakeRect(50, 100, 200, 50) set textField5 to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell textField5 setSelectable_(false) setBordered_(false) setStringValue_("レコードタイム") setFont_(current application's class "NSFont"'s fontWithName:"Osaka" |size|:18.0) setTextColor_(current application's class "NSColor"'s orangeColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentCenter) setAlphaValue_(0.0) end tell stage1's addSubview:textField5
----- make new NSView (stage2-Setting View) set stage2 to current application's class "NSView"'s alloc()'s initWithFrame:windowRect tell stage2 setBackgroundColor_(current application's class "NSColor"'s clearColor()) setAlphaValue_(1.0) end tell theWindow's contentView()'s addSubview:stage2 ----- make new NSImageView (Background) set theNSBezierPath to current application's class "NSBezierPath"'s bezierPath() set theRect to current application's NSMakeRect(windowX + 30, windowY + 30, windowWidth - 60, windowHeight - 60) theNSBezierPath's appendBezierPathWithRoundedRect:theRect xRadius:10 yRadius:10 set anImage to current application's class "NSImage"'s alloc()'s initWithSize:(current application's NSMakeSize(windowWidth, windowHeight)) -- anImage's lockFocus() set fillColor to current application's class "NSColor"'s colorWithCalibratedRed:1.0 green:0.95 blue:0.8 alpha:0.95 fillColor's |set|() theNSBezierPath's fill() set strokeColor to current application's class "NSColor"'s colorWithCalibratedRed:1.0 green:0.7 blue:0.8 alpha:0.95 strokeColor's |set|() theNSBezierPath's setLineWidth:2 theNSBezierPath's stroke() anImage's unlockFocus() -- set aImageView to current application's class "NSImageView"'s alloc()'s initWithFrame:(windowRect) tell aImageView setEditable_(false) setImageFrameStyle_(0) setImageAlignment_(0) setImageScaling_(0) setImage_(anImage) end tell stage2's addSubview:aImageView anImage's release() aImageView's release() ----- make new NSTextField (title) set theRect to current application's NSMakeRect(40, 370, 200, 50) set aTextField to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell aTextField setSelectable_(false) setBordered_(false) setStringValue_("Question 設定") setFont_(current application's class "NSFont"'s userFontOfSize:18.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentCenter) end tell stage2's addSubview:aTextField ----- make new NSTextField (contents text) set theRect to current application's NSMakeRect(40, 360, 150, 24) set aTextField to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell aTextField setSelectable_(false) setBordered_(false) setStringValue_("文字数:") setFont_(current application's class "NSFont"'s userFontOfSize:14.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentLeft) end tell stage2's addSubview:aTextField ----- make new NSTextField (input field : word count) set theRect to current application's NSMakeRect(100, 363, 40, 23) set textField6 to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell textField6 setDelegate_(me) setEditable_(true) setBordered_(false) setStringValue_(questionLine as text) setFont_(current application's class "NSFont"'s userFontOfSize:14.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(true) setBackgroundColor_(current application's class "NSColor"'s colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:0.7) setAlignment_(current application's NSTextAlignmentCenter) end tell stage2's addSubview:textField6 -----make new NSSlider (input slider : word count) set aTicks to maxQuestionLine - minQuestionLine + 1 set slider1 to current application's class "NSSlider"'s sliderWithValue:questionLine minValue:1 maxValue:10 target:me action:"sliderAction1:" tell slider1 setFrame_(current application's NSMakeRect(40, 338, 220, 23)) setNumberOfTickMarks_(aTicks) setAllowsTickMarkValuesOnly_(true) end tell stage2's addSubview:slider1 ----- make new NSTextField (minimum) set theRect to current application's NSMakeRect(45, 315, 50, 24) set aTextField to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell aTextField setSelectable_(false) setBordered_(false) setStringValue_(minQuestionLine as text) setFont_(current application's class "NSFont"'s userFontOfSize:11.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentLeft) end tell stage2's addSubview:aTextField ----- make new NSTextField (maximum) set theRect to current application's NSMakeRect(240, 315, 50, 24) set aTextField to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell aTextField setSelectable_(false) setBordered_(false) setStringValue_(maxQuestionLine as text) setFont_(current application's class "NSFont"'s userFontOfSize:11.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentLeft) end tell stage2's addSubview:aTextField aTextField's release() -----make new NSTextField (contents text) set theRect to current application's NSMakeRect(40, 280, 150, 24) set aTextField to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell aTextField setSelectable_(false) setBordered_(false) setStringValue_("難易度:") setFont_(current application's class "NSFont"'s userFontOfSize:14.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(false) setAlignment_(current application's NSTextAlignmentLeft) end tell stage2's addSubview:aTextField ----- make new NSPopupButton (Level select) set theRect to current application's NSMakeRect(40, 255, 150, 35) set popupButton1 to current application's class "NSPopUpButton"'s alloc()'s initWithFrame:theRect pullsDown:false tell popupButton1 addItemsWithTitles_({"初級", "中級", "上級"}) setTarget_(me) setAction_("popupbuttonAction1:") end tell stage2's addSubview:popupButton1 ----- make new NSTextField (Explanation) set aText to "初級:0~9の数字のみ" set theRect to current application's NSMakeRect(40, 150, 220, 100) set textField7 to current application's class "NSTextField"'s alloc()'s initWithFrame:theRect tell textField7 setSelectable_(false) setBordered_(false) setStringValue_(aText) setFont_(current application's class "NSFont"'s userFontOfSize:14.0) setTextColor_(current application's class "NSColor"'s blackColor()) setDrawsBackground_(true) setAlignment_(current application's NSTextAlignmentLeft) end tell stage2's addSubview:textField7 ----- set theRect to current application's NSMakeRect(120, 100, 60, 30) set button3 to current application's class "NSButton"'s alloc()'s initWithFrame:theRect tell button3 setBezelStyle_(1) setButtonType_(0) setTitle_("Start") setTarget_(me) setAction_("buttonAction3:") end tell stage2's addSubview:button3 -----
textField6's becomeFirstResponder() --textField6にフォーカスを当てる set randomText to "0123456789" on error errText log errText end try end makeObject (* スライダーの数値が変更された時に文字数表示のテキスト部分を反映する *) on sliderAction1:sender set questionLine to (slider1's value()) as integer textField6's setStringValue:(questionLine as text) end sliderAction1: (* 文字数表示のテキストが変更されたら、スライダーも反映する *) on controlTextDidEndEditing:aNotification set ans to textField6's stringValue() try set ans to ans as integer on error return end try if ans < minQuestionLine then set ans to minQuestionLine if ans > maxQuestionLine then set ans to maxQuestionLine textField6's setStringValue:ans slider1's setValue:(ans) end controlTextDidEndEditing: (* ポップアップが変更された時 *) on popupbuttonAction1:(sender) set popupNo to popupButton1's indexOfSelectedItem() set aText to item (popupNo + 1) of {"初級:0~9の数字のみ", "中級:アルファベット", "上級:ひらがな"} textField7's setStringValue:aText if popupNo = 0 then set randomText to "0123456789" else if popupNo = 1 then set randomText to "" repeat with i from 97 to 122 set randomText to randomText & (ASCII character i) end repeat else if popupNo = 2 then set randomText to "" repeat with i from 12353 to 12436 set randomText to randomText & (character id i) end repeat end if end popupbuttonAction1: (* Enterボタンが押された時の処理 *) on buttonAction1:(sender) try tell updateTimer1 to invalidate() end try try set getTime to (current application's NSDate's timeIntervalSinceReferenceDate()) - startTime set aIntegerTime to getTime div 1 set aMin to aIntegerTime div 60 set aSec to aIntegerTime mod 60 set mSec to ((getTime mod 1) * 100) as integer set aTime to (aMin as text) & ":" & (text -2 thru -1 of ("0" & aSec)) & "." & (text -2 thru -1 of ("0" & mSec)) textField1's setStringValue:(aTime) on error log "Err" end try button1's setEnabled:false textField3's setEnabled:false delay 0.2 set ans to (textField3's stringValue()) as text if questionText = ans then textField4's setTextColor:(current application's class "NSColor"'s redColor()) textField4's setStringValue:("○") if (recordTime = -1) or (recordTime > getTime) then set recordTime to getTime delay 1 textField5's setAlphaValue:1.0 end if else textField4's setTextColor:(current application's class "NSColor"'s blueColor()) textField4's setStringValue:("×") end if delay 1 button2's setEnabled:true end buttonAction1: (* Nextボタンが押されたときの処理 *) on buttonAction2() delay 1 button1's setEnabled:false button2's setEnabled:false stage2's setAlphaValue:1.0 textField3's resignFirstResponder() textField6's becomeFirstResponder() textField5's setAlphaValue:0.0 textField1's setStringValue:"00:00.000" textField4's setStringValue:("") end buttonAction2 (* Satrtボタンがクリックされたときの処理 *) on buttonAction3:(sender) textField6's resignFirstResponder() --textField6からフォーカスをはずす textField2's setStringValue:"?" textField3's setStringValue:"" if questionLine < 6 then set aFontSize to 36.0 else set aFontSize to 30.0 set popupNo to popupButton1's indexOfSelectedItem() if popupNo = 2 then set aFontSize to aFontSize * 0.5 end if textField1's setFont:(current application's class "NSFont"'s userFontOfSize:aFontSize) textField3's setFont:(current application's class "NSFont"'s userFontOfSize:aFontSize) textField4's setTextColor:(current application's class "NSColor"'s redColor()) stage2's setAlphaValue:0.0 -----カウントダウン repeat with aChara in {"3", "2", "1", ""} delay 1 (textField4's setStringValue:(aChara)) end repeat ----- textField3's setEnabled:true textField3's becomeFirstResponder() --textField3にフォーカスを当てる ----- set questionText to "" repeat with i from 1 to questionLine set questionText to questionText & (some item of randomText) end repeat ----- textField2's setStringValue:questionText button1's setEnabled:true set startTime to current application's NSDate's timeIntervalSinceReferenceDate() set updateTimer1 to current application's class "NSTimer"'s ¬ scheduledTimerWithTimeInterval:0.08 target:me selector:"selector1:" userInfo:(missing value) repeats:true end buttonAction3: (* タイマー処理部 *) on selector1:(sender) try set getTime to (current application's NSDate's timeIntervalSinceReferenceDate()) - startTime set aIntegerTime to getTime div 1 set aMin to aIntegerTime div 60 set aSec to aIntegerTime mod 60 set mSec to ((getTime mod 1) * 100) as integer set aTime to (aMin as text) & ":" & (text -2 thru -1 of ("0" & aSec)) & "." & (text -2 thru -1 of ("0" & mSec)) textField1's setStringValue:(aTime) on error log "TimerErr" try tell updateTimer1 to invalidate() end try end try end selector1: (* ウインドウを閉じるためのアクションを受けた時 *) on windowShouldClose:theEvent try tell updateTimer1 to invalidate() end try if debugMode then set closeFlg to true --デバグモードの場合は閉じるフラグを立てて別の終了準備スクリプトを経由させる else my closeWin:theWindow --デバグモードでない場合は直接閉じてしまう end if end windowShouldClose: (* ウインドウを閉じるための実際のアクション *) on closeWin:aWindow aWindow's |close|() --quit me --アプリケーション保存の場合はウインドウが閉じたら終了するように、これを使う。 end closeWin: |