Dock Menuを表示するAppleScriptアプリです。探すと意外と情報がまとまっていないので、掲載しておきます。
Dock Menuについては、スクリプトエディタやスクリプトメニュー、アプレットなどで動作する通常のAppleScriptでは利用できませんが、Xcode上で作成するアプリやCocoa AppleScript Applicationでは利用できます。このあたりは、どのぐらいAppleScriptの実行環境がアプリのイベントをAppleScript側に提供しているかどうかによります。
スクリプトエディタ上で作成するアプレットの一種、Cocoa-AppleScript Appletを用いると、Dock Menuを動的に作ることが可能です(後述)。
–> Download Xcode Project Archive
AppleScript名:AppDelegate.applescript |
— — AppDelegate.applescript — dock menu — — Created by Takaaki Naganoya2 on 2025/04/05. — — script AppDelegate property parent : class "NSObject" — IBOutlets property theWindow : missing value property dockMenu : missing value on applicationWillFinishLaunching:aNotification end applicationWillFinishLaunching: on applicationShouldTerminate:sender return current application’s NSTerminateNow end applicationShouldTerminate: –Dock Menu Enabled on applicationDockMenu:(aNotification) return dockMenu end applicationDockMenu: on clicked:aSender set aTag to (tag of aSender) as integer display dialog (aTag as string) end clicked: end script |
Cocoa-AppleScriptアプレット版はこちらです。macOS 12以降では、Finder上でRosettaを利用して実行するように指定する必要があります。
アプレット本体側のscriptではなく、ランタイム側の「CocoaAppletAppDelegate.scpt」を書き換える必要があります。これを読み返すと、つくづく「いきなりこの内容をScripterに使わせようとしたのは無理があった」と感じます。内容が、macOS 10.7当時のScripterのCocoaへの理解度を考えると難解すぎです。
–> Download Cocoa-AppleScript Applet
AppleScript名:CocoaAppletAppDelegate.scpt |
— — CocoaAppletAppDelegate.applescript — Cocoa-AppleScript Applet — — Copyright 2011 {Your Company}. All rights reserved. — — This application delegate emulates the OSA script applet by loading "main.scpt" from the — "Scripts" folder in the application resources and invoking the traditional run/open/reopen/quit — handlers in response to Cocoa application delegate methods being called. — — This is provided in source form so that you may customize or replace it if your needs go — beyond the basic applet handlers. — — Some of these methods must guard against re-entrancy, because invoking the main.scpt — handler may end up invoking the event handler inherited from the current application, — which calls the application delegate’s method again. script CocoaAppletAppDelegate property parent : class "NSObject" property mainScript : missing value — the applet’s main.scpt property didOpenFiles : false — true = the application opened documents during startup property isOpeningFiles : false — re-entrancy guard: true = in the process of opening files property isReopening : false — re-entrancy guard: true = in the process of re-opening property isQuitting : false — re-entrancy guard: true = in the process of quitting on applicationWillFinishLaunching:aNotification — Insert code here to initialize your application before any files are opened — Emulate an OSA Applet: Load the main script from the Scripts resource folder. try set my mainScript to load script (path to resource "main.scpt" in directory "Scripts") on error errMsg number errNum — Perhaps this should silently fail if it can’t load the script; that way, a Cocoa applet — can just have Cocoa classes and no main.scpt. display alert "Could not load main.scpt" message errMsg & " (" & errNum & ")" as critical end try end applicationWillFinishLaunching: on applicationDidFinishLaunching:aNotification — Insert code here to do startup actions after your application has initialized if mainScript is missing value then return — Emulate an OSA Applet: Invoke the "run" handler. — If we have already opened files during startup, don’t invoke the run handler. if didOpenFiles then return try tell mainScript to run on error errMsg number errNum if errNum is not -128 then display alert "An error occurred while running" message errMsg & " (" & errNum & ")" as critical end if end try — TODO: Read the applet’s "stay open" flag and quit if it’s false or unspecified. — For now, all Cocoa Applets stay open and require the run handler to explicitly quit, — which is arguably more correct for a Cocoa application, anyway. (* if not shouldStayOpen then quit end if *) end applicationDidFinishLaunching: on applicationShouldHandleReopen:sender hasVisibleWindows:flag — Insert code here to perform actions in response to a "reopen" event if mainScript is missing value then return true — Guard against re-entrancy. if not isReopening then set isReopening to true — Emulate an OSA Applet: Invoke the "reopen" handler. If there isn’t one, let the application object — handle reopen (this is different from an OSA applet, which would do nothing if there is no handler; — this way, the application will perform the usual "create untitled document" behavior by default). try tell mainScript to reopen set isReopening to false return false on error errMsg number errNum if errNum is not -128 then display alert "An error occurred while reopening" message errMsg & " (" & errNum & ")" as critical end if end try set isReopening to false end if return true end applicationShouldHandleReopen:hasVisibleWindows: on |application|:sender openFiles:filenames — Insert code here to perform actions in response to an "open documents" event — Remember that we opened files, to avoid invoking the "run" handler later. set didOpenFiles to true — Guard against re-entrancy. if not isOpeningFiles and mainScript is not missing value then set isOpeningFiles to true try — Convert all the filenames from NSStrings to script strings set theFilenameStrings to {} repeat with eachFile in filenames set theFilenameStrings to theFilenameStrings & (eachFile as text) end repeat tell mainScript to open theFilenameStrings set isOpeningFiles to false tell sender to replyToOpenOrPrint:(current application’s NSApplicationDelegateReplySuccess) on error errMsg number errNum if errNum = -128 then tell sender to replyToOpenOrPrint:(current application’s NSApplicationDelegateReplyCancel) else display alert "An error occurred while opening file(s)" message errMsg & " (" & errNum & ")" as critical tell sender to replyToOpenOrPrint:(current application’s NSApplicationDelegateReplyFailure) end if end try set isOpeningFiles to false else tell sender to replyToOpenOrPrint:(current application’s NSApplicationDelegateReplyFailure) end if end |application|:openFiles: on applicationShouldTerminate:sender — Insert code here to do any housekeeping before your application quits — Guard against re-entrancy. if not isQuitting and mainScript is not missing value then set isQuitting to true — Emulate an OSA Applet: Invoke the "quit" handler; if the handler returns, it has fully — handled the quit message and we should not quit, otherwise, it calls "continue quit", — which returns error -10000. try tell mainScript to quit set isQuitting to false return current application’s NSTerminateCancel on error errMsg number errNum — -128 means there is no quit handler — -10000 means the handler did "continue quit" if errNum is not -128 and errNum is not -10000 then display alert "An error occurred while quitting" message errMsg & " (" & errNum & ")" as critical end if end try set isQuitting to false end if return current application’s NSTerminateNow end applicationShouldTerminate: –Dock Menu Enabled on applicationDockMenu:(aNotification) set aMenu to current application’s NSMenu’s alloc()’s init() set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:"Dock Menuだよ" action:"actionHandler:" keyEquivalent:"") (aMenuItem’s setTarget:me) (aMenu’s addItem:aMenuItem) return aMenu end applicationDockMenu: on actionHandler:sender set aTag to (tag of sender) as string set aTitle to (title of sender) as string display dialog (aTitle as string) end actionHandler: end script |
More from my site
(Visited 9 times, 1 visits today)