0

Whenever I unlock my macOS machine, I want it to play a sound effect from an mp3 file.

  • So I created an automator .app to play the sound using Afplay shell command.
  • Tested the app successfully.
  • And added that app as a login item under Users & Groups.
  • Granted the system permission to access the mp3 location when it prompted me to do so.

However, I fear that it is only being triggered when I do a full logout. Not whenever I unlock the lock screen.

enter image description here

Catalina 10.15.6 (19G73)

1 Answer 1

2

I believe you'll need to use a Launch Agent to watch for when the /Library/Preferences/com.apple.loginwindow.plist file is touched, as it is when the lock screen is unlocked, to have something you want to have happen when the lock screen password unlock event occurs.

Please read the answer in its entirety before implementing it.

The following was tested and works on a clean install of macOS Catalina 10.15.6.

Open Terminal and execute the following commands:

mkdir -p ~/Library/LaunchAgents
cd ~/Library/LaunchAgents
touch com.my.detect.screen.unlocked.plist
open -e com.my.detect.screen.unlocked.plist

Copy and paste the example XML code, shown further below, into the opened com.my.detect.screen.unlocked.plist document and then save and close the document.

Back in Terminal, execute the following command:

launchctl load com.my.detect.screen.unlocked.plist

You can now close Terminal, if you wish.

To test, click the Lock Screen menu item on the menu, then log back in.


Example XML code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.my.detect.screen.unlocked</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/afplay</string>
        <string>/System/Library/Frameworks/SecurityInterface.framework/Versions/A/Resources/lockOpening.aif</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/com.apple.loginwindow.plist</string>
    </array>
</dict>
</plist>

Notes:

  • The sound file in this example XML code obviously can be changed to whatever you'd like, however, I'd suggest that its path is not within default folders of the root of your Home folder as there may be an issue accessing it. You'll have to test if it's an issue for you. As an example, in the root of my Home folder I have folders I've created that where not originally there, that is where I'd place it.

  • This will also trigger when you log out as the /Library/Preferences/com.apple.loginwindow.plist file is also touched during a log out. By touched, it means at a minimum the date/time of the file has changed, not necessarily its contents. (I was not able to trap a change to its contents, if there even was one, in spite of the date/time of the file having changed.) There may be other events that can cause it to be touched, so this is not a perfect solution. I'd still implement it and test it for a while. If you are satisfied, you can leave it in place or remove it as you wish.

  • To stop the /Library/Preferences/com.apple.loginwindow.plist file from being watched, and thus the sound no longer being played when the lock screen is unlocked, you'll need to unload the com.my.detect.screen.unlocked.plist file and then delete it.

    • In Terminal:

      cd ~/Library/LaunchAgents
      launchctl unload com.my.detect.screen.unlocked.plist 
      rm com.my.detect.screen.unlocked.plist
      

Alternate Method

This involves the use of a third-party application called EventScripts which costs $4.99 USD on the US App Store, however, there is a demo version you can download from the developers website to give a test run to see if you feel it's worth the price.

This application sits in the menu bar area with the other menu applets and response to a variety of events, the one in particular of this subject is Screen password unlocked in which you'd use the AppleScript do shell script command to execute, e.g. afplay /path/to/filename.mp3.

Note: I am not affiliated with the developer of EventScripts, just a satisfied user of the product.

Example AppleScript code:

on run eventArgs
    set thisTrigger to (trigger of eventArgs)
    if thisTrigger is "Screen password unlocked" then
        do shell script "/usr/bin/afplay /System/Library/Frameworks/SecurityInterface.framework/Versions/A/Resources/lockOpening.aif"
    end if
end run
8
  • Thank you! Would you expect ~/sound_effects to satisfy "root of my Home folder I have folders I've created that where not originally there"? $pwd returns /Users/layne/sound_effects
    – Kermit
    Commented Oct 14, 2020 at 11:15
  • It looks like you or something you installed created /System/Library/Frameworks/SecurityInterface.framework because I don't have it
    – Kermit
    Commented Oct 14, 2020 at 11:17
  • @HashRocketSyntax Yes, ~/sound_effects should be fine. -- RE: "It looks like you or something you installed created /System/Library/Frameworks/SecurityInterface.framework because I don't have it" -- As I stated in my answer "The following was tested and works on a clean install of macOS Catalina 10.15.6.". -- /System/Library/Frameworks/SecurityInterface.framework/Versions/A/Resources/lockOpening.aif is a part of macOS and is the sound that is played when one unlocks the Lock in, e.g., System Preferences > Security & Privacy. Commented Oct 14, 2020 at 13:04
  • @HashRocketSyntax So it has to be on your system too. That said, it is only in the example XML code as that is what I use to test the code works as stated and as stated in the Notes: section of my answer "The sound file in this example XML code obviously can be changed to whatever you'd like ..." Commented Oct 14, 2020 at 13:04
  • @HashRocketSyntax I had a typo in one of the Terminal commands mkdir -p cd ~/Library/LaunchAgents the cd should not have been there and the actual command is mkdir -p ~/Library/LaunchAgents. So you can delete the erroneous cd folder that was created within the root of your Home folder. Commented Oct 14, 2020 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .