Hi ndog. Sorry about the delay getting back.
I’m not in a position to recommend a good AppleScript tutorial as I’ve not read any of the recent books. Hamish Sanderson & Hanaan Rosenthal’s “Learn AppleScript” seems to be well thought of. I’ve also heard praise for Matt Neuburg’s “AppleScript: The Definitive Guide!” over the years, although the reviews on Amazon don’t seem totally convinced it’s good for beginners. There’s also “AppleScript 1-2-3” by Sal Saghoian (Apple’s Automation Product Manager, no less) and Bill Cheeseman. The first chapter of this is reproduced here. It looks like a good place to start. Once you understand the basics, a handy reference is Apple’s own AppleScript Language Guide, often just called ASLG. There’s a link to download a PDF of it in the top right hand corner of the Web page.
As ASLG’s Introduction explains, the core AppleScript language doesn’t have many commands of its own. Much of the scripting you’ll do will be with “plug-in” commands provided by scriptable applications themselves ” commands relevant to what they do and which usually only they can understand. AppleScript can also get additional commands from plug-ins called OSAXen, it can call executables such as those in the Unix operating system, and it’s recently acquired an extension called AppleScript Objective-C which gives access to the Objective-C API in Mac OS itself. So the subject can seem quite intimidating. But you can actually do a lot just knowing the basics. Once you’ve got those, you can explore further at your leisure in any direction you find useful or interesting.
But to your current task of interest. You originally asked about the possibility of copying a file from a location worked out from its name to a certain folder. This would be simply:
-- Preset the HFS (colon separated) path to the root folder. (Use the actual one, not this!)
property rootPath : "Volume name:any:other:folders:Root folder:"
-- Ask the user for a file name.
set fileName to text returned of (display dialog "Please enter the name of the file you want to copy from the server." default answer "xxxx-xxxx.jpg")
-- Ask the user for the destination folder.
set destinationFolder to (choose folder with prompt "Choose a destination folder for the copied files.")
-- Work out the path to the file.
set filePath to rootPath & text 1 thru 2 of fileName & ":" & text 3 thru 4 of fileName & ":" & fileName
-- Tell the Finder to 'duplicate' (a Finder command) the file to the destination folder, overwriting if there's already one there with the same name.
tell application "Finder" to duplicate file filePath to destinationFolder replacing yes
It would look even simpler without the explanatory comments.
If instead you want to copy several files whose names are listed one-per-line in a text file called, say, “File list.txt” on your desktop, you need to read the text file and to act on each line in it:
-- Preset the HFS (colon separated) path to the root folder.
property rootPath : "Volume name:any:other:folders:Root folder:"
-- Get the paragraphs of the text file, assumed to be called "File list.txt" and on the user's desktop.
set fileList to paragraphs of (read file ((path to desktop as text) & "File list.txt"))
-- Ask the user for the destination folder.
set destinationFolder to (choose folder with prompt "Choose a destination folder for the copied files.")
-- Work out the path for each file name (paragraph) and tell the Finder to duplicate the file to the destination folder.
repeat with fileName in fileList
set filePath to rootPath & text 1 thru 2 of fileName & ":" & text 3 thru 4 of fileName & ":" & fileName
tell application "Finder" to duplicate file filePath to destinationFolder replacing yes
end repeat
It may be prudent to guard against the possibility of the text file containing an empty line or a non-existent file name:
property rootPath : "Volume name:any:other:folders:Root folder:"
set fileList to paragraphs of (read file ((path to desktop as text) & "File list.txt"))
set destinationFolder to (choose folder with prompt "Choose a destination folder for the copied files.")
repeat with fileName in fileList
-- Only act if there are characters in this line.
if ((count fileName) > 0) then
set filePath to rootPath & text 1 thru 2 of fileName & ":" & text 3 thru 4 of fileName & ":" & fileName
try -- If anything goes wrong, catch the error.
tell application "Finder" to duplicate file filePath to destinationFolder replacing yes
on error
-- The only thing likely to go wrong here is the file not existing in the calculated location.
display dialog "There was some problem duplicating the file " & filePath & ". It may not exist." buttons {"Stop", "Skip"} default button "Skip" cancel button "Stop" with icon caution
end try
end if
end repeat
You asked about the possibility of a droplet. I presume you’d want to drag a text file containing the file names onto it. A droplet has to have an ‘open’ handler and must be saved as an application (using the “File Format” pop-up menu in the “Save” dialog).
property rootPath : "Volume name:any:other:folders:Root folder:"
-- The droplet's 'open' handler. 'theseItems' will receive a list of all the files and folders involved in the drop, so it must be treated as a list even when there's only one item.
on open theseItems
set destinationFolder to (choose folder with prompt "Choose a destination folder for the copied files.")
-- Repeat with every dropped item.
repeat with thisItem in theseItems
-- Only act if this item's likely to be a text file.
if ((thisItem as text) ends with ".txt") then
-- Get the paragraphs of this text file.
set fileList to paragraphs of (read thisItem)
repeat with fileName in fileList
if (count fileName) > 0 then
set filePath to rootPath & text 1 thru 2 of fileName & ":" & text 3 thru 4 of fileName & ":" & fileName
try
tell application "Finder" to duplicate file filePath to destinationFolder replacing yes
on error
display dialog "There was some problem duplicating the file "" & filePath & "". It may not exist." buttons {"Stop", "Skip"} default button "Skip" cancel button "Stop" with icon caution
end try
end if
end repeat
else
-- If this item doesn't look like a text file, rub the user's nose in it. ;)
display dialog "The item "" & thisfile & "" doesn't have a ".txt" extension. It may not be a text file." buttons {"Stop", "Skip"} default button "Skip" cancel button "Stop" with icon caution
end if
end repeat
end open
If in fact the text file contains a couple of hundred names, it would probably be faster to one of the Unix methods rather than the Finder, but unless you know Unix, the script would be harder to understand.