Monday, November 20, 2017

#1 2017-10-16 06:32:42 pm

nellbern
Member
Registered: 2007-01-16
Posts: 120

Copy files from a text file times out

The following script which works perfectly if the chosen source folder does not contain a lot files but if I select a folder that contains lots, lots, lots of files then I get error "Finder got an error: AppleEvent timed out." number -1712. I added a random long timeout but I'm not sure how many hours I need to put in order for the script to work & I'm not even sure if this is the best solution bc the source folder contains around 1.2 TB.

Basically you choose the source folder, a destination, find all the strings from the text file, and copy the matches into the destination. Any suggestions are welcome how to get the same results

Applescript:

set files_source to (choose folder with prompt "Select the source folder") as text
set files_dest to (choose folder with prompt "Select a destination folder") as alias
set file_ref to (choose file with prompt "Select a plain text .txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as text)
with timeout of 60 * minutes seconds
   tell application "Finder"
       set theFiles to a reference to entire contents of folder files_source
       repeat with thisItem in theFiles
           set theFileName to name of thisItem as text
           if theFileName is in files_list then
               try
                   duplicate thisItem to files_dest
               on error
                   display dialog "Duplicate files named " & theFileName & ". Overwrite or skip?" buttons {"Cancel", "Overwrite", "Skip"} with icon caution
                   if button returned of the result is "Overwrite" then
                       duplicate thisItem to files_dest with replacing
                   end if
               end try
           end if
       end repeat
   end tell
end timeout

Model: iMac (Retina 5K, 27-inch, Late 2015)
AppleScript: 2.8.1 (183.1)
Browser: Safari 537.36
Operating System: Mac OS X (10.11.6)

Offline

 

#2 2017-10-17 02:04:36 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5177

Re: Copy files from a text file times out

The Finder unfortunately chokes on getting the entire contents of folder containing any more than a handful of items.

Here's a faster alternative:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set files_source to (choose folder with prompt "Select the source folder")
set files_dest to (choose folder with prompt "Select a destination folder")
set file_ref to (choose file with prompt "Select a plain text .txt file to read:" default location files_dest)
set files_list to paragraphs of (read file_ref as text)
set sourceFolderURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of files_source)
set destFolderURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of files_dest)
-- make lowercase list so case isn't an issue
set namesList to (current application's NSArray's arrayWithArray:files_list)'s valueForKey:"lowercaseString"
-- get all file URLs
set fileManager to current application's NSFileManager's defaultManager()
set theURLs to (fileManager's enumeratorAtURL:sourceFolderURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) errorHandler:(missing value))'s allObjects()
-- filter out those whose name matches
set thePredicate to current application's NSPredicate's predicateWithFormat:"lastPathComponent.lowercaseString IN %@" argumentArray:{namesList}
set theMatches to theURLs's filteredArrayUsingPredicate:thePredicate
-- loop through them
repeat with aURL in theMatches
   -- check if it already exists at destination
   set destURL to (destFolderURL's URLByAppendingPathComponent:(aURL's lastPathComponent()))
   if (destURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean then
       display dialog "Duplicate file named " & aURL's lastPathComponent() & ". Overwrite or skip?" buttons {"Cancel", "Overwrite", "Skip"} with icon caution
       if button returned of the result is "Overwrite" then
           -- delete old and copy
           (fileManager's removeItemAtURL:destURL |error|:(missing value))
           (fileManager's copyItemAtURL:aURL toURL:destURL |error|:(missing value))
       end if
   else
       (fileManager's copyItemAtURL:aURL toURL:destURL |error|:(missing value))
   end if
end repeat


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)