MacOS – Applescript exits return loop before complete

applescriptbashmacos

I have to go through a stack of hard drives & usb keys looking for old quickbook files. So, I've written an applescript which triggers when a new drive is mounted. It then searches the drive for qbb or qbo files and copies them to a folder on the main drive.

I have it working almost perfectly but, for the life of me, I cannot get the repeat loop to run past the first item in the list. So, it grabs qbb files, but not qbo files. I know it is failing because I trigger it with a USB kye that has dummy qbo and qbb files on it.

I know I am missing something obvious, but keep racking my brain to no avail.

Here is my script. Can someone please give me sanity check (and solution)?

Thanks!

on adding folder items to thisFolder after receiving addedItems

   # set the path for the files to be copied too.
   set thePath to ((path to home folder as text) & "00_QBB:")
   if not pathExists(thePath) then
       display dialog "Could not find the path. Files have not been copied."
       return
   end if

   try
       # get name of Loaded Drive
       set loadedDriveName to item 1 of addedItems

       # set paths for source source drive
       set loadedDrive to POSIX path of loadedDriveName 

       # create folder, named for Loaded Drive, for inbound copies
       set thePath to thePath & loadedDriveName
       set thePath to POSIX path of thePath
       set makeFolder to "mkdir " & thePath 
       # display dialog makeFolder
       do shell script (makeFolder)

       # set search criteria
       set qbFileTypes to {"qbb", "qbo"}

       # lookthrough drive for desired file types to find and copy
       repeat with n from 1 to (count of qbFiletypes)
           set qbCount to (count of qbFiletypes)
           set qbCurrentType to (item n of qbFiletypes)
           # display dialog qbCurrentType & " + " & qbCount
           set grab_qbb to "find " & loadedDrive & " -iname \"*" & qbCurrentType & "*\" -exec cp {} " & thePath & " \\;"
           # display dialog grab_qbb
           do shell script (grab_qbb)
       end repeat

       on error
           display dialog "Could not copy files."
       return
   end try

end adding folder items to

on pathExists(thePath) -- pathExists("path:to:folder: OR :file")
   try
       get thePath as alias
       return true
       on error
       return false
   end try
end pathExists

Best Answer

I would have gone about this a bit differently.

First, I'd use Disk Arbitrator so the external disks would mount read-only and not preform any Spotlight indexing. There's no need to allow indexing if the target is just .qbb and .qbo files to be found using the find command.

Second, I'd use an Automator Folder Action with a Run Shell Script action, as IMO it's just much easier to code.

The following example bash code preforms these tasks:

  • Creates all destination directories as needed.
  • Handles disks with multiple volumes.
  • Finds the .qbb and .qbo files and copies them.
  • Ejects the target disk when finished copying.
  • Notifies the process has completed.

Also, as an Automator Folder Action there is a icon on the menu bar which aside from being a visual indicator a process is running, it also allows one to terminate the process if necessary.

Setup the Automator Folder Action as shown below:

Automator Folder Action

        Hint: Open image in new tab to clearly see settings.


When the process is finished it displays the following notification and makes a sound, which can be omitted if not wanted.

Notification Image


Example bash code:

p="$HOME/00_QBB"

[ ! -d "${p}" ] && mkdir "${p}"

for d in "$@"; do
    n="$(basename "$d")"
    mkdir -p "${p}/${n}"
    find "$d" -type f \( -iname '*.qbb' -o -iname '*.qbo' \) -exec cp -an {} "${p}/${n}" \; 
done

sleep 3
diskutil eject "$1"

osascript -e 'display notification "Insert another disk to start new search..." with title "QuickBooks File Recovery" subtitle "The find command has completed." sound name "Purr"

Obviously you might be inserting disks that you do not need to or want to search, so you can use Folder Action Setup to enable/disable this easily.

I use Spotlight to bring up Folder Action Setup and then check/uncheck the checkboxes as necessary.

Folder Action Setup