Help with textedit script please!!!

I have a DVD with say 15000 files on it example name of just one file would be: HB-10_0154.pdf
i then have a textedit doc with a list of nearly 6000 files of the 15000 on the DVD the equivelent file name in the list to the one on the DVD is:
HB-10_0154 , Cork_Tiles_Flooring_Storyboard , 2 , 02c05 , 97598 , 97598 , 91017
What i need to do is select these 6000 files and copy them off the CD to another folder using the info in the textedit doc
Anyone with any ideas on how i would do this would be gratefully appreciated

cheers

pidge:

Under what format does the TextEdit document contain the filenames? Is each filename on a separate line, or are they comma separated? Is it the same all the way through the entire document?

its just a standard textedit document,
there all on separate line for example:
HB-10_0154 , Cork_Tiles_Flooring_Storyboard , 2 , 02c05 , 97598 , 97598 , 91017
HB-10_0211_E_R1 , A3_Value_laminate_flooring_Ä7.49 , 2 , , 101219 , 101219 , 101219
HB-10_0384_E , A3_Ceramic_Glueless_Flooring_Ä24.49 , 2 , , 99702 , 99702 , 99702
HB-10PC_0010 , Suckers_Singles , 2 , 04a01,pr08a01 , 93127 , 93127 , 92157
the files on the DVD for these 4 items would be:
HB-10_0154.pdf
HB-10_0211_E_R1.pdf
HB-10_0384_E.pdf
HB-10PC_0010.pdf

i just wanna move them to another folder
this is what i have so far:
tell application “TextEdit”
set theDoc to document 1
set paracount to (count of paragraphs of text of theDoc)
repeat with i from 1 to paracount
set HBList to word 1 of paragraph i of theDoc as string
set HBList1 to word 2 of paragraph i of theDoc as string
set thefile to HBList & “-” & HBList1 & “.pdf”
end repeat
end tell

Hope this helps
cheers

i think i might have it with this: but would appreciate it if anyone could tell me a better way this worked on 4 files that i copied to a test folder on my hard disk before
they were moved.

cheers

Sorry Helps if you post it!!!
tell application “TextEdit”
set theDoc to document 1
set paracount to (count of paragraphs of text of theDoc)
repeat with i from 1 to paracount
set HBList to word 1 of paragraph i of theDoc as string
set HBList1 to word 2 of paragraph i of theDoc as string
set thefile to HBList & “-” & HBList1 & “.pdf”
tell application “Finder”
activate
if exists file thefile of folder “test” of folder “Desktop” of folder “steve” of folder “Users” of startup disk then
move the file thefile of alias “Donatello:Users:steve:Desktop:test:” to alias “Donatello:Users:steve:Desktop:HB PDF’s:”
end if
end tell
end repeat
end tell

Hi, pidge1.

Rather than extract each paragraph individually from the TextEdit document and deal with each file individually in the Finder, it would be much quicker to extract all the text in one go, doctor it in AppleScript, and then get the Finder to move all the matching files in one go. There used to be a limit of about 4000 on the number of elements that could be extracted from a string in one go (I don’t know if it’s still true), so the following deals with 3900 paragraphs at a time. I’m afraid I’m not in a position to be able to test it with 6000 files. :slight_smile:

-- Using a handler keeps all the variables local, which stops their
-- final values being saved back into the script file afterwards.
on main()
	-- A script object to be referenced for fast list access.
	script o
		property fileNames : missing value
	end script
	
	tell application "TextEdit" to set theText to text of front document
	
	-- Deal with 3900 or less paragraphs at a time.
	set paraCount to (count theText's paragraphs)
	repeat with x from 1 to paraCount by 3900
		set y to x + 3899
		if (y > paraCount) then set y to paraCount
		
		set o's fileNames to paragraphs x thru y of theText
		
		-- Replace each paragraph in the list with the associated file name.
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to " ," -- space & comma
		repeat with i from 1 to (y - x + 1)
			set item i of o's fileNames to (text item 1 of item i of o's fileNames) & ".pdf"
		end repeat
		set AppleScript's text item delimiters to astid
		
		-- Move the 3900 or less matching files.
		tell application "Finder" to move (files of disk "My DVD" whose name is in o's fileNames) to folder "Destination folder"
	end repeat
	
end main

main()

Thanks i’ll give this a go!!!

Nigel

Run your script looked to be working fine until the file moving part, got an apple event timed out error
is this something to do with it running through the 15000 or so files to find the right ones and it doesn’t have enough time
or is that a shot in the dark…

Hi, pidge1.

I think your analysis is right. It would take a long time just to identify the files from amongst the 15000, let alone copy them to another folder. You could try setting a timeout of, say, an hour. This simply increases the time that the script’s prepared to wait for the “OK, done” from the Finder before assuming that something’s gone wrong.

-- Move the 3900 or less matching files.
with timeout of 3600 seconds
	    tell application "Finder" to move (files of disk "My DVD" whose name is in o's fileNames) to folder "Destination folder"
end timeout

If that doesn’t help, it may prove necessary to move each file explicitly, one at a time. This would take a while, but at least it would be a steady flow rather than a bottleneck.

-- Using a handler keeps all the variables local, which stops their
-- final values being saved back into the script file afterwards.
on main()
	-- A script object to be referenced for fast list access.
	script o
		property fileNames : missing value
	end script
	
	tell application "TextEdit" to set theText to text of front document
	
	-- Deal with 3900 or less paragraphs at a time.
	set paraCount to (count theText's paragraphs)
	repeat with x from 1 to paraCount by 3900
		set y to x + 3899
		if (y > paraCount) then set y to paraCount
		
		set o's fileNames to paragraphs x thru y of theText
		
		-- Derive a file name from each paragraph in the list and move that particular file.
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to " ," -- space & comma
		repeat with i from 1 to (y - x + 1)
			set fileName to (text item 1 of item i of o's fileNames) & ".pdf"
			tell application "Finder"
				try
					move file fileName of disk "My DVD" to folder "Destination folder"
				on error msg
					-- The file isn't on the DVD or its name already exists at the destination.
					display dialog msg buttons {"OK"} default button 1 with icon note
				end try
			end tell
		end repeat
		set AppleScript's text item delimiters to astid
		
	end repeat
	
end main

main()

You can zap the ‘display dialog’ line if you don’t want any errors reported while moving the files.

By the way, as I expect you’ll have gathered, I’ve assumed that all 15000 files are in the same place on the DVD.

Nigel

Thanks for the scripts i have added the timeout and am still getting an error not in applescript in the finder(spinning beach ball) finder not responding.
But thats cool i’m gunna go with the other script,
if your interested the script i wrote took just over 15 mins to move all files and yours took just under 10 mins.
i’m still new to scripting so gunna study they way you wrote that, and hopefully learn from it, never seen that line with (script o) in it before can you advise me on where i might get a book or something
where it tells you this stuff, most scripting books i know don’t cover the syntax i see knocking around on this forum.

really appreciate the help.Thanks

Hi Nigel,

Maybe you can cut down the move time using ignoring applicaiton responses with the Finder ‘move’ command.

gl,

For openers, Pidge, try Danny Goodman’s AppleScript Handbook - a $15 download in PDF format.

The reason for using a script within a script here and referring back to a property of that script in the main script is that AppleScript stores that script property differently than it does a simple list. For large lists there is a significant benefit in the time it takes to alter the list or refer to its elements.

Sorry to hear that. Maybe the Finder just can’t handle that many files a time.

I’m glad it was at least partly effective. :slight_smile:

You’re at the cutting edge here. :wink: Seriously, though, I don’t know if any of the books mention the script object speed trick or not. The ‘script o’ block defines a script object called ‘o’ with a property called ‘fileNames’, which later gets set to the list of lines taken from TextEdit. It’s an incidental and unoffical by-product of the way AppleScript lists are implemented that if you use a ‘reference’ (an expression like ‘o’s fileNames’ of ‘fileNames of o’) to refer to a list, access to the list’s items is much faster than if you just use a variable name (‘fileNames’). The script object here gives ‘fileNames’ something to belong to so that a reference can be used.

I wondered about that, but wasn’t able to test it with a large number of files. My concern was that the loop would go round and round pouring ‘move’ instructions on the Finder without waiting for it to finish any of them.