diff --git a/Kill Process.alfredworkflow b/Kill Process.alfredworkflow index b2a3870..9027249 100644 Binary files a/Kill Process.alfredworkflow and b/Kill Process.alfredworkflow differ diff --git a/Readme.md b/Readme.md index 4bbb65b..4adf15b 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,5 @@ # Description -Kill Process is an Alfred 2 workflow that makes it easy to kill misbehaving processes. It is, in essence, a way to easily find processes by name and kill them using `kill -9`. +Kill Process is an Alfred 3 workflow that makes it easy to kill misbehaving processes. It is, in essence, a way to easily find processes by name and kill them using `kill -9`. # Features * Autocompletes process names @@ -10,6 +10,7 @@ Kill Process is an Alfred 2 workflow that makes it easy to kill misbehaving proc * Shows process paths * Ignores case * Kills all processes with matching names on cmd+return +* Kills and relaunches a process on shift+return * Supports [Alleyoop updating](http://www.alfredforum.com/topic/1582-alleyoop-update-alfred-workflows/). ![screenshot: `kill it`](screenshot1.png) @@ -19,8 +20,9 @@ Kill Process is an Alfred 2 workflow that makes it easy to kill misbehaving proc 1. Type `kill` into Alfred followed by a space. 2. Begin typing the name of the process you want to kill. 3. When you see the process you want to kill, select it from the list as usual. -4. Press return to kill the selected process. -Alternatively, press cmd+return to kill all processes with the same name as the selected one. +4. Press return to kill the selected process. Alternatively: + - Press cmd+return to kill all processes with the same name as the selected one. + - Press shift+return to relaunch the selected process after killing it. To filter by argument, add a colon and the argument you want to target (or a snippet of it) after your processes name (see the [second screenshot](screenshot2.png)). diff --git a/script.rb b/script.rb index d71ce68..d6d8802 100644 --- a/script.rb +++ b/script.rb @@ -1,3 +1,4 @@ +require 'json' # Type a query to test with here. # !!!!! Comment this line out when pasting into alfred preferences. theQuery = "chr" @@ -14,9 +15,11 @@ # Grep for processes whose name contains the query. The regex isolates the name by only searching characters after the last slash in the path. # The -i flag ignores case. processes = `ps -A -o pid -o %cpu -o comm | grep -i [^/]*#{Regexp.quote(theQuery)}[^/]*$`.split("\n") -# Start the XML string that will be sent to Alfred. This just uses strings to avoid dependencies. -xmlString = "\n\n" +# Create a hash that will be serialised into a JSON string for Alfred. +result = {items: []} processes.each do | process | + # Create a hash for each matched process in the script filter output. + item = {variables:{}, icon: {}} # Extract the PID, CPU usage, and path from the line (lines are in the form of `123 12.3 /path/to/process`). processId, processCpu, processPath = process.match(/(\d+)\s+(\d+[\.|\,]\d+)\s+(.*)/).captures # If an argument filter has been specified, get the arguments and search for the filter. @@ -30,25 +33,25 @@ end # Use the same expression as before to isolate the name of the process. processName = processPath.match(/[^\/]*#{theQuery}[^\/]*$/i) + # Start assembling this process's JSON values. + item[:uid] = processName + item[:title] = "#{processName}#{matchedArgs.join(" ")}" + item[:arg] = processId + item[:subtitle] = "#{processCpu}% CPU @ #{processPath}" + # Add processPath to the workflow's environment variables when this item is actioned. This enables relaunching the process. + item[:variables][:path] = processPath # Search for an application bundle in the path to the process. - iconValue = processPath.match(/.*?\.app\//) + item[:icon][:path] = processPath.match(/.*?\.app\//) # The icon type sent to Alfred is 'fileicon' (taken from a file). This assumes that a .app was found. - iconType = "fileicon" + item[:icon][:type] = "fileicon" # If no .app was found, use OS X's generic 'executable binary' icon. # An empty icon type tells Alfred to load the icon from the file itself, rather than loading the file type's icon. - if !iconValue - iconValue = "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ExecutableBinaryIcon.icns" - iconType = "" + if !item[:icon][:path] + item[:icon][:path] = "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ExecutableBinaryIcon.icns" + item[:icon][:type] = "" end - # Assemble this item's XML string for Alfred. See http://www.alfredforum.com/topic/5-generating-feedback-in-workflows/ - thisXmlString = "\t - #{processName}#{matchedArgs.join(" ")} - #{processCpu}% CPU @ #{processPath} - #{iconValue} - \n" - # Append this process's XML string to the global XML string. - xmlString += thisXmlString + # Append this process's hash to the global hash. + result[:items].push(item) end -# Finish off and echo the XML string to Alfred. -xmlString += "" -puts xmlString \ No newline at end of file +# Serialise the global hash as a JSON string and echo it to Alfred. +puts result.to_json()