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 → Skillet.scpt.zip (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 HH.mm.ss"
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
else
display notification ¬
"No items were selected in the Finder!" with title ¬
"Desktop Cleanup Script" subtitle ¬
"Error Text:" sound name "Tink"
end if
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
# 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
else
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).
-Chris