Search code examples
applescriptfinder

Applescript to move files to Newly created directory - but what if it exists?


I have put together this applescript that when a file is added to an action folder, it makes a new directory based on the first portion of the file name, makes a directory called 'images' inside, and then Moves that file to the 'images' folder inside. This all works GREAT, UNTIL. .dun-dun-dun.. The directory exists already!! (ohno_) . So I tried adding some If statements and it still is running the rest of the script fine (which adds in some other files and moves the completed folder set to another folder on my hard drive), but I can not get the file with the same first portion of the name to Move or copy over.. I'm reading a lot of posts here on If file exists, finding few on if Folder exists, and trying to figure it out, but can not get this to GO.. This is where I am at so far:

on run {input, parameters}
    tell application "Finder"
        set file_image to name of file input
    end tell
    tell application "Finder"
        set fileName to name of file input
        set AppleScript's text item delimiters to "."
        if number of text items of fileName > 1 then
            set fileName to text items 1 thru -2 of fileName as text
        end if
        set AppleScript's text item delimiters to "-"
        if number of text items of fileName > 1 then
            set fileName to text items 1 thru -2 of fileName as text
        end if
        fileName
    end tell
    
    tell application "Finder"
        activate
        set target of Finder window 1 to folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set thePath to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        if exists folder thePath then
            set source_folder to folder "new-actiontester" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
            set source_files to every file in source_folder
            set target_folder to folder "images" of folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
            repeat with i from 1 to number of items in source_files
                set source_file to (item i of source_files)
                move source_file to (target_folder) -- use "copy source_file to folder (target_folder as alias)" to copy the files
            end repeat
        end if
        make new folder at folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk with properties {name:fileName}
        set target of Finder window 1 to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        make new folder at folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk with properties {name:"images"}
        set source_folder to folder "new-actiontester" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set source_folder_list to folder "img-list" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set source_files to every file in source_folder
        set source_files_list to every file in source_folder_list
        set target_folder to folder "images" of folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set target_Go to folder "GO" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set folder_Number to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        set target_folder_list to folder fileName of folder "MAKER" of folder "Desktop" of folder "nathan" of folder "Users" of startup disk
        repeat with i from 1 to number of items in source_files
            set source_file to (item i of source_files)
            move source_file to (target_folder) -- use "copy source_file to folder (target_folder as alias)" to copy the files
        end repeat
        repeat with i from 1 to number of items in source_files_list
            set source_file_list to (item i of source_files_list)
            copy source_file_list to folder (target_folder_list as alias) -- use "copy source_file to folder (target_folder_list as alias)" to copy the files
        end repeat
        copy folder_Number to folder (target_Go as alias) -- use "copy source_file to folder (target_folder_list as alias)" to copy the files
    end tell

Solution

  • I find other people's folder actions a horror show to troubleshoot so for my own testing, I simply set input to an alias (and removed the run bookends). Presumably it won't be an issue here but as I have no idea whether it runs like this, I leave the on run as I found it.

    Essentially, I put your each of the make new folder commands inside its own try block. Hopefully that will resolve your issue.

    on run {input, parameters}
    tell application "Finder"
        set fileName to name of file input
        set AppleScript's text item delimiters to "."
        if number of text items of fileName > 1 then
            set fileName to text items 1 thru -2 of fileName as text
        end if
        set AppleScript's text item delimiters to "-"
        if number of text items of fileName > 1 then
            set fileName to text items 1 thru -2 of fileName as text
        end if
        set AppleScript's text item delimiters to ""
    end tell
    tell application "Finder"
        set mokr to (path to desktop) & "MAKER:" as text
        set target of Finder window 1 to mokr
        
        try
            make new folder at mokr with properties {name:fileName} with name
        end try
        
        set thePath to mokr & fileName as alias
        try
            make new folder at thePath with properties {name:"images"} with name
        end try
        
        if exists folder thePath then
            set source_folder to folder "new-actiontester" of desktop
            set source_files to every file in source_folder
            set target_folder to folder "images" of thePath
            
        repeat with source_file in source_files
            duplicate source_file to target_folder
        end repeat
    
        end if
    end tell
    end run
    

    Minor edits: In the top section, I simply added a reset to the delimiters as I used an as text which required they be defaults. It is a considered a good practice to reset them to something (defaults or previous) so you might consider that.

    The command to copy a file in applescript is actually duplicate.

    I used a variable to streamline your paths but it should work with your style as well.