Search code examples
applescriptsubdirectory

Applescript: How do I set a 'move to' folder based on the filename to sort files into Alphabetical folders?


I'm trying to create a 'folder action' that will organise new files added into sub-folders for each letter of the alphabet based on the first letter of the filename.

I wrote the following code which works to move everything added into the subfolder "A" as a starting point, but now I've reached a block.

on adding folder items to thisfolder after receiving added_items
repeat with i from 1 to number of items in added_items
    set this_item to (item i of added_items)
    set DestinationFolderName to "A"
    tell application "Finder"
        move this_item to folder DestinationFolderName of thisfolder with replacing
    end tell
end repeat
end adding folder items to

My dilemma now is how to change set DestinationFolderName to "A" to be reactive to the filename of this_item.

I tried set DestinationFolderName to text 1 thru 1 of filename and a few other iterations but the issue seems to be finding the filename to extract the first letter.

Any help would be much appreciated.


Solution

  • As mentioned in the answer provided by @vadian, it is never a good idea to move files into sub-folders of folders with attached folder action commands.

    For purposes of this script I created a new (move to) folder named “New Destination” on my Desktop, which will contain the subfolders “A”, ”B”, “C” etc…

    If the file being moved, for example starts with the letter “D” and there is no corresponding move to destination folder named “D”, within the folder named “New Destination”, Then folder “D” will be created automatically

    SATIMAGE Scripting Addition Was used in this script in the event that the first letter of the name of the file being moved is lowercase, the corresponding folder that may be created will be changed from lower to uppercase

    property DestinationFolderName : missing value
    property moveToFolder : (path to desktop folder as text) & "New Destination"
    
    on adding folder items to this_folder after receiving these_items
        repeat with i from 1 to number of items in these_items
            set this_item to item i of these_items
            tell application "Finder"
                set fileName to name of item this_item
                set DestinationFolderName to character 1 of fileName
            end tell
    
            -- SATIMAGE scripting addition is needed for next command
            set DestinationFolderName to uppercase DestinationFolderName
    
            tell application "Finder"
                set checkExists to moveToFolder & ":" & DestinationFolderName
                try
                    if not (exists of checkExists) then
                        make new folder at moveToFolder ¬
                            with properties {name:DestinationFolderName}
                    end if
                end try
                move this_item to folder DestinationFolderName of folder moveToFolder with replacing
            end tell
        end repeat
    end adding folder items to