My goal is to take a list of items off of Drive X:
0812345
0812367
0812347
out of a text document or excel file that is sent to me. I don’t care which its in. The list will contain different numbers of files each time. These are audio file names saved on a drive and the individuals requesting the numbers will put them in what ever file type i specify. Ok i want to take the files and copy them to my desktop and (preferably put them in a folder with the file number on it)[i can do this last part if i use one file].
On my desktop, I’d like to end up with either on folder with all of the files or as i said a folder named with each file. My job unfortunately consists of burning countless audio cds and right now i have to copy them to my desktop manually. This is simple if i construct an automator action and use the “Get Specified Finder Items”. I simply can’t figure out how to get items out of a list and into the automator action.
Ok here’s what i’ve the best i can figure out after a whole day. This was off another post but seems to do what i want. the problem is once again, i don’t know how to get from here to there. I think the beginning part of this script does what i want. it returns the list of the items in a file. But i don’t think applescript recognizes them as file names and I can’t figure out how to set them as such. Also how do i pass this info, file names, what have you, to something that i can do something with. the last line is simply my final choice of what i’m trying to do. I think that the tab file found the items i want based upon the error message, but i can’t seem to understand the dictionary so that i can get the identified files copied.
set theFile to choose file with prompt "Choose a tab-delimited text file"
set theData to paragraphs of (read theFile)
display dialog (count of theData)
copy theData to "Hd:/Users/Brock/Desktop"/applescript]
thanks in advance
It would be helpful to have some more information.
If the text file contains only file names, you need also the whole source path to copy the files.
And as you’re mentioning a tab-delimited file, which data are tab delimited?
In the last line there are 3 problems:
¢ AppleScript works only with HFS paths (colon delimited)
¢ the copy command copies a value into a variable, the proper command to copy files is duplicate or move
¢ Only the Finder (and System Events) can copy files
Getting closer. I still can’t get a working loop (repeat) though. This script does what i want but will not advance to the second item on the list. I don’t know why but my variable theRequest returns a 3 the first time i ask and a 3 the second time its asked in the script. But won’t advance (i think) or copy more than 1 file (actual result). It only copies the file untitled1.txt. Also it places a \ in front making it \untitled1.txt. Is this a left over from the POSIX naming?
The text file that i’m using is a text file with 3 items in it. They are essentially names of files (no path and look exactly as they are below). These correspond to 3 files with the same name on an external drive.
untitled1.txt
untitled2.txt
untitled3.txt
set fileList to {}
set theFile to choose file with prompt "Choose text file"
set AppleScript's text item delimiters to return
set fileList to (read theFile)
set AppleScript's text item delimiters to ""
display dialog fileList ---> displays list of items in file
set theRequest to count of paragraphs of fileList
display dialog of (count of paragraphs of fileList) ---> displays correct count of paragraphs
set dsk to path to desktop
set cd_name to "data"
try
repeat with n from 1 to theRequest
display dialog of (theRequest)
if (theRequest) = 0 then
exit repeat
end if
set file_to_copy to quoted form of POSIX path of ((cd_name & ":" & word n of fileList as text) & ".txt")
set target_file to quoted form of ((POSIX path of dsk & ":" & word n of fileList as text) & ".txt")
do shell script "cp " & file_to_copy & space & target_file
set n to n + 1
end repeat
end try
At least part of your problem is in the try block. When using a repeat, you do not need to set n to n+1; the repeat does it for you. Also, it would be cleaner to test the value of theRequest before even beginning the repeat. Try this:
try
if theRequest > 0 then
repeat with n from 1 to theRequest
display dialog theRequest
set file_to_copy to quoted form of POSIX path of ((cd_name & ":" & word n of fileList as text) & ".txt")
set target_file to quoted form of ((POSIX path of dsk & ":" & word n of fileList as text) & ".txt")
do shell script "cp " & file_to_copy & space & target_file
end repeat
end if
end try
Ok i’m thourghly confused. The value of theRequest is always 3. So i don’t know why i was testing for it. I’ve added a test for the variable n. It displays 1 and then two, but i only ever get one file outputted. It doesn’t appear to be overwriting the first file either. It just seems to quit. I have added 4 more files to the list and still only get one out. The repeat is killing me. I managed to “borrow” another loop and it works. I don’t understand what is different. Thoughts?
this works:
set fileList to {}
set theFile to choose file with prompt "Choose text file"
set AppleScript's text item delimiters to return
set fileList to (read theFile)
set AppleScript's text item delimiters to ""
display dialog fileList ---> displays list of items in file
set source to "data"
set file_list to every paragraph of fileList
tell application "Finder"
set folder_path to POSIX path of source
--Make a backup folder at the desktop if it does not exist
try
make new folder at desktop with properties {name:"Backup"}
end try
--Copy the files
repeat with file_ in file_list
set name_ to quoted form of (folder_path & "/" & file_ as string)
set copy_script to "cp " & name_ & " ~/Desktop/Backup"
display dialog "Now copying " & name_ giving up after 1
do shell script copy_script
end repeat
end tell
and this only copies the first file
set fileList to {}
set theFile to choose file with prompt "Choose text file"
set AppleScript's text item delimiters to return
set fileList to (read theFile)
set AppleScript's text item delimiters to ""
display dialog fileList ---> displays list of items in file
set theRequest to count of paragraphs of fileList
display dialog of (count of paragraphs of fileList)
set dsk to path to desktop
set cd_name to "data"
try
if theRequest > 0 then
repeat with n from 1 to theRequest
display dialog theRequest
set file_to_copy to quoted form of POSIX path of ((cd_name & ":" & word n of fileList as text) & ".txt")
set target_file to quoted form of ((POSIX path of dsk & ":" & word n of fileList as text) & ".txt")
do shell script "cp " & file_to_copy & space & target_file
display dialog "Now copying " & file_to_copy giving up after 1
end repeat
end if
end try
I’m missing what is material in the changing of the wording.
To get a word off a list of words I use this AppleScript:
tell application "TextWrangler"
activate
select word 1 of text window 1
cut selection
end tell
I use TextWrangler for the list because “word 1” includes the paragraph return at the end of a line. So the next time it runs it will get the next word in the list. If I use MSWord, it only gets the word and leaves the return mark so the next time I get an empty space.
The document with the list has to be open when running this script. The “cut selection” puts the word in the clipboard for Automator to paste into a field in the database I’m working on.
Here’a a script for MSWord:
tell application “Microsoft Word”
activate
select word 1 of active document
cut object word 1 of selection
end tell
So what I do is have the script get the word, Automator puts it in the database I’m working on and does some more stuff. I then repeat the whole action for the next word on the list. I’m using the Automator Loop Utility to do the looping.