Skip to content

Commit

Permalink
Add configure menu listing all workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-pennyworth committed Jul 11, 2024
1 parent 79062ef commit 455d331
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
40 changes: 40 additions & 0 deletions AlfredExtraPane/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,36 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

let menu = NSMenu()

// Menu for configuring panes
let configureMenu = NSMenu()
let globalConfigMenuItem = NSMenuItem(
title: "Global",
action: #selector(openFileAction(_:)),
keyEquivalent: ""
)
globalConfigMenuItem.representedObject = try? globalConfigFile()
configureMenu.addItem(globalConfigMenuItem)
configureMenu.addItem(NSMenuItem.separator())
for workflow in Alfred.workflows().sorted(by: {$0.name < $1.name}) {
let menuItem = NSMenuItem(
title: workflow.name,
action: #selector(openFileAction(_:)),
keyEquivalent: ""
)
menuItem.representedObject =
workflow.dir.appendingPathComponent(workflowConfigFilename)
configureMenu.addItem(menuItem)
}
let configureMenuItem = NSMenuItem(
title: "Configure",
action: nil,
keyEquivalent: ""
)
configureMenuItem.submenu = configureMenu
menu.addItem(configureMenuItem)

menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(
title: "Check for Updates",
action: #selector(checkForUpdates),
Expand Down Expand Up @@ -139,6 +169,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@objc func quit() {
NSApplication.shared.terminate(nil)
}

@objc func openFileAction(_ sender: NSMenuItem) {
if let filePath = sender.representedObject as? URL {
if !fs.fileExists(atPath: filePath.path) {
let empty: [PaneConfig] = []
write(empty, to: filePath)
}
openFile(atPath: filePath.path)
}
}
}

autoreleasepool {
Expand Down
7 changes: 7 additions & 0 deletions AlfredExtraPane/utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func readFile<T>(named: String, then: (String) -> T) -> T? {
}
}

func openFile(atPath path: String) {
let task = Process()
task.launchPath = "/usr/bin/open"
task.arguments = [path]
task.launch()
}

// src: https://stackoverflow.com/a/26406426
extension URL {
var queryParameters: QueryParameters { return QueryParameters(url: self) }
Expand Down

0 comments on commit 455d331

Please sign in to comment.