Automatically Delete Files From Downloads Folder Daily Unless Tagged With “Keep”?
-
01-06-2021 - |
質問
How can I automate the deletion of files from my downloads folder but keep files I've tagged with "Keep"? I've been able to use Automator to delete files daily, but I can't work with tags in Automater. Most likely this requires AppleScript (I'm fairly new to AppleScript). I've created an AppleScript to delete files from Downloads folder, but it's not working (I can't tell if it's a permissions issue), and I'm lost on how file tags/labels work in AppleScript.
try
tell application "Finder"
delete (every item of folder "/Users/wheel58m/Library/Mobile Documents/com~apple~CloudDocs/Downloads")
end tell
on error
display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try
Any documentation or resources is appreciated.
解決
Automatically Delete Files From Downloads Folder Daily Unless Tagged With “Keep”?
The following example AppleScript code can be used in a shell script with an /usr/bin/osascript
shebang, or an AppleScript script/application, or a Run AppleScript action in an Automator workflow to delete any file in the targetFolder
that does not have a custom "Keep" Tag set in Finder:
-- # targetFolder can be either an HFS path or a POSIX path,
-- # "path:to:target:folder:" or "/path/to/target/folder"
-- #
-- # Change the value of targetFolder from "/path/to/target/folder"
-- # to the appropriate fully qualified pathname in either form.
set targetFolder to "/path/to/target/folder"
-- # The remaining code should not need to be modified,
-- # unless the mdfind query needs to be changed.
set targetFolderPOSIXpath to ¬
the quoted form of ¬
the POSIX path of targetFolder
set shellCMD to {¬
"mdfind -onlyin", space, targetFolderPOSIXpath, space, ¬
"'! kMDItemKind == Folder && ! kMDItemUserTags == Keep'"} ¬
as string
set posixFilesToDelete to ¬
paragraphs of ¬
(do shell script shellCMD)
set filesToDelete to {}
repeat with thisFile in posixFilesToDelete
set thisFile to POSIX file thisFile as alias
set the end of filesToDelete to thisFile
end repeat
if filesToDelete is not {} then ¬
tell application "Finder" to ¬
delete filesToDelete
Note: Scroll as necessary to see remaining code.
Notes:
- As I do not have the target folder mentioned in the OP, I could not test for any permissions issues that may need addressing in: System Preferences > Security & Privacy > Privacy
- The example AppleScript code was tested and work for me without issue in macOS Catalina using a temporary folder created within my Home folder while adding files and folders to it and tagging only some files with a custom "Keep" Tag in Finder.
- As to "Automatically Delete Files" there are a number of different ways to accomplish this depending on how the example AppleScript code is used. Used as an AppleScript shell script or application it can be scheduled to run at whatever time you'd like, using any of several different methods.
- As an AppleScript application using: Calendar
- As an AppleScript shell script using:
launchd
orcron
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.
他のヒント
From what I can tell, tags are still called "labels" in Applescript. Finder will return a different "label index" depending on a file's tag/label.
tell application "Finder" to get label index of file "path:to:file"
When a file has no tags, this returns "0". When a file has a "red" tag, it returns 2, and when it has a "yellow" tag, it returns 3. When a file has a "red" and a "yellow" tag, it... still returns 3. 😬
This all feels kind of weird/fragile, but if you can't find a better way to do this (!), I'd do some experimenting to find the "label index" of your keep
tag, and then check for that in your code.
The following AppleScript is a working version of the code you posted, if you replace <SystemDrive>
and <username>
with the names of your system drive and username, respectively.
try
tell application "Finder"
delete (every item of folder "<SystemDrive>:Users:<username>:Library:Mobile Documents:com~apple~CloudDocs:Downloads")
end tell
on error
display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try
See Apple's documentation on working with files and folders in AppleScript.
I couldn't find any mention of tags in AppleScript's Finder library but Howard Oakley states that they can be added with AppleScript, hence they should be accessible, perhaps as labels as @Wowfunhappy suggests. You might need to install and learn how to use tag
, as mentioned in the same article.