From 405a8259bb69f4012ade6d95a50254ea49eb4ad3 Mon Sep 17 00:00:00 2001 From: Robin Breathe Date: Sat, 14 Jul 2018 23:15:32 +0200 Subject: [PATCH] Preliminary macOS Mojave compatibility, plus linting --- action.js | 57 ++++++++++++++---------------- filter.js | 101 ++++++++++++++++++++++++++--------------------------- info.plist | 2 +- 3 files changed, 77 insertions(+), 83 deletions(-) diff --git a/action.js b/action.js index c9de72a..b7cd120 100755 --- a/action.js +++ b/action.js @@ -1,25 +1,22 @@ #!/usr/bin/env osascript -l JavaScript function getenv(name, default_value) { - ObjC.import('stdlib') - try { - value = $.getenv(name); - } finally { - return (typeof value === 'undefined') ? default_value : value; - } + var env = $.NSProcessInfo.processInfo.environment.js; + return (name in env) ? env[name].js : default_value; } -function run(args) -{ - args = args.join(" ").split(" ") - var action = String(args[0]); +function run(args) { + args = args.join(" ").split(" "); + var app, action = String(args[0]); if (action == "ssh") { var host = String(args[1] || ""); - var app = Application("com.runningwithcrayons.Alfred-3"); - app.runTrigger(getenv("ssh_trigger", "ssh"), - {inWorkflow:getenv("ssh_workflow", "net.isometry.alfred.ssh"), withArgument:host}); + app = Application("com.runningwithcrayons.Alfred-3"); + app.runTrigger(getenv("ssh_trigger", "ssh"), { + inWorkflow:getenv("ssh_workflow", "net.isometry.alfred.ssh"), + withArgument:host + }); } else { - var app = Application(getenv("iterm_application", "com.googlecode.iterm2")); + app = Application(getenv("iterm_application", "com.googlecode.iterm2")); app.strictPropertyScope = true; app.strictCommandScope = true; app.strictParameterType = true; @@ -27,22 +24,22 @@ function run(args) var tab = win.tabs[Number(args[2])]; var ses = tab.sessions.byId(String(args[3])); switch (action) { - case "select": - // tab => session => window => app - tab.select(); - ses.select(); - win.select(); - app.activate(); - break; - case "close": - ses.close(); - break; - case "debug": - console.log(JSON.stringify({args:args, title:ses.name()})); - break; - default: - console.log("Invalid arguments:", JSON.stringify(args)); - break; + case "select": + // tab => session => window => app + tab.select(); + ses.select(); + win.select(); + app.activate(); + break; + case "close": + ses.close(); + break; + case "debug": + console.log(JSON.stringify({args:args, title:ses.name()})); + break; + default: + console.log("Invalid arguments:", JSON.stringify(args)); + break; } } } diff --git a/filter.js b/filter.js index 99edf55..9c410b7 100755 --- a/filter.js +++ b/filter.js @@ -1,47 +1,41 @@ #!/usr/bin/env osascript -l JavaScript function getenv(name, default_value) { - ObjC.import('stdlib') - try { - value = $.getenv(name); - } finally { - return (typeof value === 'undefined') ? default_value : value; - } + var env = $.NSProcessInfo.processInfo.environment.js; + return (name in env) ? env[name].js : default_value; } function sessionToObj(windowId, tabIndex, query) { - return function(session) { - return { - id: [windowId, tabIndex, session.id()].join(" "), - title: session.name(), - tty: session.tty(), - output: session.isProcessing(), - profile: session.profileName(), - query: query - } - } + return (session) => ({ + id: [windowId, tabIndex, session.id()].join(" "), + title: session.name(), + tty: session.tty(), + output: session.isProcessing(), + profile: session.profileName(), + query: query + }); } function objToItem(obj) { - var description = obj.tty + (obj.output?" *":""); - return { - title: obj.title, - subtitle: ["Select", description].join(" "), - arg: ["select", obj.id].join(" "), - uid: [obj.title, obj.profile].join("/"), - icon: { path: "icon.png" }, - mods: { - alt: { - arg: ["close", obj.id].join(" "), - subtitle: ["Close", description].join(" ") - }, - cmd: { - arg: ["ssh", obj.query].join(" "), - title: ["⇌ ssh", obj.query].join(" "), - subtitle: "Switch to ssh workflow" - } + var description = obj.tty + (obj.output?" *":""); + return { + title: obj.title, + subtitle: ["Select", description].join(" "), + arg: ["select", obj.id].join(" "), + uid: [obj.title, obj.profile].join("/"), + icon: { path: "icon.png" }, + mods: { + alt: { + arg: ["close", obj.id].join(" "), + subtitle: ["Close", description].join(" ") + }, + cmd: { + arg: ["ssh", obj.query].join(" "), + title: ["⇌ ssh", obj.query].join(" "), + subtitle: "Switch to ssh workflow" } } + }; } function allSessionObjs(app, query) { @@ -52,7 +46,11 @@ function allSessionObjs(app, query) { var windowId = windowIds[i]; var tabs = windows.byId(windowId).tabs; for (var tabIndex=0; tabIndex < tabs.length; ++tabIndex) { - results.push(...tabs[tabIndex].sessions().map(sessionToObj(windowId, tabIndex, query))); + results.push( + ...tabs[tabIndex].sessions().map( + sessionToObj(windowId, tabIndex, query) + ) + ); } } return results; @@ -60,19 +58,15 @@ function allSessionObjs(app, query) { function titleFilter(pattern) { var re = new RegExp(pattern.replace(/./g, "$&.*?")); - return function(obj) { - return re.exec(obj.title); - } + return (obj) => re.exec(obj.title); } function ttyFilter(ttyNum) { if (ttyNum.length == 0) { - return function() {return true;}; + return () => true; } else { var re = new RegExp(ttyNum.padStart(3, "0") + "$"); - return function(obj) { - return re.exec(obj.tty); - }; + return (obj) => re.exec(obj.tty); } } @@ -86,21 +80,24 @@ function run(args) { app.strictCommandScope = true; app.strictParameterType = true; if (app.running()) { - sessionObjs = allSessionObjs(app, query).filter(titleFilter(query)).filter(ttyFilter(tty)); + sessionObjs = allSessionObjs(app, query) + .filter(titleFilter(query)) + .filter(ttyFilter(tty)); } if (sessionObjs.length == 0) { return JSON.stringify({ - items: - [ - { - title: ["⇌ ssh", query].join(" "), - subtitle: "No matches! Switch to ssh workflow?", - arg: ["ssh", query].join(" "), - icon: { path: "icon.png" }, - } - ] + items: [ + { + title: ["⇌ ssh", query].join(" "), + subtitle: "No matches! Switch to ssh workflow?", + arg: ["ssh", query].join(" "), + icon: { path: "icon.png" }, + } + ] }); } else { - return JSON.stringify({items: sessionObjs.map(objToItem)}); + return JSON.stringify({ + items: sessionObjs.map(objToItem) + }); } } diff --git a/info.plist b/info.plist index ca3e5b0..d3e6415 100644 --- a/info.plist +++ b/info.plist @@ -210,7 +210,7 @@ Combine with an [iTerm2](https://www.iterm2.com/) profile configured as ssh prot net.isometry.alfred.ssh version - 1.3 + 1.4-beta1 webaddress https://github.com/isometry/alfred-tty