Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hs.fs.volume.eject is not executed at hs.caffeinate.watcher.systemWillSleep event #3742

Open
mrdc opened this issue Jan 22, 2025 · 0 comments

Comments

@mrdc
Copy link

mrdc commented Jan 22, 2025

Hello,

I'm trying to do some pre-sleep tasks when my MacBook goes to sleep. Especially, unmount several remote volumes, but hs.fs.volume.eject("/Volumes/SomeVolume") is not executed at hs.caffeinate.watcher.systemWillSleep event.
Initially, I've tried this and the unmount wasn't successful:

function caffeinateWatcher(eventType)
    if (eventType == hs.caffeinate.watcher.systemWillSleep) then
            print ("Executing pre-Sleep tasks...")
            -- Execute sleep script
            hs.task.new("/Users/user/.sleep", nil):start():waitUntilExit()
            --hs.task.waitUntilExit()
    elseif (eventType == hs.caffeinate.watcher.systemDidWake) then
            print ("Executing after-Wake tasks...")
            -- Execute wake script
            --hs.task.new("/Users/user/.wakeup", nil):start()

    end
end

sleepWatcher = hs.caffeinate.watcher.new(onSystemSleep)
sleepWatcher:start()

.sleep:

#!/bin/bash
caffeinate -i diskutil unmount "/Volumes/SomeVolume"

# or

diskutil unmount "/Volumes/SomeVolume"

This also failed:

local function ejectVolume(volumeName)
    local ejectTask = hs.task.new("/usr/sbin/diskutil", function(exitCode, stdout, stderr)
        if exitCode == 0 then
            hs.notify.new({
                title = "Volume Ejected",
                informativeText = "Successfully ejected volume: " .. volumeName
            }):send()
        else
            hs.notify.new({
                title = "Eject Failed",
                informativeText = "Failed to eject volume: " .. volumeName .. "\nError: " .. stderr
            }):send()
        end
    end, {"unmount", volumeName})

    ejectTask:start()
end

Using AppleScript also failed:

hs.osascript.applescript([[
        tell application "Finder"
                eject disk SomeVolume
        end tell
    ]])

Is it possible to delay going to sleep and execute all tasks triggered by the systemWillSleep event?

macOS Sonoma 14.7.2
Hammerspoon 1.0.0 (6864)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant