I want a folder action that deletes all non pdf files. My script as follows does not work
on adding folder items to this_folder after receiving added_items
repeat with i from 1 to count of added_items
set currentFile to item i of added_items
tell application "System Events"
if file type of currentFile is not in {"PDF "} then
delete currentFile
end if
end tell
end repeat
end adding folder items to
I tried simplifying it by getting rid of the if statement and telling it to delete all files, but even that did not work. Any help appreciated.
When I run a ‘get file type’ command on a pdf file, I get missing value. Apparently this has been deprecated.
Try using the type identifier:
type identifier:"com.adobe.pdf"
on adding folder items to this_folder after receiving added_items
repeat with i from 1 to count of added_items
set currentFile to item i of added_items
tell application "System Events"
if type identifier of currentFile is not in {"com.adobe.pdf"} then
delete currentFile
end if
end tell
end repeat
end adding folder items to
I should note that for some unknown reason, I couldn’t get any file deleted using system events. I switched to using the finder (inside the system events block) and suddenly stuff was vanishing instantly. I then removed the tell part of the statement, leaving the work to system events and it continues to delete with wild abandon.
glenrubin. FWIW. In recent versions of macOS, System Events will not reliably work with aliases with the delete and other commands. The Finder is a good alternative, and this is demonstrated in the following script. Unfortunately, setting permissions for Folder Actions and Open-handler scripts in recent versions of macOS is difficult and, occasionally, simply not possible.
set theFiles to (choose file with multiple selections allowed)
set deleteItems to {}
tell application "Finder"
repeat with aFile in theFiles
if (kind of aFile) is not "PDF document" then -- localize "PDF document" if required
set end of deleteItems to aFile
end if
end repeat
display dialog "Move " & (count deleteItems) & " files that are not PDFs to the trash?"
delete deleteItems
end tell
Thanks to all for the important information on System Events being deprecated for the operations I am attempting!! I would have never found that on my own!!!
FWIW, there are two methods that can be used to move files to the trash with ASObjC. In testing on my Sonoma computer, both work reliably with the following exceptions:
When the items being trashed are on an external drive, there may be a delay before the trashed items appear in the trash.
A trashed item’s put-back context-menu selection is typically not present, although the files can be put back with a simple copy and paste.
The following comment is from Shane regarding the recycleURLs method:
As you said, blocks are out, so recycleURLs_completionHandler_ will only work with missing value as the second argument. There’s no way you can get feedback without blocks here, although I suppose you could check the old path and be pretty safe in assuming that if it’s not there, it’s worked. The problem, though, is that the command is asynchronous, so you won’t know quite when to check.
The following demonstrates the two methods, but please use with test files only.
use framework "AppKit"
use framework "Foundation"
use scripting additions
-- An array of NSURLs
set theFiles to (choose file with multiple selections allowed) -- a list of aliases
set theFiles to current application's NSArray's arrayWithArray:theFiles -- an array of NSURLs
-- NSWorkspace's recycleURLs method
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
theWorkspace's recycleURLs:theFiles completionHandler:(missing value)
-- NSFilemanager's trashItemAtURL method
set fileManager to current application's NSFileManager's defaultManager()
repeat with aFile in theFiles
(fileManager's trashItemAtURL:aFile resultingItemURL:(missing value) |error|:(missing value))
end repeat
Why not use an application like Hazel and have it monitor a specific folder and delete all files that don’t have the .pdf extension? These actions are known as a rule. The application runs in the background.