Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Kill Process.alfredworkflow
Binary file not shown.
8 changes: 5 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <kbd>cmd</kbd>+<kbd>return</kbd>
* Kills and relaunches a process on <kbd>shift</kbd>+<kbd>return</kbd>
* Supports [Alleyoop updating](http://www.alfredforum.com/topic/1582-alleyoop-update-alfred-workflows/).

![screenshot: `kill it`](screenshot1.png)
Expand All @@ -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 <kbd>cmd</kbd>+<kbd>return</kbd> to kill all processes with the same name as the selected one.
4. Press return to kill the selected process. Alternatively:
- Press <kbd>cmd</kbd>+<kbd>return</kbd> to kill all processes with the same name as the selected one.
- Press <kbd>shift</kbd>+<kbd>return</kbd> 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)).

Expand Down
39 changes: 21 additions & 18 deletions script.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'json'
# Type a query to test with here.
# !!!!! Comment this line out when pasting into alfred preferences.
theQuery = "chr"
Expand All @@ -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 = "<?xml version=\"1.0\"?>\n<items>\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.
Expand All @@ -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<item uid=\"#{processName}\" arg=\"#{processId}\">
<title>#{processName}#{matchedArgs.join(" ")}</title>
<subtitle>#{processCpu}% CPU @ #{processPath}</subtitle>
<icon type=\"#{iconType}\">#{iconValue}</icon>
</item>\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 += "</items>"
puts xmlString
# Serialise the global hash as a JSON string and echo it to Alfred.
puts result.to_json()