Skip to content

Instantly share code, notes, and snippets.

@mpjuers
Last active June 4, 2020 21:44
Show Gist options
  • Save mpjuers/dff63156cacfe7e2deffbec4fe33eee9 to your computer and use it in GitHub Desktop.
Save mpjuers/dff63156cacfe7e2deffbec4fe33eee9 to your computer and use it in GitHub Desktop.
Hammerspoon eject menu #lua #hammerspoon #desktopautomation
changeEjectMenuIcon = function (mods)
if mods:containExactly({'cmd'}) then
ejectMenu:setTitle('⮑')
elseif mods:containExactly({}) then
ejectMenu:setTitle('⏏')
end
return(0)
end
ejectAll = function ()
hs.osascript.applescript(
'tell application "Finder" to eject (every disk whose ejectable is true and local volume is true and free space is not equal to 0)'
)
hs.notify.show('All drives unmounted.', '', '')
end
execMenuItem = function (mods, table)
if (
mods['cmd'] == true and
mods['ctrl'] == false and
mods['alt'] == false and
mods['shift'] == false and
mods['fn'] == false
) then
hs.osascript.applescript('tell application "Finder" to open ("/Volumes/' .. table['title'] .. '/" as POSIX file)')
else
hs.osascript.applescript('tell application "Finder" to eject disk "' .. table['title'] .. '"')
hs.notify.show(table['title'] .. ' unmounted.', '', '')
end
end
initEjectMenu = function ()
local ejectMenuDrives = select(
2, hs.osascript.applescript(
'tell application "Finder" to get the name of (every disk whose ejectable is true)'
)
)
local ejectMenuTable = {
{title = "Eject All", fn = ejectAll},
{title = '-'}
}
if pcall(function () next(ejectMenuDrives) end) then
for k, drive in pairs(ejectMenuDrives) do
print(drive .. " is ejectable.")
table.insert(ejectMenuTable, {title = drive, fn = execMenuItem})
end
else
print("No external drives.")
end
return(ejectMenuTable)
end
ejectMenu = hs.menubar.new():setTitle("⏏")
ejectMenu:setMenu(initEjectMenu)
flagsEvent = hs.eventtap.new(
{hs.eventtap.event.types.flagsChanged},
function (event) changeEjectMenuIcon(event:getFlags()) end
)
flagsEvent:start()
hs.hotkey.bind(hyper2, "E", ejectAll)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment