0
votes

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.

2
As to the 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. :)user3439894
You aren’t running the added code. The adding 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
The answer from User3439894 cleans up the code if that's what you like, however it still does not run correctly in the 'Folder Action' scripts and is not called when the action is run. Like my current script it works in Script editor, but not in real use. Thanks for the additional programming ideas.Diveboss
red_menace do you mind elaborating? How to I run the added code. Like I said I'm an amateur at coding. Always looking to learn. What kind of statement? Where does it go? How do I use a call handler?Diveboss
Thanks again for the input. I played more, read more and put the added code in the correct place in the script so that it would get called. - Problem solved!Diveboss

2 Answers

0
votes

This was my resolve and gives what I wanted. Again thanks everyone that responded. This is now shown as formatted correctly. I didn't read the first time how to enter the code block on this website, so it appeared I had not formatted it correctly. That was not the case.

property ORIGINAL_AIRDROP_FOLDER : "macOS SSD:Users:KerryVogt:Downloads"
property NEW_AIRDROP_FOLDER : "macOS SSD:Users:KerryVogt: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'"

on adding folder items to this_folder after receiving added_items
    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 "Files have been saved in the folder AirDrop, in the Download folder."
        else
            say "An Air Drop folder will be added to the download folder to complete this action."
            make new folder at inputFolder with properties {name:"AirDrop"}
            say "Air Drop folder has been added"
        end if
    end tell

    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
0
votes

i have mojave 10.14.6 for me line

moveFile(current_item, **alias** NEW_AIRDROP_FOLDER)

doesn't work. no errors, just quit script. i just removed word "alias" and now it works:

moveFile(current_item, NEW_AIRDROP_FOLDER)