指定の画像を、画面の中央にウィンドウ表示するAppleScriptです。
NSWindowを作成して、そこにNSImageViewを表示し、さらにその上に透明状態のボタンを表示しています。画像をクリックすると、その上にかぶせているボタンがクリックを受信してウィンドウのクローズを行います。
AppleScript名:イメージビュー+ボタンを作成 v2 |
— Created 2015-12-11 by Takaaki Naganoya — Modified 2018-04-08 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" property windisp : false set aFilePath to POSIX path of (choose file) set aTitle to "イメージビューのじっけん/ImageView Test" set aButtonMSG to "OK" set wRes to dispImageView(aTitle, aFilePath, aButtonMSG, 10) of me on dispImageView(aTitle as text, dispImagePath, aButtonMSG as text, timeOutSecs as number) set (my windisp) to true –指定パスからNSImageに画像を読み込む set aImageURL to current application’s |NSURL|’s fileURLWithPath:dispImagePath set {theResult, theValue} to aImageURL’s getResourceValue:(reference) forKey:(current application’s NSURLTypeIdentifierKey) |error|:(missing value) if theResult is not equal to true then return false –the file is not a image set aImage to current application’s NSImage’s alloc()’s initWithContentsOfURL:aImageURL set aSize to aImage’s |size|() set imgWidth to width of aSize set imgHeight to height of aSize –NSImageViewを作って画像を読み込む set aView to current application’s NSImageView’s alloc()’s init() aView’s setBounds:(current application’s NSMakeRect(0, 0, imgWidth, imgHeight)) aView’s setImageScaling:(current application’s NSImageScaleProportionallyUpOrDown) aView’s setEditable:false aView’s setImage:aImage –Windowをつくる set aWin to makeWinWithView(aView, imgWidth, imgHeight, aTitle, 1.0) set wController to current application’s NSWindowController’s alloc() wController’s initWithWindow:aWin wController’s showWindow:me –Buttonをつくる set bButton to (current application’s NSButton’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, imgWidth, imgHeight))) bButton’s setTitle:"OK" bButton’s setTransparent:true bButton’s setTarget:me bButton’s setAction:("clicked:") aView’s addSubview:bButton aWin’s makeKeyAndOrderFront:me –Windowを表示状態に 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 my closeWin:aWin return true –Safely closed end dispImageView –Button Clicked Event Handler on clicked:aSender log {"clicked:"} set (my windisp) to false end clicked: –make Window for Input on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV) set aScreen to current application’s NSScreen’s mainScreen() set aFrame to {{0, 0}, {aWinWidth, aWinHeight}} set aBacking to current application’s NSTitledWindowMask –NSBorderlessWindowMask set aDefer to current application’s NSBackingStoreBuffered — Window set aWin to current application’s NSWindow’s alloc() (aWin’s initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen) –aWin’s setBackgroundColor:(current application’s NSColor’s whiteColor()) aWin’s setTitle:aTitle aWin’s setDelegate:me aWin’s setDisplaysWhenScreenProfileChanges:true aWin’s setHasShadow:true aWin’s setIgnoresMouseEvents:false aWin’s setLevel:(current application’s NSPopUpMenuWindowLevel) –NSNormalWindowLevel 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 repeat with n from 10 to 1 by -1 (aWindow’s setAlphaValue:n / 10) delay 0.02 end repeat aWindow’s |close|() end closeWin: |
More from my site
(Visited 32 times, 1 visits today)