The attached script was created by merging two script ideas. The concept is to move all Airdrop files to a sub-fold "AirDrop" in the Downloads folder automatically using the 'Folder Action' function. However, the 'Airdrop' folder must be present for that part of the script to function and sometimes, while cleaning up, I delete it along with all other downloads.
So, I added a little more code (mind you I'm an amateur at coding) to have it check for the "AirDrop" folder and create it if missing.
This part works when I test it in Script Editor and lets me know if the 'Airdrop' folder exists and if not it is created.
When attached to the download folder as an action script the recent added code appears to be skipped. All air drop files go into the download folder or airdrop sub-folder if present but the script does appear to check for the AirDrop folder or not "say" whether there is a "AirDrop" folder or not. It will not create it if it is missing.
property ORIGINAL_AIRDROP_FOLDER : "macOS SSD:Users:USerName:Downloads"
property NEW_AIRDROP_FOLDER : "macOS SSD:Users:UserName:Downloads:AirDrop"
property QUARANTINE_KEY : "59"
property GET_QUARANTINE_COMMAND_START : "ls -l -@ '"
property GET_QUARANTINE_COMMAND_END : "' | tr '\\n' ' ' | sed 's/.*com\\.apple\\.quarantine\\s*\\(\\d*\\)/ \\1/' | awk '{$1=$1};1'"
------ Added to check for and create missing sub-folder---------
tell application "Finder"
set inputFolder to (ORIGINAL_AIRDROP_FOLDER) as Unicode text
set convertedFolderPath to (ORIGINAL_AIRDROP_FOLDER) & ":" & ("AirDrop")
if (exists (folder convertedFolderPath)) then
say "Air Drop folder exists"
else
say "Air Drop folder does not exist"
make new folder at inputFolder with properties {name:"AirDrop"}
say "Created Air Drop folder"
end if
end tell
On adding folder items to this_folder after receiving added_items
repeat with i from 1 to length of added_items
set current_item to item i of added_items
set quarantine_type to getQuarantineType(POSIX path of current_item)
if quarantine_type is equal to QUARANTINE_KEY then
moveFile(current_item, alias NEW_AIRDROP_FOLDER)
end if
end repeat
end adding folder items to
on moveFile(move_file, destination_dir)
tell application "Finder"
move move_file to destination_dir with replacing
end tell
end moveFile
on getQuarantineType(file_path)
return do shell script GET_QUARANTINE_COMMAND_START & file_path & GET_QUARANTINE_COMMAND_END
end getQuarantineType
I would like it to run the part of the script to create the missing folder when required.
tell application "Finder"
block after the comment------ Added to check for and create missing sub-folder---------
, I'd delete it an simply use:do shell script "mkdir -p \"$HOME/Downloads/AirDrop\""
This way if the folder doesn't exist, it's created, and if it does exist, nothing changes! No need for all the rigmarole and malarkey. :) – user3439894adding folder items
handler is what gets called for a folder action, so you need to add statements or call a handler for your added check from there. – red_menace