Search code examples
applescriptfinder

Trouble with script moving files to a newly created folder in Applescript


I am having trouble with what I think is a pretty straightforward task, but cannot seem to get my script to work correctly. I have found a lot of help through the forums with regards to the individual routines used, but it still seems to fail.

In short, what I would like to do is monitor a folder for new files being added. Once a batch of files are added (every other day or so), it will create a folder in another directory with the new folder name being the current date, move those files to the new directory, and then execute a simple bash script which uses the new directory name as an argument.

My script compiles ok, but once files are added it only creates the new folder and nothing else. I appreciate any help.

property the_sep : "-"

on adding folder items to my_folder after receiving the_files

tell application "Finder"
    (* First create a new folder with name of folder = current date *)
    set the_path to (folder "qa" of folder "Documents" of folder "ehmlab" of folder "Users" of disk "Macintosh HD")
    set the_name to (item 1 of my myDate())
    set the_name to (the_name & the_sep & item 2 of my myDate())
    set the_name to (the_name & the_sep & item 3 of my myDate())
    make folder at the_path with properties {name:the_name}
    set newDir to the_path & the_name
end tell

(* Next, move the newly added files to the source into the newly created date folder *)
repeat with i from 1 to number of items in the_files
    tell application "Finder"
        try
            set this_file to (item i of the_files)
            move file this_file to folder newDir
        end try
    end tell
end repeat

do shell script "qc.sh " & newDir

end adding folder items to

on myDate()
set myYear to "" & year of (current date)
set myMth to text -2 thru -1 of ("0" & (month of (current date)) * 1)
set myDay to text -2 thru -1 of ("0" & day of (current date))
return {myYear, myMth, myDay}
end myDate

Solution

  • The failure reason are different path styles.

    AppleScript uses HFS paths (colon separated).
    UNIX uses POSIX paths (slash separated).

    The solution is to coerce the HFS path string to POSIX path

    do shell script "qc.sh " & quoted form of POSIX path of newDir
    

    This is a shorter version of the script using the shell also for the time stamp and for creating the directory.

    on adding folder items to my_folder after receiving the_files
    
      (* Next, move the newly added files to the source into the newly created date folder,
      "path to documents" is a relative path to the documents folder of the current user *)
    
      set baseFolder to (path to documents folder as text) & "qa:"
      set timeStamp to do shell script "date +%Y-%m-%d"
      set newDir to baseFolder & timeStamp
      do shell script "mkdir -p " & quoted form of POSIX path of newDir
    
      (* Next, move the newly added files to the source into the newly created date folder *)
    
      repeat with aFile in the_files
        tell application "Finder"
          try
            move aFile to folder newDir
          end try
        end tell
      end repeat
    
      do shell script "qc.sh " & quoted form of POSIX path of newDir
    
    end adding folder items to