Clean Desktop

Continuing the discussion from Toggle Finder File Selection to Web Friendly Names:

Thanks to help at the previous post I was able to convert over clean desktop action I used in QuicKeys for time stamping and moving files from the desktop to a Clean Desktop folder in ~/Clean Desktop/CD Keyboard Maestro Temp/

This will then move the files to a folder in there with a time stamp.

I got stuck on checking if the folder exists and then creating it but just made it by manually.

It would be great if
"/Users/~/Clean Desktop/CD Keyboard Maestro Temp" but you have to put in the current user for the move or rename file command.

QK - Clean Desktop in Home Folder.kmmacros (21.3 KB)

Two things I really liked about the QuicKeys macro is that it plays the standard move file sound when it is finished moving. However in Keyboard Maestro I hear no audio feedback that a file was actually moved. I know I can choose the play sound action when Macro is done but I want it to be when the files are moved in relationship to the moved files. Not just some hack that plays the sound regardless of files being moved or not when the macro is finished running.

/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Unmount.aif

The second is that in Keyboard Maestro I will get newly created folders regardless of if I have a file or folder selected. I am sure there is someway to make it abort the next steps if nothing is selected. I have abort Macro on "For each" in these collections but it still moves on.

The form is "~/Clean Desktop/…"

"/Users/~/Clean Desktop" would refer to a directory within the (really dumbly) named "~" in the "Users" folder.

I imagine you can use AppleScript for the move instead and ask the Finder to do the move which would then presumably make the sound.

if you were moving a bunch of files though, that would be a low of sounds!

Remove the first AppleScript. Add the Create New Folder action inside the For Each, and turn off

And turn off the Notify and Abort on Failure options.

At the end, you can rename the folder within Keyboard Maestro as well:

Mark that as no notification or abort on failure as well.

No AppleScript required, no folder created unless you have something selected. No sound still though.

Hey Skillet,

Apparently you misunderstand how $HOME directory (Tilde) notation works.

~     == your home directory
~/    == your home directory
~/xyz == a subdirectory in your home directory

E.g. ~/ is your user-directory.

You're jumping around between AppleScript, the shell, and Keyboard Maestro too much.

Have a look at this:

Clean Desktop in Home Folder.ccs.kmmacros (24 KB)

Unfortunately the Finder Selection collection doesn't have a test for whether it is empty or not, so I've created a clean-up directory beforehand and copy selected items into it if there's a selection (or delete it later if there is NO selection).

There are a couple of ways to test for the Finder selection before continuing:

  • You can do it with AppleScript.
  • You can save each line of the Finder Selection collection to a variable and test the variable afterward to make certain it is NOT empty.
  • If it's NOT empty then use a For Each with Lines in a Variable collection to do the processing.

Working → Sweeper → (12.2 KB)

Personally I prefer to do this sort of job entirely with AppleScript:

# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/08/09 21:40
# dMod: 2016/08/09 22:01 
# Appl: Finder
# Task: Move Selected Items in the Finder to a date-stamped clean-up folder.
# Ajoc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Move, @Selection, @Selected, @Items
# Vers: 1.00
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set dateFormat to "y-dd-MM"

tell application "Finder" to set finderSelectionList to selection as alias list

if finderSelectionList ≠ {} then
   set dateStamp to my formatDate:(current date) usingFormat:dateFormat
   set cleanUpFolderPath to "~/Clean Desktop/Cleaned " & dateStamp
   set cleanUpFolderPath to current application's NSString's stringWithString:cleanUpFolderPath
   set cleanUpFolderPath to cleanUpFolderPath's stringByExpandingTildeInPath() as string
   my createDirectoryAtPath:cleanUpFolderPath
   set cleanUpFolderAlias to alias POSIX file cleanUpFolderPath
   tell application "Finder"
      move finderSelectionList to cleanUpFolderAlias
   end tell
   display notification ¬
      "No items were selected in the Finder!" with title ¬
      "Desktop Cleanup Script" subtitle ¬
      "Error Text:" sound name "Tink"
end if

# createDirectoryAtPath will create intermediate directories as necessary
on createDirectoryAtPath:thePath
   set {theResult, theError} to current application's NSFileManager's defaultManager()'s createDirectoryAtPath:thePath withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
   if not (theResult as boolean) then
      set errorMsg to theError's localizedDescription() as text
      error errorMsg
   end if
end createDirectoryAtPath:
on formatDate:theDate usingFormat:formatString
   if class of theDate is date then set theDate to my makeNSDateFrom:theDate
   set theFormatter to current application's NSDateFormatter's new()
   theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
   theFormatter's setDateFormat:formatString
   set theString to theFormatter's stringFromDate:theDate
   return theString as text
end formatDate:usingFormat:
on makeNSDateFrom:theASDate
   set {theYear, theMonth, theDay, theSeconds} to theASDate's {year, month, day, time}
   if theYear < 0 then
      set theYear to -theYear
      set theEra to 0
      set theEra to 1
   end if
   set theCalendar to current application's NSCalendar's currentCalendar()
   set newDate to theCalendar's dateWithEra:theEra |year|:theYear |month|:(theMonth as integer) ¬
      |day|:theDay hour:0 minute:0 |second|:theSeconds nanosecond:0
   return newDate
end makeNSDateFrom:

By using the Finder all items are moved in one job (instead of one-by-one), and you get a move dialog (if warranted).



Take a look at the example macro I just published based on another user's request. If I understand your requirements, it should be fairly easy to adapt to your needs:

In the "Move Each Item..." Action (rename of "For Each..." action), you can easily:

  • modify the file name to make it time stamped
  • Add a "Play Sound" action of your choice

The "Choose Folder" Script Action will allow you to both select existing folders, and to create new ones.

Thanks I just had a brain fart, I knew that and have used that several times.

Yes that would be a really dumb user name I wonder if you could even create one with that name.

That is great, I should have tested what would happen if I tried to create a folder where there was one already. This is what you were talking about in another thread. It is all coming together better now the move or create now that I am seeing it in action more.

I would have never guessed that would rename the file at that path, very helpful to see and something I will use multiple times. Thanks for showing me how it is done in Keyboard Maestro.

You're a ninja!

And I get my move sound back that indicates files have really moved. Much faster.

Interesting when I run that AppleScript in Script Debugger it works just fine until I press enter and then it gives me an error. In Script Editor I get the error regardless of running it first or not.

Thanks I'll have to take a closer look at it. I am getting an error initially and it is not moving my selection but letting me choose a folder. I'll figure out what is happening.

Hey Skillet,

That's odd.

It works fine for me in the Script Editor and in Script Debugger 5 & 6.

Are you using OSX El Capitan? I'm on OSX 10.11.6.


Hey Skillet,

One thought – change that as string highlighted in the error to as text


I am using macOS 10.11.6 & Script Debugger 5 (will hopefully purchase version 6 soon).

Works perfectly with that change after compiled in Script Editor and Script Debugger 5.

Hello, I happened to read this post today. In this post, a tutorial will show you how to clean your desk quickly. would that be something for you @skillet?

Thanks, always nice to see alternative methods.

1 Like