Newbie apple script question

I have generated a .txt file which contains a long list of directories on a file server, I wish to copy them to a separate folder

{each line has a directory only, eg:

./Andrew Hill/Point of Departure
./Andrew Lloyd Webber/Highlights from The Phantom of The Opera

.}

Is there a way to get applescript to use the contents of this file to generate the copy commands?

Hi,

assuming the paths in the text file are full paths you could use this


set destinationFolder to POSIX path of (choose folder)
set theDirectories to paragraphs of (read file "disk:path:to:file.txt")

repeat with oneDirectory in theDirectories
	do shell script "/usr/bin/ditto " & quoted form of oneDirectory & space & quoted form of destinationFolder
end repeat

If they are not full paths you must build them

Thanks for your rapid reply, I’ve tried to run your script, to no avail. :frowning:

To be clear: what is the purpose of the folder being requested? Is this the destination folder for the list of directories to copy? And if so, how do I specify the .txt file which contains the list to be copied?

Some options:

-- diskname:subfolders:filename
set v to alias "Macintosh HD:users:username:desktop:afile.txt"
-- or
set v to (((path to desktop) as text) & "afile.txt")
-- or
set v to (choose file)

destinationFolder is the separate folder you mentioned in your first post.
You can hard-code the path with a POSIX path (slash separated) like

set destinationFolder to "/Users/myUser/separateFolder"

The source path must be a HFS path (colon separated) starting with the disk name.

If you’re going to use one of ief2’s solutions which specify the path as an alias, you must omit the keyword file, for example

set theDirectories to paragraphs of (read alias "Macintosh HD:Users:username:Desktop:afile.txt")

Ok, progress…of a sort. Here’s how I’ve modified your script:

set destinationFolder to POSIX path of (choose folder)
set theDirectories to paragraphs of (read alias "Macintosh HD:Users:glenn:Desktop:apeonlydirs_v1.0.txt")


repeat with oneDirectory in theDirectories
	do shell script "/usr/bin/ditto " & quoted form of oneDirectory & space & quoted form of destinationFolder
end repeat

Now I manage to get to the command line ‘repeat with…,’ only to get error message:

error “ditto: can’t get real path for source” number 1.

The file ‘apeonlydirs_v1.0.txt’ looks like this:

/Volumes/media/Music/MC\ Files/Ape/Alanis Morissette/Jagged Little Pill
/Volumes/media/Music/MC\ Files/Ape/Andrew Hill
/Volumes/media/Music/MC\ Files/Ape/Andrew Hill/Point of Departure
/Volumes/media/Music/MC\ Files/Ape/Andrew Lloyd Webber
/Volumes/media/Music/MC\ Files/Ape/Andrew Lloyd Webber/Highlights from The Phantom of The Opera
/Volumes/media/Music/MC\ Files/Ape/B-Tribe
/Volumes/media/Music/MC\ Files/Ape/B-Tribe/Fiesta Fatal!

…where the directory paths were lifted from the Finder (files stored on a remote NAS), where media is a mounted share. Do I need to list the explicit path? The share was mounted using AFP

If your path all ready has the '' before every space, I think getting rid of the quoted form of will make your script work:

set destinationFolder to POSIX path of (choose folder)
set theDirectories to paragraphs of (read alias "Macintosh HD:Users:glenn:Desktop:apeonlydirs_v1.0.txt")


repeat with oneDirectory in theDirectories
	do shell script "/usr/bin/ditto " & oneDirectory & space & quoted form of destinationFolder
end repeat

Hope it works,
ief2

Update - Figured it out with a bit of sleuthing…seems to work ok now, thanks! :smiley:

One or two follow on questions if I might:
i) What if I wanted to create a subdirectory similar to the subdirectories listed in the input file?

EG, If the target directory is “./Files_to_Convert”

and my input file contains the following subdirectories:


/Volumes/media/Music/MC Files/Ape/Counting Crows/August and Everything After
/Volumes/media/Music/MC Files/Ape/Cowboy Junkies/Black Eyed Man

your script currently dumps all files within these subdirectories into “./Files_to_Convert”

How can I put them into the corresponding subdirectories:


./Files_to_Convert/Counting Crows/August and Everything After
./Files_to_Convert/Cowboy Junkies/Black Eyed Man

?

Also
ii) is there anyway to skip copying a folder if it contains subfolders?

Eg, if there are two lines in the input file
‘./Cowboy Junkies’
‘./Cowboy Junkies/Black Eyed Man’

can the script ignore the first instance? (The file is the product of a recursive find, and includes some parent directories under the grandparent which is really just an organizing folder).

Thanks very much in advance,
G

It does! One question - occasionally, my input file specifies a non existent directory (dunno how it happened!), and I get message “ditto: can’t get real path for source.” How can I prevent the script from stopping upon getting this message, and move onto the next line?