Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

カテゴリー: Noification

日付変更通知

Posted on 11月 12 by Takaaki Naganoya

日付の変更を通知するAppleScriptです。

この手のScriptを組もうとしたら、一定間隔で日付(current date)を確認する ぐらいが安全な方法として考えられます。on idleハンドラを利用して、一定間隔でタイマ割り込みを利用するのが「定番」でしょう。ただ、そのためにはApplet形式で書き出して実行する必要があります。

最近のAppleScript実行環境は、ごく一部の例外をのぞいてCocoaの機能を呼び出せるので、Cocoaの機能でタイマー割り込みを行って、一定間隔で日付データを確認することもできます。こちらは、。一度実行すればメモリ上にオブジェクトが常駐するので、Script Editorの実行が停止していても、起動さえしていれば日付変更時に通知を受信できます。

タイマー割り込み以外の方法を調べてみたら、こんなSystem Notificationが利用できたので、まとめておきました。

本Scriptが機能するのは、「AppleScript最新リファレンス v2.8対応」で明記した「システム通知の受信が有効なAppleScript実行環境」のみです。

AppleScript名:日付変更通知.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2023/11/12
—
–  Copyright © 2023 Piyomaru Software, All Rights Reserved
—

use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set dOvs to current application’s NSNotificationCenter’s defaultCenter()
dOvs’s addObserver:me selector:"dateChanged:" |name|:(current application’s NSCalendarDayChangedNotification) object:(missing value)

on dateChanged:anObj
  display dialog "Update"
end dateChanged:

★Click Here to Open This Script 

Posted in Noification | Tagged 12.0savvy 13.0savvy 14.0savvy | Leave a comment

NSApplicationにアクセス

Posted on 9月 21, 2019 by Takaaki Naganoya

NSApplicationにアクセスして実行環境の各種情報を取得するAppleScriptです。

プロセス関連のCocoaオブジェクトは何種類かありますが、

NSApplicationは実行環境そのもの、実行中のアプリケーションの内部情報を取得するオブジェクトのようです。他のアプリケーションをBundle IDで指定してNSApplicationを取得するような処理ができるのかと思って調べていたのですが、どーもできないっぽい。

AppleScriptからSystem Events経由でアクセスするいつものやり方で、取得できない要素はとくにないのですが、一長一短というか得意不得意があるという感じです。

AppleScript名:NSApplicationにアクセス.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/08/04
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set anApp to current application’s NSApplication’s sharedApplication()
–> <ScriptDebuggerApplication: 0x600002562d00>

anApp’s |running| as boolean
–> true

anApp’s enabledRemoteNotificationTypes()
–> 0

anApp’s mainMenu()
–>
(*
<NSMenu: 0x600001878240>
  Title: AMainMenu
  Open bounds: [t=1440, l=0, b=1440, r=0]
  Supermenu: 0x0 (None), autoenable: YES
  Items: (
"<NSMenuItem: 0x6000029294a0 Script Debugger, submenu: 0x6000018780c0 (Script Debugger)>",
"<NSMenuItem: 0x600002929aa0 File, submenu: 0x6000018789c0 (File)>",
"<NSMenuItem: 0x60000292ae20 Edit, submenu: 0x600001878a80 (Edit)>",
"<NSMenuItem: 0x60000291c240 View, submenu: 0x600001878c40 (View)>",
"<NSMenuItem: 0x60000291cfc0 Search, submenu: 0x600001878e80 (Search)>",
"<NSMenuItem: 0x60000291d5c0 Script, submenu: 0x600001878f00 (Script)>",
"<NSMenuItem: 0x60000291e700 Dictionary, submenu: 0x600001879040 (Dictionary)>",
"<NSMenuItem: 0x60000291ef40 Window, submenu: 0x600001879080 (Window)>",
"<NSMenuItem: 0x6000029285a0 Clippings, submenu: 0x600001878840 (Clippings)>",
"<NSMenuItem: 0x60000291f540 Scripts, submenu: 0x6000018791c0 (Scripts)>",
"<NSMenuItem: 0x60000291fc00 Help, submenu: 0x600001879340 (Help)>"
)
*)

anApp’s servicesMenu()
(*
<NSMenu: 0x600001878340>
  Title: Services
  Open bounds: [t=nan, l=6.95319e-310, b=nan, r=6.95319e-310]
  Supermenu: 0x6000018780c0 (Script Debugger), autoenable: YES
  Items: (
"<NSMenuItem: 0x60000e924cc0 Add to Wunderlist>",
"<NSMenuItem: 0x60000e927120 ATOK\U30a4\U30df\U30af\U30eb\U3067\U691c\U7d22>",
"<NSMenuItem: 0x60000e926f40 Evernote \U306b\U8ffd\U52a0>",
"<NSMenuItem: 0x60000e926a60 Finder\U3067\U60c5\U5831\U3092\U898b\U308b>",
"<NSMenuItem: 0x60000e927e40 Finder\U306b\U8868\U793a>",
"<NSMenuItem: 0x60000e9275a0 \U958b\U304f>",
"<NSMenuItem: 0x60000e9279c0 man\U30da\U30fc\U30b8\U3092\U30bf\U30fc\U30df\U30ca\U30eb\U3067\U958b\U304f>",
"<NSMenuItem: 0x60000e9246c0 \U30bf\U30fc\U30df\U30ca\U30eb\U306eman\U30da\U30fc\U30b8\U30a4\U30f3\U30c7\U30c3\U30af\U30b9\U3067\U691c\U7d22>",
"<NSMenuItem: 0x60000e924c00 \U30b9\U30c6\U30a3\U30c3\U30ad\U30fc\U30e1\U30e2\U3092\U4f5c\U6210>",
"<NSMenuItem: 0x60000e9249c0 \U30b9\U30dd\U30fc\U30af\U30f3\U30c8\U30e9\U30c3\U30af\U3068\U3057\U3066iTunes\U306b\U8ffd\U52a0>",
"<NSMenuItem: 0x60000e9244e0 \U30c6\U30ad\U30b9\U30c8\U3092\U7c21\U4f53\U5b57\U4e2d\U56fd\U8a9e\U306b\U5909\U63db>",
"<NSMenuItem: 0x60000e927a20 \U30c6\U30ad\U30b9\U30c8\U3092\U5168\U89d2\U306b\U5909\U63db>",
"<NSMenuItem: 0x60000e9276c0 \U30c6\U30ad\U30b9\U30c8\U3092\U534a\U89d2\U306b\U5909\U63db>",
"<NSMenuItem: 0x60000e9247e0 \U30c6\U30ad\U30b9\U30c8\U3092\U7e41\U4f53\U5b57\U4e2d\U56fd\U8a9e\U306b\U5909\U63db>",
"<NSMenuItem: 0x60000e925440 \U30de\U30c3\U30d7\U3092\U8868\U793a>",
"<NSMenuItem: 0x60000e926040 \U30a4\U30e1\U30fc\U30b8\U3092\U8aad\U307f\U8fbc\U3080>",
"<NSMenuItem: 0x60000e925ec0 \U30c7\U30b9\U30af\U30c8\U30c3\U30d7\U30d4\U30af\U30c1\U30e3\U3092\U8a2d\U5b9a>",
"<NSMenuItem: 0x60000e926c40 Skim\U3067URL\U3092\U958b\U304f>",
"<NSMenuItem: 0x60000e9266a0 Skim\U3067\U30d5\U30a1\U30a4\U30eb\U3092\U9<<description truncated at 2000 characters out of 4221>>"
*)

anApp’s mainWindow()
–> <ScriptWindow: 0x7ffd73e344c0>

anApp’s keyWindow()
–> <ScriptWindow: 0x7ffd73e344c0>

anApp’s |windows|()
(*
(NSArray) {
  <NSPanel: 0x600003139500>,
  <NSWindow: 0x60000312e500>,
  <MiniDebuggerWindow: 0x7ffd73e0b870>,
  <LNSFindWrapAroundWindow: 0x60000312b000>,
  <NSToolTipPanel: 0x7ffd73dee5e0>,
  <NSPanel: 0x600003104800>,
  <NSPanel: 0x600003104700>,
  <NSWindow: 0x60000315c800>,
  <NSColorPanel: 0x7ffce4aa99c0>,
  <NSWindow: 0x600003121d00>,
  <ScriptWindow: 0x7ffd73e344c0>,
  <NSComboBoxWindow: 0x7ffce4f27700>
}
*)

anApp’s dockTile()
–> <NSDockTile: 0x60000221db00>

anApp’s dockTile()’s |size|()
–> {width:128.0, height:128.0}

anApp’s dockTile()’s owner()
–> <ScriptDebuggerApplication: 0x600003f38480>

anApp’s dockTile()’s showsApplicationBadge()
–> false

anApp’s dockTile()’s badgeLabel()

anApp’s applicationIconImage()
–> <NSImage 0x60000573be40 Size={128, 128} Reps=( ……

anApp’s acceptsFirstResponder()
–> false

anApp’s becomeFirstResponder()
–> true

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.12savvy 10.13savvy 10.14savvy NSApplication | Leave a comment

アラートダイアログ上にpath control x2を表示 v2

Posted on 8月 3, 2019 by Takaaki Naganoya

アラートダイアログ上にPathcontrolを表示して、ファイルパス選択をドラッグ&ドロップで受け付けるAppleScriptの改良版です。

実行中にアピアランスのDark Mode/Light Modeへの変更が行われたときの対応を追加してみました。

実行途中でアピアランスの変更が行われると、OSが提供している部品をそのまま利用している分にはOSの管理下で色変更が行われますが、あとから色指定している部品はそのままです(↑)。上記の(↑)ように、割と違和感があるというか、実用上困る(そもそもアピアランス変更をそんな時に行う方がどうかしているとは思うのですが)ので、対処してみました。

部品はひととおり作ってあったので、組み合わせただけです。アピアランスの変更Notificationを受信する部品や、アピアランステーマを判定する部品などです。

部品の組み合わせ方にも些細なノウハウがあるので、一応やっておいたことには意義があると思っています。

AppleScript名:アラートダイアログ上にpath control x2を表示 v2
— Created 2019-08-02 by Takaaki Naganoya
— 2019 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSView : a reference to current application’s NSView
property NSAlert : a reference to current application’s NSAlert
property NSColor : a reference to current application’s NSColor
property NSTextField : a reference to current application’s NSTextField
property NSPathControl : a reference to current application’s NSPathControl
property NSUserDefaults : a reference to current application’s NSUserDefaults
property NSRunningApplication : a reference to current application’s NSRunningApplication
property NSDistributedNotificationCenter : a reference to current application’s NSDistributedNotificationCenter

property theResult : 0
property returnCode : 0
property resList : {}
property aPathControl : missing value
property bPathControl : missing value

set paramObj to {myMessage:"ファイルの入出力フォルダ選択", mySubMessage:"どれか選択してください。", fromPathMes:"移動元:", toPathMes:"移動先:"}
my performSelectorOnMainThread:"chooseTwoPath:" withObject:paramObj waitUntilDone:true

return resList
–> {fromPathRes:"/Users/me/Desktop/keyn1.png", toPathRes:"/Users/me/Desktop/scriptmenu1.png"}

on chooseTwoPath:paramObj
  set aMainMes to myMessage of paramObj
  
set aSubMes to mySubMessage of paramObj
  
set fromLabel to fromPathMes of paramObj
  
set toLabel to toPathMes of paramObj
  
  
—Dark Mode Notification受信開始
  
NSDistributedNotificationCenter’s defaultCenter()’s addObserver:me selector:"darkModeChanged:" |name|:"AppleInterfaceThemeChangedNotification" object:(missing value)
  
  
— create a view
  
set theView to NSView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 600, 60))
  
  
— create two path control & text field
  
set aPathControl to NSPathControl’s alloc()’s initWithFrame:(current application’s NSMakeRect(100, 35, 700, 20))
  
set bPathControl to NSPathControl’s alloc()’s initWithFrame:(current application’s NSMakeRect(100, 0, 700, 20))
  
  
–Set path control’s bg colors
  
set {aCol, bCol} to pathControlSrtringColor() of me
  
aPathControl’s setBackgroundColor:(aCol)
  
bPathControl’s setBackgroundColor:(bCol)
  
  
  
set aHome to current application’s |NSURL|’s fileURLWithPath:(current application’s NSHomeDirectory()) –initial dir
  
aPathControl’s setURL:aHome
  
bPathControl’s setURL:aHome
  
  
set a1TF to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 35, 100, 20))
  
set a2TF to NSTextField’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, 100, 20))
  
a1TF’s setEditable:false
  
a2TF’s setEditable:false
  
a1TF’s setStringValue:fromLabel
  
a2TF’s setStringValue:toLabel
  
a1TF’s setDrawsBackground:false
  
a2TF’s setDrawsBackground:false
  
a1TF’s setBordered:false
  
a2TF’s setBordered:false
  
  
theView’s setSubviews:{a1TF, aPathControl, a2TF, bPathControl}
  
  
— set up alert
  
set theAlert to NSAlert’s alloc()’s init()
  
tell theAlert
    its setMessageText:aMainMes
    
its setInformativeText:aSubMes
    
its addButtonWithTitle:"OK"
    
its addButtonWithTitle:"Cancel"
    
its setAccessoryView:theView
  end tell
  
  
— show alert in modal loop
  
NSRunningApplication’s currentApplication()’s activateWithOptions:0
  
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
  
if (my returnCode as number) = 1001 then error number -128
  
  
set s1Val to (aPathControl’s |URL|’s |path|()) as string
  
set s2Val to (bPathControl’s |URL|’s |path|()) as string
  
  
set resList to {fromPathRes:s1Val, toPathRes:s2Val}
  
  
–Notification受信停止
  
NSDistributedNotificationCenter’s defaultCenter()’s removeObserver:me |name|:"AppleInterfaceThemeChangedNotification" object:(missing value)
end chooseTwoPath:

on doModal:aParam
  set (my returnCode) to aParam’s runModal()
end doModal:

–ダークモードの判定。ダークモード時:true、ライトモード時:falseが返る
on retLIghtOrDark()
  set curMode to (NSUserDefaults’s standardUserDefaults()’s stringForKey:"AppleInterfaceStyle") as string
  
return (curMode = "Dark") as boolean
end retLIghtOrDark

–aMaxValを最大値とする数値でNSColorを作成して返す
on makeNSColorFromRGBAval(redValue as integer, greenValue as integer, blueValue as integer, alphaValue as integer, aMaxVal as integer)
  set aRedCocoa to (redValue / aMaxVal) as real
  
set aGreenCocoa to (greenValue / aMaxVal) as real
  
set aBlueCocoa to (blueValue / aMaxVal) as real
  
set aAlphaCocoa to (alphaValue / aMaxVal) as real
  
set aColor to NSColor’s colorWithCalibratedRed:aRedCocoa green:aGreenCocoa blue:aBlueCocoa alpha:aAlphaCocoa
  
return aColor
end makeNSColorFromRGBAval

–Notification handler
on darkModeChanged:(aNotification)
  set {aCol, bCol} to pathControlSrtringColor() of me
  
aPathControl’s setBackgroundColor:(aCol)
  
bPathControl’s setBackgroundColor:(bCol)
end darkModeChanged:

on pathControlSrtringColor()
  set dMode to retLIghtOrDark() of me –Dark mode:true
  
if dMode = false then
    –Light Mode
    
set aCol to (NSColor’s cyanColor())
    
set bCol to (NSColor’s yellowColor())
  else
    –Dark Mode
    
set aCol to (makeNSColorFromRGBAval(0, 96, 65, 255, 255) of me)
    
set bCol to (makeNSColorFromRGBAval(93, 92, 0, 255, 255) of me)
  end if
  
  
return {aCol, bCol}
end pathControlSrtringColor

★Click Here to Open This Script 

Posted in Color dialog GUI Noification | Tagged 10.12savvy 10.13savvy 10.14savvy 10.15savvy NSAlert NSColor NSDistributedNotificationCenter NSPathControl NSRunningApplication NSTextField NSUserDefaults NSView | Leave a comment

スリープおよびスリープ解除Notificationを受信

Posted on 1月 27, 2019 by Takaaki Naganoya

Macがスリープ/スリープから復帰したノーティフィケーションを受信するAppleScriptです。

Cocoaの機能が利用できない時代にスリープ解除を検出するアプリケーション「Okaeri」をAppleScriptで作って動かしていましたが、あれはon idleハンドラで1秒ごとにタイマー割り込みを発生させ、前回割り込み発生時間を記録、前回割り込みと現在時刻の差分を計算し、1ないし数秒以上間隔が空いていたらスリープ解除が発生したとみなして、syslogにスリープ解除が記録されていないかをチェックするようにしていました。

なので、Cocoaの機能呼び出しやNotificationの受信を行えなくても、ある程度は同様の処理が行えるわけですが、使えれば使えたで便利ではあります。

AppleScript名:スリープおよびスリープ解除Notificationを受信.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/01/25
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set aCenter to current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()

aCenter’s addObserver:me selector:"notifSleep:" |name|:"NSWorkspaceWillSleepNotification" object:(missing value)

aCenter’s addObserver:me selector:"notifWaked:" |name|:"NSWorkspaceDidWakeNotification" object:(missing value)

on notifSleep:aNotif
  display notification "System will sleep"
  
say "System will sleep"
end notifSleep:

on notifWaked:aNotif
  display notification "System did wake"
  
say "System did wake"
end notifWaked:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSWorkspace | 2 Comments

ディスプレイのスリープ/復帰Notificationを受信

Posted on 1月 26, 2019 by Takaaki Naganoya

Macに接続・搭載しているディスプレイのスリープ/スリープ復帰が行われたノーティフィケーションを受信するAppleScriptです。

AppleScript名:ディスプレイがスリープした.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/01/25
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set aCenter to current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()

aCenter’s addObserver:me selector:"notifDispSleeped:" |name|:"NSWorkspaceScreensDidSleepNotification" object:(missing value)

on notifDispSleeped:aNotif
  say "Display sleeped"
end notifDispSleeped:

★Click Here to Open This Script 

AppleScript名:ディスプレイがスリープから復帰した.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/01/25
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set aCenter to current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()

aCenter’s addObserver:me selector:"notifDispWaked:" |name|:"NSWorkspaceScreensDidWakeNotification" object:(missing value)


on notifDispWaked:aNotif
  say "Display waked"
end notifDispWaked:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSWorkspace | Leave a comment

ボリウムの名称変更Notificationを受信

Posted on 1月 26, 2019 by Takaaki Naganoya

Macに接続・搭載しているドライブのボリウム名変更が行われたノーティフィケーションを受信するAppleScriptです。

ドライブ名を変更してみると、なぜか通知が2回飛んでくるところが謎です。
→ システム再起動後に追試してみましたが、通知が飛んでくるのは1回だけでした。Scriptを複数回実行してしまったのが原因だと思われます。

AppleScript名:ボリウムの名称変更_notification.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/01/25
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

–ドライブ名の変更時にnotify(なぜか2回通知される?)
set aCenter to current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()

aCenter’s addObserver:me selector:"driveNameChanged:" |name|:"NSWorkspaceDidRenameVolumeNotification" object:(missing value)

on driveNameChanged:aNotif
  say "Drive name changed"
end driveNameChanged:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSWorkspace | Leave a comment

Spaces変更Notificationを受信

Posted on 1月 26, 2019 by Takaaki Naganoya

Spacesのスペース変更が行われた(異なるスペースに変更しなくてもOK)ノーティフィケーションを受信するAppleScriptです。

ちなみに、AppleScriptによる処理を行う分にはSpacesは百害あって一利もないので、複数Spaceを登録「しないで」使うことが推奨されます。

AppleScript名:Spaces変更_notification.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/01/25
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set aCenter to current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()

aCenter’s addObserver:me selector:"spacesChanged:" |name|:"NSWorkspaceActiveSpaceDidChangeNotification" object:(missing value)

on spacesChanged:aNotif
  say "Active Spaces changed"
end spacesChanged:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSWorkspace | 1 Comment

コンピュータのシャットダウン/再起動イベントを受信

Posted on 1月 25, 2019 by Takaaki Naganoya

実行中のMacのシャットダウン/再起動イベントを受信するAppleScrptです。

Classic Mac OS時代のシステム終了項目のような処理をAppleScriptで行いたい場合に、本Scriptのような処理を常駐型アプレット形式で書き出しておいて、起動させたままにしておけば、システム終了/再起動のイベントを受信してハンドラを実行します。

スクリプトエディタ上で実行しても、イベントを受信します、一応、ねんのため。

→ Download this script archive

AppleScript名:シャットダウン_notification.scptd
—
–  Created by: Takaaki Naganoya
–  Created on: 2019/01/25
—
–  Copyright © 2019 Piyomaru Software, All Rights Reserved
—
use AppleScript version "2.4" — Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set aCenter to current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()
aCenter’s addObserver:me selector:"computerWillShutDownNotification:" |name|:"NSWorkspaceWillPowerOffNotification" object:(missing value)

on computerWillShutDownNotification:aNotif
  say "Computer Will shutdown or restart now"
end computerWillShutDownNotification:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy 10.14savvy NSWorkspace | Leave a comment

Dark ModeのNotificationを受信する

Posted on 5月 1, 2018 by Takaaki Naganoya

システム環境設定(System Preferences)の「一般」で「メニューバーとDockを暗くする」(Dark Mode)の設定を変更した際のNotificationを受信して、現在のModeを検出するAppleScriptです。

–> Watch Demo Movie

ただ、ステータスバーに画像アイコンを表示して、Dark Mode/Light Modeに応じてアイコン画像を切り替えるだけなら、別にNotificationを受信して画像を差し替えるようなことをしなくても、

set aBar to current application’s NSStatusBar’s systemStatusBar()’s statusItemWithLength:(current application’s NSVariableStatusItemLength)
aBar’s setTitle:"TEST MENU"
aBar’s setMenu:statMenu
set anImage to (current application’s NSImage’s imageNamed:"statusBarIconImage")
anImage’s setTemplate:true –これだけでDark Modeの切り替えに自動対応
aBar’s setImage:anImage

★Click Here to Open This Script 

ぐらいで対応できます(Xcode上のAppleScript Project)。

AppleScript名:Dark ModeのNotificationを受信する
— Created 2018-05-01 by Takaaki Naganoya
— 2018 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

–Save This Script as "stay open applet" to try.

on run
  current application’s NSDistributedNotificationCenter’s defaultCenter()’s addObserver:me selector:"darkModeChanged:" |name|:"AppleInterfaceThemeChangedNotification" object:(missing value)
end run

on quit
  current application’s NSDistributedNotificationCenter’s defaultCenter()’s removeObserver:me |name|:"AppleInterfaceThemeChangedNotification" object:(missing value)
  
continue quit –超重要
end quit

on darkModeChanged:(aNotification)
  set curMode to (my currentDarkMode:"test") as string
  
display notification curMode
end darkModeChanged:

on currentDarkMode:(aNotification)
  tell application "System Events"
    tell appearance preferences
      set curMode to (dark mode)
    end tell
    
    
if (curMode as boolean) = true then
      return "Dark"
    else
      return "Light"
    end if
  end tell
end currentDarkMode:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

ドライブマウント検出

Posted on 2月 20, 2018 by Takaaki Naganoya

ドライブのマウントを検出してsayコマンドで通知するAppleScriptです。

AppleScript名:ドライブマウント検出
— Created 2016-10-27 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

on run
  current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()’s addObserver:me selector:"onVolumeMount:" |name|:"NSWorkspaceDidMountNotification" object:(missing value)
  
  
current application’s NSWorkspace’s sharedWorkspace()’s notificationCenter()’s addObserver:me selector:"onVolumeUnmount:" |name|:"NSWorkspaceDidUnmountNotification" object:(missing value)
end run

on onVolumeMount:aNotif
  say "マウントしました" using "Kyoko" –Mount Message In Japanese
end onVolumeMount:

on onVolumeUnmount:aNotif
  say "アンマウントしました" using "Kyoko" –Unmount Message In Japanese
end onVolumeUnmount:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Notification

Posted on 2月 8, 2018 by Takaaki Naganoya

macOS 10.10より、標準のScripting環境(スクリプトエディタ)上でCocoa-bridgeが使えるようになったので、Cocoa系のevent(Notification)が使えるようになったことを利用したサンプルです。

AppleScriptは基本的に、アプリケーションの機能や設定値を呼び出すためのもので、ごく一部の限られたアプリケーション以外からは「アプリケーション側からAppleScript(の所定のハンドラ)を呼び出す」方向のイベント呼び出しは利用できませんでした。

しかし、Cocoaのnotificationが利用できるようになったことで、多彩なイベント受信が行えるようになりました。

iTunes(or Music.app)の再生曲変更のnotificationを受信してやたらとTwitterなどに流そうとする入門Scripterがいるのですが(どこかの伝統芸か何かなんだろうか?)、そこはそんなに楽しいとも思えないし、本来のパワーを発揮していないとは思うのですが、見かけたらそっと触れないようにしておくことにしています。

AppleScript名:Notification
— Created 2017-10-13 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property NSDistributedNotificationCenter : a reference to current application’s NSDistributedNotificationCenter

on run
  NSDistributedNotificationCenter’s defaultCenter()’s addObserver:me selector:"statusChanged:" |name|:"com.apple.iTunes.playerInfo" object:(missing value)
  
end run

on statusChanged:sender
  tell application id "com.apple.iTunes"
    try
      tell current track
        set anAlbum to album
        
set aName to name
        
set anArtist to album artist
        
set aRating to rating
      end tell
    on error
      return
    end try
  end tell
  
display notification aName
end statusChanged:

★Click Here to Open This Script 

Posted in Noification System | Tagged 10.11savvy 10.12savvy 10.13savvy iTunes Music | Leave a comment

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 13, Ventura(継続更新)
  • アラートダイアログ上にWebViewで3Dコンテンツを表示(WebGL+three.js)v3
  • Xcode 14.2でAppleScript App Templateを復活させる
  • macOS 13 TTS Voice環境に変更
  • UI Browserがgithub上でソース公開され、オープンソースに
  • 2022年に書いた価値あるAppleScript
  • ChatGPTで文章のベクトル化(Embedding)
  • iWork 12.2がリリースされた
  • ChatGPTでchatに対する応答文を取得
  • macOS 13対応アップデート:AppleScript実践的テクニック集(1)GUI Scripting
  • 新発売:CotEditor Scripting Book with AppleScript
  • macOS 14, Sonoma
  • macOS 13でNSNotFoundバグふたたび
  • Finderの隠し命令openVirtualLocationが発見される
  • macOS 13.1アップデートでスクリプトエディタの挙動がようやくまともに
  • あのコン過去ログビューワー(暫定版)
  • Dockアイコンにプログレスバーを追加 v3
  • UI BrowserがLate Night Software, ltdによって継続サポートされることが発表される
  • クリップボードに入った書式つきテキストをプレーン化する「PlainerText」
  • CotEditor v4.4.1+macOS 13の組み合わせで発生している問題

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1390) 10.14savvy (586) 10.15savvy (434) 11.0savvy (278) 12.0savvy (190) 13.0savvy (67) CotEditor (61) Finder (48) iTunes (19) Keynote (99) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (41) NSJSONSerialization (21) NSMutableArray (62) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (57) Pages (38) Safari (41) Script Editor (21) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC