Skip to content

Instantly share code, notes, and snippets.

@twio142
Last active November 25, 2021 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twio142/dbed097abce0d3381c0eb1c26ff7fe4e to your computer and use it in GitHub Desktop.
Save twio142/dbed097abce0d3381c0eb1c26ff7fe4e to your computer and use it in GitHub Desktop.
An Alfred JXA workflow to list currently running apps, with fuzzy match
#!/usr/bin/osascript -l JavaScript
// Adapted from the original script of @deanishe
// https://git.deanishe.net/deanishe/alfred-quit-apps/src/branch/master/DockApplications.js
ObjC.import('AppKit')
ObjC.import('stdlib')
function run(argv) {
let apps = ObjC.unwrap($.NSWorkspace.sharedWorkspace.runningApplications)
.filter(app => app.activationPolicy == $.NSApplicationActivationPolicyRegular && !ObjC.unwrap(app.backgroundOnly))
.map(app => {
return {
title: ObjC.unwrap(app.localizedName),
subtitle: ObjC.unwrap(app.bundleURL.fileSystemRepresentation),
arg: ObjC.unwrap(app.bundleURL.fileSystemRepresentation),
variables: {pid: ObjC.unwrap(app.processIdentifier)},
match: getMatch(ObjC.unwrap(app.bundleURL.fileSystemRepresentation)) || ObjC.unwrap(app.localizedName),
icon: { path: ObjC.unwrap(app.bundleURL.fileSystemRepresentation), type: 'fileicon' },
type: 'file'
}
})
function getMatch(filePath) {
// Retrieve match string from Alfred's filecache database
filePath = filePath.replace(/(?=["\\])/, '\\')
var db = $.getenv('alfred_workflow_data').split('/').slice(0,-2).join('/') + '/Databases/filecache.alfdb'
var sql = `sqlite3 "${db}" << EOF
SELECT name || IFNULL(altnames, "") || "," || IFNULL(nameChars, "")
FROM files
WHERE path = "${filePath}" OR path = "/System/Volumes/Data${filePath}"
EOF`
var app = Application.currentApplication()
app.includeStandardAdditions = true
return app.doShellScript(sql)
}
return JSON.stringify({'items': apps}, null, 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment