I’m trying to create a script that will do two things: 1) check for the existence of certain files, and 2) move those files to the trash. I need to do #1 so that I can tell the user that a particular set of files was found and removed, and I need to do #2 using a shell script with admin privileges.
The code I’ve written so far works great, as long as I use absolute paths. As soon as I try to refer to the user folder using the tilde (‘~’) symbol, though, it all falls apart. Although I can hard-code the paths for the most part, I cannot hard-code the username.
How can I make this work for a path in the user’s home folder? The code in question is below:
– Given a list of path strings, returns a list of POSIX paths, quoted for use in shell scripts
on CreateQuotedPOSIXPathList(pathStrings)
set thePOSIXPaths to {}
repeat with thePath in pathStrings
set thePOSIXPaths to thePOSIXPaths & {quoted form of the POSIX path of thePath}
end repeat
return thePOSIXPaths
end CreateQuotedPOSIXPathList
– Given a list of quoted-form POSIX file paths, moves all files in the set to the trash
– Returns true if at least one file was found, false if none existed
on MoveFilesToTrash(fileList)
set someWereFound to false
repeat with oneFile in fileList
if exists oneFile then
set someWereFound to true
set theCommand to "mv " & oneFile & " ~/.Trash"
do shell script theCommand with administrator privileges
end if
end repeat
return someWereFound
end MoveFilesToTrash
set fileList to {“/Users/thomas/Desktop/test folder”, “/Users/thomas/Desktop/test file”}
set pathList to CreateQuotedPOSIXPathList(fileList)
MoveFilesToTrash(pathList)