MacOS – How to create an apple script that changes the destination folder of airdropped files

airdropapplescriptmacos

I've downloaded the following apple script, which moves airdropped files to a specified folder (which I'll refer to as the Airdrop folder):

property AIRDROP_FOLDER : "Macintosh HD:Users:pschorn: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'"

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 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 followed the instructions on this website to set it up as a folder actions script.

However, if I move an airdropped file out of my newly created airdrop folder and back into my downloads folder, it gets automatically put BACK in the airdrop folder. I don't want this to happen.

I found that after applying this terminal command xattr -d com.apple.quarantine [file path of airdropped file], the file will no longer get moved back into my airdrop folder after I move it out.

MY question is: How do I integrate this command into the above apple script so that the airdropped files do not automatically get moved back to my airdrop folder after I move them out of it?

Best Answer

If you are sure of the provenance of the AirDropped files and are happy to remove the quarantine extended attribute you can remove it as you say with xattr. In AppleScript you can do this using do shell script and passing the quoted path in alias form.

removeQuarantine(quoted form of (POSIX path of (current_item as alias)))
on removeQuarantine(file_path_alias)
    do shell script "xattr -d com.apple.quarantine " & file_path_alias
end removeQuarantine

In your script you want to call this before the file is moved so your script would look as below (remember to update the AIRDROP_FOLDER to your required path).

The script will now check files for com.apple.quarantine equal to 59 and if so remove the attribute before moving the file.

Therefore if you then move the file back to ~/Downloads it will not be moved again as the com.apple.quarantine attribute is no longer there.


property AIRDROP_FOLDER : "macOS:Users:hali:Desktop:Test"
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'"

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
            removeQuarantine(quoted form of (POSIX path of (current_item as alias)))
            moveFile(current_item, alias 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

on removeQuarantine(file_path_alias)
    do shell script "xattr -d com.apple.quarantine " & file_path_alias
end removeQuarantine

Note : This script will overwrite files of the same name in the destination without prompting. If this is a concern there is a forked version by user SuraAzure on github that appends a sequence number to the filename before moving if a file with the same name already exists in the destination directory.