commit 1eca425c5f453bc0ed47f780d491656549d3ab53 Author: relikd Date: Tue Jan 27 16:07:35 2026 +0100 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e1b3d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +/*.app +/*.tar.gz +*.xcodeproj diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..edc2ee6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2026 relikd + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ab74d4d --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +# usage: make [CONFIG=debug|release] + +ifeq ($(CONFIG), debug) + CFLAGS=-Onone -g +else + CFLAGS=-O +endif + +PLIST=$(shell grep -A1 $(1) src/Info.plist | tail -1 | cut -d'>' -f2 | cut -d'<' -f1) +HAS_SIGN_IDENTITY=$(shell security find-identity -v -p codesigning | grep -q "Apple Development" && echo 1 || echo 0) + + +Menuscript.app: SDK_PATH=$(shell xcrun --show-sdk-path --sdk macosx) +Menuscript.app: src/* examples/* + @mkdir -p Menuscript.app/Contents/MacOS/ + swiftc ${CFLAGS} src/main.swift -target x86_64-apple-macos10.13 \ + -emit-executable -sdk ${SDK_PATH} -o bin_x64 + swiftc ${CFLAGS} src/main.swift -target arm64-apple-macos10.13 \ + -emit-executable -sdk ${SDK_PATH} -o bin_arm64 + lipo -create bin_x64 bin_arm64 -o Menuscript.app/Contents/MacOS/Menuscript + @rm bin_x64 bin_arm64 + @echo 'APPL????' > Menuscript.app/Contents/PkgInfo + @mkdir -p Menuscript.app/Contents/Resources/ + @cp src/AppIcon.icns Menuscript.app/Contents/Resources/AppIcon.icns + @rm -rf Menuscript.app/Contents/Resources/examples/ + @cp -R examples/ Menuscript.app/Contents/Resources/examples/ + @cp src/Info.plist Menuscript.app/Contents/Info.plist + @touch Menuscript.app + @echo +ifeq ($(HAS_SIGN_IDENTITY),1) + codesign -v -s 'Apple Development' --options=runtime --timestamp Menuscript.app +else + codesign -v -s - Menuscript.app +endif + @echo + @echo 'Verify Signature...' + @echo + codesign -dvv Menuscript.app + @echo + codesign -vvv --deep --strict Menuscript.app +ifeq ($(HAS_SIGN_IDENTITY),1) + @echo + -spctl -vvv --assess --type exec Menuscript.app +endif + + +.PHONY: clean +clean: + rm -rf Menuscript.app bin_x64 bin_arm64 + + +.PHONY: release +release: VERSION=$(call PLIST,CFBundleShortVersionString) +release: Menuscript.app + tar -czf "Menuscript_v${VERSION}.tar.gz" Menuscript.app diff --git a/README.md b/README.md new file mode 100644 index 0000000..5caf756 --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +[![macOS 10.13+](https://img.shields.io/badge/macOS-10.13+-888)](#install) +[![Current release](https://img.shields.io/github/release/relikd/Menuscript)](https://github.com/relikd/Menuscript/releases) +[![All downloads](https://img.shields.io/github/downloads/relikd/Menuscript/total)](https://github.com/relikd/Menuscript/releases) + + + +# Menuscript + +A menu bar script executor. + + + +Menuscript adds a status bar menu to call custom (user defined) scripts. +The app reads the content of a directory and adds all executable files to the status menu. +The screenshot above represents the content of the [examples](examples/) directory. + + +## Usage + +1) Define your own script directory in Preferences. +2) Add subdirectories and scripts to your scripts dir. +3) Run a script from your status menu. +4) Depending on your script action, you may want to allow `Full Disk Access` for Menuscript. + +*Note:* Menuscript reloads the directory structure each time you open the status menu, no need to restart the app. + + +### Requirements + +Script files are called as is. +Therefore each script __must__ have the executable flag (`chmod 755` or `chmod +x`) and __must__ execute itself on double-click. +The latter can be achieved by adding a shebang (e.g., `#!/bin/sh`) to the script file. +You may want to omit the file extension (in case of Python, that would launch the editor instead of executing the script). + +Apart from that, there is no limitation on the script language. +You can use Bash, Python, Swift, Ruby, whatever. +And of course, you can always write a script wrapper to call something else. + +=> If you can call the script with `open` (e.g., `open myscript`), it will work in the status menu too. + + +### Configuration + +There are a few ways to modify the menu structure: + +#### Sort Order + +By default, menu items are sorted in alphabetic order (case-insensitive). +You can change the item order by prepending numbers to the filename. +It does not matter whether you prepend your files with `42` or `42.` or `42 - `, as long as the first character is a number. + +You dont need to rename all items either. +Each item has a default numerical order of `100`. +By prepending a number lower or higher than 100, you can place items at the top or bottom of the menu respectively. + +#### Modifier Flags + +Modifier flags change what happens if you click on the menu item. +Flags are defined by adding a text snippet to the filename. +These constant strings are defined: + +- __[txt]__: Execute the script and dump all output in a new `TextEdit` window (useful for reports or log files, etc.) +- __[verbose]__: Usually, script files are executed in the background. With this flag, a new `Terminal` window will open and show the activley running script (useful for continuous output like `top` or `netstat -w`, etc.) + +#### Menu Icon + +A subdirectory can have a custom icon if the folder contains an image file named `icon.X` (where `X` is one of: `svg`, `png`, `jpg`, `jpeg`, `gif`, `ico`). diff --git a/examples/Custom Sort Order/1. One b/examples/Custom Sort Order/1. One new file mode 100755 index 0000000..c3c7a92 --- /dev/null +++ b/examples/Custom Sort Order/1. One @@ -0,0 +1,2 @@ +#!/bin/sh +say 1 diff --git a/examples/Custom Sort Order/2. Two b/examples/Custom Sort Order/2. Two new file mode 100755 index 0000000..2370dc1 --- /dev/null +++ b/examples/Custom Sort Order/2. Two @@ -0,0 +1,2 @@ +#!/bin/sh +say 2 diff --git a/examples/Custom Sort Order/3. Three b/examples/Custom Sort Order/3. Three new file mode 100755 index 0000000..c77c076 --- /dev/null +++ b/examples/Custom Sort Order/3. Three @@ -0,0 +1,2 @@ +#!/bin/sh +say 3 diff --git a/examples/Custom Sort Order/4. Four b/examples/Custom Sort Order/4. Four new file mode 100755 index 0000000..5d0b4ee --- /dev/null +++ b/examples/Custom Sort Order/4. Four @@ -0,0 +1,2 @@ +#!/bin/sh +say 4 diff --git a/examples/Custom Sort Order/9 Nine b/examples/Custom Sort Order/9 Nine new file mode 100755 index 0000000..2504f3f --- /dev/null +++ b/examples/Custom Sort Order/9 Nine @@ -0,0 +1,2 @@ +#!/bin/sh +say 9 diff --git a/examples/Custom Sort Order/999 Zero b/examples/Custom Sort Order/999 Zero new file mode 100755 index 0000000..c049e92 --- /dev/null +++ b/examples/Custom Sort Order/999 Zero @@ -0,0 +1,2 @@ +#!/bin/sh +say 0 diff --git a/examples/Custom Sort Order/Five b/examples/Custom Sort Order/Five new file mode 100755 index 0000000..55841a7 --- /dev/null +++ b/examples/Custom Sort Order/Five @@ -0,0 +1,2 @@ +#!/bin/sh +say 5 diff --git a/examples/Flags/999 Python example [verbose] b/examples/Flags/999 Python example [verbose] new file mode 100755 index 0000000..0ca94ad --- /dev/null +++ b/examples/Flags/999 Python example [verbose] @@ -0,0 +1,4 @@ +#!/usr/bin/env python3 +print() +print("=> 2^8 is", 2**8) +print() diff --git a/examples/Flags/icon.svg b/examples/Flags/icon.svg new file mode 100644 index 0000000..8eb51d4 --- /dev/null +++ b/examples/Flags/icon.svg @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/examples/Flags/ifconfig (text editor) [txt] b/examples/Flags/ifconfig (text editor) [txt] new file mode 100755 index 0000000..e541c14 --- /dev/null +++ b/examples/Flags/ifconfig (text editor) [txt] @@ -0,0 +1,2 @@ +#!/bin/sh +ifconfig diff --git a/examples/Flags/top (updated) [verbose] b/examples/Flags/top (updated) [verbose] new file mode 100755 index 0000000..6ceae65 --- /dev/null +++ b/examples/Flags/top (updated) [verbose] @@ -0,0 +1,2 @@ +#!/bin/sh +top diff --git a/examples/Open Desktop Folder b/examples/Open Desktop Folder new file mode 100755 index 0000000..a3a7d04 --- /dev/null +++ b/examples/Open Desktop Folder @@ -0,0 +1,2 @@ +#!/bin/sh +open ~/Desktop diff --git a/examples/Toggle Desktop Icons b/examples/Toggle Desktop Icons new file mode 100755 index 0000000..bebd9b2 --- /dev/null +++ b/examples/Toggle Desktop Icons @@ -0,0 +1,8 @@ +#!/bin/sh +prev=$(defaults read com.apple.finder CreateDesktop) +if [ "$prev" = 0 ]; then + flag=1 +else + flag=0 +fi +defaults write com.apple.finder CreateDesktop $flag && killall Finder diff --git a/img/icon-sm.svg b/img/icon-sm.svg new file mode 100644 index 0000000..fa119ca --- /dev/null +++ b/img/icon-sm.svg @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + run.sh + + \ No newline at end of file diff --git a/img/icon.svg b/img/icon.svg new file mode 100644 index 0000000..4082c4a --- /dev/null +++ b/img/icon.svg @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + run.sh + + Quit + + \ No newline at end of file diff --git a/img/screenshot.png b/img/screenshot.png new file mode 100644 index 0000000..27317c4 Binary files /dev/null and b/img/screenshot.png differ diff --git a/src/AppIcon.icns b/src/AppIcon.icns new file mode 100644 index 0000000..ef56361 Binary files /dev/null and b/src/AppIcon.icns differ diff --git a/src/Info.plist b/src/Info.plist new file mode 100644 index 0000000..aada7a0 --- /dev/null +++ b/src/Info.plist @@ -0,0 +1,36 @@ + + + + + CFBundleIdentifier + de.relikd.Menuscript + CFBundleExecutable + Menuscript + CFBundleName + Menuscript + CFBundleIconFile + AppIcon + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0.0 + CFBundleVersion + 1 + LSMinimumSystemVersion + 10.13 + LSBackgroundOnly + + LSUIElement + + NSHighResolutionCapable + + NSSupportsSuddenTermination + + NSPrincipalClass + NSApplication + NSHumanReadableCopyright + Copyright © 2026 relikd. + + diff --git a/src/main.swift b/src/main.swift new file mode 100755 index 0000000..da75caf --- /dev/null +++ b/src/main.swift @@ -0,0 +1,276 @@ +#!/usr/bin/env swift +import AppKit +import Cocoa +import SwiftUI + +class FlippedView: NSView { + override var isFlipped: Bool { true } +} + +class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { + private var statusItem: NSStatusItem! + private weak var settingsWindow: NSWindow? = nil + + func applicationDidFinishLaunching(_ aNotification: Notification) { + // UserDefaults.standard.register(defaults: ["storage": "~/"]) + initStatusIcon() + if userStorageURL() == nil { + showSettings() + } + } + + private func initStatusIcon() { + self.statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) + self.statusItem.button?.title = "⌘" + // self.statusItem.button?.image = NSImage.statusIcon + self.statusItem.menu = NSMenu(title: resolvedStorageURL().path) + self.statusItem.menu?.delegate = self + } + + func menuDidClose(_ menu: NSMenu) { + if menu == self.statusItem.menu { + self.statusItem.menu = NSMenu(title: menu.title) + self.statusItem.menu?.delegate = self + } + } + + /** + Delegate method not used. Here to prevent weird `NSMenu` behavior. + Otherwise, Cmd-Q (Quit) will traverse all submenus (incl. creating unopened). + */ + func menuHasKeyEquivalent(_ menu: NSMenu, for event: NSEvent, target: AutoreleasingUnsafeMutablePointer, action: UnsafeMutablePointer) -> Bool { + return false + } + + func menuNeedsUpdate(_ menu: NSMenu) { + for entry in Entry.listDir(menu.title) { + let itm = menu.addItem(withTitle: entry.title, action: nil, keyEquivalent: "") + itm.representedObject = entry + itm.image = entry.icon() + + if entry.isDir { + itm.submenu = NSMenu(title: entry.url.path) + itm.submenu?.delegate = self + } else { + itm.action = #selector(menuItemCallback) + } + } + + if menu == self.statusItem.menu { + menu.addItem(NSMenuItem.separator()) + menu.addItem(withTitle: "Preferences", action: #selector(showSettings), keyEquivalent: ",") + menu.addItem(withTitle: "Quit", action: #selector(NSApp.terminate), keyEquivalent: "q") + } + } + + @objc private func menuItemCallback(sender: NSMenuItem) { + (sender.representedObject as! Entry).run() + } + + // MARK: - Manage storage path + + private func resolvedStorageURL() -> URL { + userStorageURL() + // if run via .app bundle + ?? Bundle.main.url(forResource: "examples", withExtension: nil) + // if calling swift directly + ?? URL(string: "file://" + FileManager.default.currentDirectoryPath + "/" + #file)! + .deletingLastPathComponent() + .deletingLastPathComponent() + .appendingPathComponent("examples") + } + + private func userStorageURL() -> URL? { + UserDefaults.standard.url(forKey: "storage") + } + + @objc func selectStoragePath() { + let panel = NSOpenPanel() + panel.canChooseDirectories = true + panel.canCreateDirectories = true + panel.canChooseFiles = false + panel.allowsMultipleSelection = false + panel.begin { + if $0 == .OK { + UserDefaults.standard.set(panel.url, forKey: "storage") + self.statusItem.menu?.title = panel.url!.path + } + } + } + + @objc func openStoragePath() { + NSWorkspace.shared.open(resolvedStorageURL()) + } + + // MARK: - Settings View + + @objc func showSettings() { + if self.settingsWindow == nil { + let win = NSWindow( + contentRect: NSMakeRect(0, 0, 400, 115), + styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, + defer: true) + self.settingsWindow = win + win.title = "Preferences" + win.isReleasedWhenClosed = false // because .buffered + win.contentView = initSettingsView() + win.contentMinSize = NSMakeSize(300, win.contentView!.frame.size.height) + win.center() + win.makeKeyAndOrderFront(nil) + } + self.settingsWindow!.orderFrontRegardless() + } + + private func initSettingsView() -> NSView { + let pad = 20.0 + let view = FlippedView(frame: NSMakeRect(0, 0, 400, 120)) + + let lbl = NSTextField(frame: NSMakeRect(pad, pad, 400 - 2 * pad, 22)) + lbl.isEditable = false + lbl.isBezeled = false + lbl.drawsBackground = false + lbl.stringValue = "Scripts storage path:" + view.addSubview(lbl) + + let pth = NSPathControl(frame: NSMakeRect(pad, NSMaxY(lbl.frame), 400 - 2 * pad, 22)) + pth.autoresizingMask = [.maxYMargin, .width] + pth.isEditable = true + pth.allowedTypes = ["public.folder"] + pth.pathStyle = .standard + pth.url = resolvedStorageURL() + view.addSubview(pth) + + let chg = NSButton(title: "Change", target: self, action: #selector(selectStoragePath)) + chg.frame = NSMakeRect(pad, NSMaxY(pth.frame), 90, 40) + chg.autoresizingMask = [.maxXMargin, .maxYMargin] + view.addSubview(chg) + + let opn = NSButton(title: "Open", target: self, action: #selector(openStoragePath)) + opn.frame = NSMakeRect(NSMaxX(chg.frame), NSMaxY(pth.frame), 90, 40) + // opn.autoresizingMask = [.maxXMargin, .maxYMargin] + view.addSubview(opn) + view.frame.size.height = NSMaxY(pth.frame) + 40 + pad + return view + } +} + +// MARK: - A menu item + +struct Entry { + enum Flags { + case Verbose + case Text + } + + let order: Int + let title: String + let flags: [Flags] + let url: URL + + var isDir: Bool { url.hasDirectoryPath } + + init(_ url: URL) { + self.url = url + // TODO: remove file extension? + var fname = url.lastPathComponent + var idx = fname.firstIndex { !$0.isWholeNumber } ?? fname.startIndex + // sort order + if let order = Int(fname[.. [Entry] { + var rv: [Entry] = [] + for url + in (try? FileManager.default.contentsOfDirectory( + at: URL(string: path)!, + includingPropertiesForKeys: [.isDirectoryKey, .isExecutableKey])) ?? [] + { + if url.hasDirectoryPath || FileManager.default.isExecutableFile(atPath: url.path) { + rv.append(Entry(url)) + } + } + return rv.sorted() + } + + func icon() -> NSImage? { + guard isDir else { + return nil + } + var img: NSImage? = nil + for ext in ["svg", "png", "jpg", "jpeg", "gif", "ico"] { + let iconPath = self.url.appendingPathComponent("icon." + ext) + if FileManager.default.fileExists(atPath: iconPath.path) { + img = NSImage(contentsOf: iconPath) + break + } + } + if img == nil { + img = NSImage(named: NSImage.folderName) + } + img?.size = NSMakeSize(16, 16) + return img + } + + func run() { + let proc = Process() + proc.currentDirectoryURL = self.url.deletingLastPathComponent() + // proc.environment = ["HOME": "$HOME"] + if self.flags.contains(.Verbose) { + proc.launchPath = "/usr/bin/open" + proc.arguments = [self.url.path] + } else { + proc.executableURL = self.url + } + + if self.flags.contains(.Text) { + // open result in default text editor + let io = Pipe() + proc.standardOutput = io + proc.standardError = io + + proc.launch() + + let p2 = Process() + p2.launchPath = "/usr/bin/open" + p2.arguments = ["-f"] + p2.standardInput = io + p2.launch() + + // let data = io.fileHandleForReading.readDataToEndOfFile() + // let output = String(data: data, encoding: .utf8) ?? "" + } else { + proc.launch() + } + } +} + +extension Entry : Comparable { + static func < (lhs: Entry, rhs: Entry) -> Bool { + return (lhs.order, lhs.title) < (rhs.order, rhs.title) + } +} + +// MARK: - Main Entry + +let delegate = AppDelegate() +NSApplication.shared.delegate = delegate +NSApplication.shared.run() +// _ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)