Macにバンドルされているメール(Mail.app)で選択された受信メール内の情報を
本文テキスト・ヘッダ・ソースそれぞれを表示させるようにする。
メールを開くのではなく、テキストやソースだけ取得するので、既読にならず、悪意のある実行プログラムも動作させないまま読むことができる。
use AppleScript version "2.7" use scripting additions use framework "Foundation" use framework "AppKit" property debugMode : true global theWindow global closeFlg global currentAppName --スクリプトエディタを使用して実行しているのか、アプリケーション済から起動されたかの判断用 global popupButton1 global selectedMail global textView1, textView2, textView3 on run set closeFlg to false -- set currentAppName to name of (current application) if currentAppName ≠ "Script Editor" then set debugMode to false if debugMode then log "debugMode : ON" else log "debugMode : Off" end if -- my performSelectorOnMainThread:"mainMakeObject:" withObject:(missing value) waitUntilDone:true -- log "End" end run on mainMakeObject:inputObj -- メインルーチン set aTitle to "メールの情報取得" set aRect to {0, 0, 500, 300} set theWindow to my makeNewWindow(aTitle, aRect, true) -- 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, aRect, moveCenter) set {windowX, windowY, windowW, windowH} to aRect set theRect to current application's NSMakeRect(windowX, windowY, windowW, windowH) set aScreen to current application's class "NSScreen"'s mainScreen() set aStyle to (current application's NSWindowStyleMaskTitled as integer) set aStyle to aStyle + (current application's NSWindowStyleMaskClosable as integer) set aStyle to aStyle + (current application's NSWindowStyleMaskMiniaturizable as integer) set aStyle to aStyle + (current application's NSWindowStyleMaskResizable as integer) set aBacking to current application's NSBackingStoreBuffered set aWindow to current application's class "NSWindow"'s alloc()'s initWithContentRect:theRect styleMask:aStyle backing:aBacking defer:false screen:aScreen tell aWindow setAlphaValue_(0.0) --作成時のチラつき回避のため、いったん不透明度を0.0にして透明に setDelegate_(me) setTitle_(aTitle) setMinSize_(current application's NSMakeSize(windowW, windowH)) setMaxSize_(current application's NSMakeSize(10000, 10000)) setBackgroundColor_(current application's class "NSColor"'s colorWithCalibratedRed:0.95 green:0.95 blue:0.95 alpha:1.0) setDisplaysWhenScreenProfileChanges_(true) setReleasedWhenClosed_(true) makeKeyAndOrderFront_(me) end tell -- if moveCenter then -- |windowをセンターに移動| aWindow's |center|() --システム任せで中央に移動(※少し上にずれてしまう)下記のScreen命令で取得できなかった場合の避難処理 -- set theScreen to current application's class "NSScreen"'s screens() set {windowCenterX, winsowCenterY} to {windowX + (windowW / 2), windowY + (windowH / 2)} repeat with screenNo from 1 to (count of theScreen) set {{screenX1, screenY1}, {screenWidth, screenHeight}} to (item screenNo of theScreen)'s frame() set {screenX2, screenY2} to {screenX1 + screenWidth, screenY1 + screenHeight} if (windowCenterX > screenX1) and (windowCenterX < screenX2) and (winsowCenterY > screenY1) and (winsowCenterY < screenY2) then set originX to screenX1 + (screenWidth / 2) - (windowW / 2) set originY to screenY1 + (screenHeight / 2) - (windowH / 2) (aWindow's setFrameOrigin:(current application's NSMakePoint(originX, originY))) exit repeat end if end repeat end if -- aWindow's setAlphaValue:1.0 --不透明度を戻して表示 -- return aWindow end makeNewWindow (* 配置する各オブジェクトを配置する *) on makeObject() set {{windowX, windowY}, {windowWidth, windowHeight}} to theWindow's contentView's frame() try -- 新しくオブジェクトを配置する部分 -------------------- --make new PopupButton set autoresizingViewFree to ((current application's NSViewWidthSizable) as integer) + ((current application's NSViewMinYMargin) as integer) --幅と下を柔軟に set theRect to current application's NSMakeRect(3, windowHeight - 32, windowWidth - 48, 35) set popupButton1 to current application's class "NSPopUpButton"'s alloc()'s initWithFrame:theRect pullsDown:false tell popupButton1 addItemsWithTitles_({"準備中"}) setAutoresizingMask_(autoresizingViewFree) setTarget_(me) setAction_("popupAction1") end tell theWindow's contentView()'s addSubview:popupButton1 --make new button set autoresizingViewMinXMargin to ((current application's NSViewMinXMargin) as integer) + ((current application's NSViewMinYMargin) as integer) --左と下を柔軟に set theRect to current application's NSMakeRect(windowWidth - 50, windowHeight - 26, 50, 24) set button1 to current application's class "NSButton"'s alloc()'s initWithFrame:theRect tell button1 setBezelStyle_(1) setButtonType_(0) setTitle_("更新") setTarget_(me) setAction_("action1") setAutoresizingMask_(autoresizingViewMinXMargin) end tell theWindow's contentView()'s addSubview:button1 --make new TabView -- TabView set autoresizingViewFree to ((current application's NSViewWidthSizable) as integer) + ((current application's NSViewHeightSizable) as integer) --天地左右の幅を柔軟に set theRect to current application's NSMakeRect(windowX, windowY, windowWidth, windowHeight - 25) set tabView to current application's class "NSTabView"'s alloc()'s initWithFrame:theRect theWindow's contentView()'s addSubview:tabView tell tabView setAutoresizingMask_(autoresizingViewFree) end tell -- ViewItem1 set tabViewItem1 to current application's class "NSTabViewItem"'s alloc() tell tabViewItem1 initWithIdentifier_("tab1") setLabel_("本文テキスト") end tell tabView's addTabViewItem:tabViewItem1 set {{x, y}, {w, h}} to (tabViewItem1's view()'s frame()) as list set viewItemRect to current application's NSMakeRect(x, y, w, h) -- ViewItem2 set tabViewItem2 to current application's class "NSTabViewItem"'s alloc() tell tabViewItem2 initWithIdentifier_("tab2") setLabel_("ヘッダ") view()'s setFrame:viewItemRect -- tabViewView1と同じ大きさで変更 end tell tabView's addTabViewItem:tabViewItem2 -- ViewItem3 set tabViewItem3 to current application's class "NSTabViewItem"'s alloc() tell tabViewItem3 initWithIdentifier_("tab3") setLabel_("ソース") view()'s setFrame:viewItemRect end tell tabView's addTabViewItem:tabViewItem3 -- make new textView (NSScrollView + NSTextView) -- textView1 set theRect1 to current application's NSMakeRect(0, 0, w, h) set scrollview to current application's class "NSScrollView"'s alloc()'s initWithFrame:theRect1 tell scrollview setBorderType_(0) setHasVerticalScroller_(true) setHasHorizontalScroller_(false) setAutoresizingMask_(autoresizingViewFree) end tell set autoresizingViewWidthSizable to ((current application's NSViewWidthSizable) as integer) --天地左右の幅を柔軟に set theRect2 to current application's NSMakeRect(0, 0, w - 10, h) set textView1 to current application's class "NSTextView"'s alloc()'s initWithFrame:theRect2 tell textView1 setMinSize_(current application's NSMakeSize(10, h)) setMaxSize_(current application's NSMakeSize(10000, 10000)) setVerticallyResizable_(true) setHorizontallyResizable_(false) textContainer()'s setContainerSize:(current application's NSMakeSize(w, 10000)) setString_("-未選択-") setAutoresizingMask_(autoresizingViewFree) end tell textView1's textContainer's setWidthTracksTextView:true scrollview's setDocumentView:textView1 tabViewItem1's view()'s addSubview:scrollview -- textView2 set scrollview to current application's class "NSScrollView"'s alloc()'s initWithFrame:theRect1 tell scrollview setBorderType_(0) setHasVerticalScroller_(true) setHasHorizontalScroller_(false) setAutoresizingMask_(autoresizingViewFree) end tell set textView2 to current application's class "NSTextView"'s alloc()'s initWithFrame:theRect2 tell textView2 setMinSize_(current application's NSMakeSize(10, h)) setMaxSize_(current application's NSMakeSize(10000, 10000)) setVerticallyResizable_(true) setHorizontallyResizable_(false) setAutoresizingMask_(autoresizingViewFree) textContainer()'s setContainerSize:(current application's NSMakeSize(w, 10000)) setString_("-項目なし-") end tell textView2's textContainer's setWidthTracksTextView:true scrollview's setDocumentView:textView2 tabViewItem2's view()'s addSubview:scrollview -- textView3 set scrollview to current application's class "NSScrollView"'s alloc()'s initWithFrame:theRect1 tell scrollview setBorderType_(0) setHasVerticalScroller_(true) setHasHorizontalScroller_(false) setAutoresizingMask_(autoresizingViewFree) end tell set textView3 to current application's class "NSTextView"'s alloc()'s initWithFrame:theRect2 tell textView3 setMinSize_(current application's NSMakeSize(10, h)) setMaxSize_(current application's NSMakeSize(10000, 10000)) setVerticallyResizable_(true) setHorizontallyResizable_(false) setAutoresizingMask_(autoresizingViewFree) textContainer()'s setContainerSize:(current application's NSMakeSize(w, 10000)) setString_("-項目なし-") end tell textView3's textContainer's setWidthTracksTextView:true scrollview's setDocumentView:textView3 tabViewItem3's view()'s addSubview:scrollview -- my action1() ----------------------------------------------------------- on error errText log errText end try end makeObject on action1() --ポップアップボタンの内容を更新 if application "Mail" is not running then set popupList to {"(Mailが起動していません。)"} else set popupList to {"-選んでください-"} tell application "Mail" set selectedMail to selection if (count of selectedMail) > 0 then repeat with aMail in selectedMail set subjectText to subject of aMail set popupList to popupList & {subjectText} end repeat else set popupList to {"(選択されていません)"} end if end tell end if tell popupButton1 removeAllItems() addItemsWithTitles_(popupList) end tell end action1 on popupAction1() tell popupButton1 set selectionNo to indexOfSelectedItem() end tell if selectionNo = 0 then return --表示を初期化 textView1's setString:"" textView2's setString:"" textView3's setString:"" --メールの内容を取得 set selectionMail to item selectionNo of selectedMail tell application "Mail" try set mailText to content of selectionMail on error set mailText to "読み取りに失敗しました" end try try set mailHeader to all headers of selectionMail on error set mailHeader to "読み取りに失敗しました" end try try set mailSource to source of selectionMail on error set mailSource to "読み取りに失敗しました" end try end tell --結果を表示 textView1's setString:mailText textView2's setString:mailHeader textView3's setString:mailSource end popupAction1 (* ウインドウバーの閉じるボタンアクションを受けた時 *) on windowShouldClose:sender set closeFlg to true end windowShouldClose: (* ウインドウを閉じるための実際のアクション *) on closeWin:aWindow aWindow's orderOut:me if currentAppName ≠ "Script Editor" then quit me end closeWin: |