Can I extend Alfred with a command-line program to clean up the desktop?

Solution 1:

You have to use clean up window of desktop instead of clean up the desktop.

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

Solution 2:

Using @Lauri Ranta's answer as a starting point, I put together this Applescript for Alfred:

on alfred_script(q)
set q to "" & q
if q is "kind" then
    tell application "Finder" to clean up window of desktop by kind
else if q is "cdate" then
    tell application "Finder" to clean up window of desktop by creation date
else if q is "mdate" then
    tell application "Finder" to clean up window of desktop by modification date
else if q is "size" then
    tell application "Finder" to clean up window of desktop by physical size
else if q is "label" then
    tell application "Finder" to clean up window of desktop by label index
else
    tell application "Finder" to clean up window of desktop by name
end if
end alfred_script

You can see the different parameters to change the order that you want in the script (and change them to something that makes better sense to you if you want).

To use it, open Alfred's Preferences -> Extensions and click the + button in the lower left. Choose AppleScript and enter a name for it. (I called mine 'Clean up desktop'.) Write a Title and a Keyword for the script, and then paste the code into the AppleScript field. Click Save and you should be good to go!

If you're using Alfred 2: open Alfred's Preferences -> Workflows and click the + button in the lower left. Choose Templates -> Essentials -> Keyword to AppleScript. Double-click the keyword node and set the keyword you want (I chose cdesk), Title and Subtext (here I listed the possible arguments: kind, cdate, mdate, size, label). Save the results and then edit the Run NSAppleScript node and paste the code and save.