Keep Desktop Clean By Name

Recorded on February 2nd, 2015

I try not to keep a lot on my desktop, but sometimes it’s hard to avoid. What is there I like to have tidy, which means that I am frequently doing Finder » Clean Up By » Name

The first problem is that this only works if there are no other Finder windows open. Plus, it’s annoying to have to keep doing this every time the Desktop changes.

Enter AppleScript.

Thanks to this post on AskDifferent I found that the AppleScript command I wanted was:

    tell application "Finder" to clean up window of desktop by name

or in shell script format:

    osascript -e 'tell application "Finder" to clean up window of desktop by name'

that worked just fine. Now I just needed a way to do it automatically whenever the Desktop changed. I could use Hazel for this, but for simple folder monitoring and triggering, I prefer launchd:

    <?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>sync.com.tjluoma.cleandesktop</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/bin/osascript</string>
            <string>-e</string>
            <string>tell application "Finder" to clean up window of desktop by name</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>WatchPaths</key>
        <array>
            <string>/Users/luomat/Desktop</string>
        </array>
    </dict>
    </plist>

Translation: “Any time the /Users/luomat/Desktop/ folder changes, run osascript -e 'tell application "Finder" to clean up window of desktop by name'

Obviously you’ll need to change <string>/Users/luomat/Desktop</string> to the proper path for your $HOME.

To Install

  1. Download gist from GitHub
  2. Change /Users/luomat/ to whatever your $HOME directory is
  3. Move file to ~/Library/LaunchAgents/sync.com.tjluoma.cleandesktop.plist (you can use whatever name you like, just be sure it ends with .plist)
  4. To activate it, run this in Terminal:

    launchctl load ~/Library/LaunchAgents/sync.com.tjluoma.cleandesktop.plist

To Uninstall:

In Terminal:

    launchctl unload ~/Library/LaunchAgents/sync.com.tjluoma.cleandesktop.plist

    mv ~/Library/LaunchAgents/sync.com.tjluoma.cleandesktop.plist ~/.Trash/

The first line disables it in launchd and the second moves the plist to the trash folder so it won’t be used in the future.

Teaser

Wondering why I called it sync.com.tjluoma.cleandesktop.plist instead of com.tjluoma.cleandesktop.plist? I’ll explain that in another post.