loops

hi all,
trying to work with loops a little bit. I am trying to write a sript to go into folders and merge xml files in subfolders into one big file.
I am good at getting the first file, but can’t grab the 2nd, 3rd, …etc.
here the part os the code that suppost to handle that.

tell application “Finder” to set other_files to every file of some_folder
repeat with whatever_item in other_items
– return first file that is an xml file
set my_info to info for (whatever_item as alias)
if name extension of my_info is “XML” then
return whatever_item
end if
end repeat

any suggestion or help will be greatly appreciated.

bill

Hi Bill,

Something like this:

set some_folder to choose folder
tell application “Finder”
try
set other_files to every file of some_folder as alias list
on error
set other_files to first file of some_folder as alias as list
end try
end tell
set dp to (path to desktop as string)
set file_spec to (dp & “XMLMerged.txt”) as file specification
set ref_num to (open for access file_spec with write permission)
try
set eof ref_num to (get eof ref_num) – concatentate
repeat with this_file in other_files
set this_ext to name extension of (info for this_file)
if this_ext is “xml” then
set t to “”
try
set t to read this_file
end try
write t to ref_num
write return to ref_num – just in case
end if
end repeat
close access ref_num
on error err_mess
close access ref_num
display dialog err_mess buttons {“OK”} default button “OK”
error number -128
end try
beep 3

gl,

OOPS. Replace this line:

set eof ref_num to (get eof ref_num) – concatenate

with this line:

read ref_num – move marker beyond eof

The previous line does nothing.

gl,

thanks Kel…This is very helpful

I have tried your suggested code…it works great…but always get an end of file error???

can you please let me know why I am getting that error!

Hi Bill,

It’s the line I wrote aquickly above to move the marker. replace it with this instead:

try
read ref_num
end try

This moves the marker to eof only if there is text to read.

Write back if you find anymore bugs.

gl,

hi Kel,
well…I am getting this error now
“can not make null into type alias”

what I am trying to understand is at first I get a prompt asking about folder to merge, then I get a prompt asking about what to name the ouput folder, then I get a prompt where to store output folder, then (not sure why I am getting that prompt) I get a prompt asking about select a folder!

bill

Hi Bill,

I don’t know why you’re getting all those prompts. The script should just ask you for the folder that contains the xml files. Here’s the script:


set some_folder to choose folder
tell application "Finder"
	try
		set other_files to every file of some_folder as alias list
	on error
		set other_files to first file of some_folder as alias as list
	end try
end tell
set dp to (path to desktop as string)
set file_spec to (dp & "XMLMerged.txt") as file specification
set ref_num to (open for access file_spec with write permission)
try
	if (get eof ref_num) is not 0 then
		read ref_num -- move marker pass eof
	end if
	repeat with this_file in other_files
		set this_ext to name extension of (info for this_file)
		if this_ext is "xml" then
			set t to ""
			try
				set t to read this_file
			end try
			write t to ref_num
			write return to ref_num -- just in case
		end if
	end repeat
	close access ref_num
on error err_mess
	close access ref_num
	display dialog err_mess buttons {"OK"} default button "OK"
	error number -128
end try
beep 3

Try opening this straight into your script editor and running it there on a test folder with xml files.

gl,

Kel,
hope we are on the same track.
I have mutiple xml files in subfolders of a main foder. For example, Articles(folder) has Europe(folder), Asia(Folder). Europe (folder) has Germany.xml …France.xml…etc

not sure what the script suppost to do, it only prompts to select a folder and then not sure if it’s merging the XML files or not!

Kel, thanks for your patiance…but I am trying my best to make this work.

Bill

I didn’t get the subfolders part.

brb,

I just changed it to get all files including files in sub-folders.


set some_folder to choose folder
tell application "Finder"
	try
		set other_files to every file of entire contents of some_folder as alias list
	on error
		set other_files to first file of entire contents of some_folder as alias as list
	end try
end tell
set file_spec to ((some_folder as string) & "XMLMerged.txt") as file specification
set ref_num to (open for access file_spec with write permission)
try
	if (get eof ref_num) is not 0 then
		read ref_num -- move marker pass eof
	end if
	repeat with this_file in other_files
		set this_ext to name extension of (info for this_file)
		if this_ext is "xml" then
			set t to ""
			try
				set t to read this_file
			end try
			write t to ref_num
			write return to ref_num -- just in case
		end if
	end repeat
	close access ref_num
on error err_mess
	close access ref_num
	display dialog err_mess buttons {"OK"} default button "OK"
	error number -128
end try
beep 3

Also, it places the merged file into the chosen folder.

gl,

that’s great :slight_smile: thanks Kel. I do now have a file with all the XML file in it. but I am getting these two annoying lines from each article

<?xml version="1.0" ?>

how can I get ridd of that? should I just cound on find and replace in BBEDIT,or is it something we could handle in this script?
also, wanna add at the beginning of the merged file:

<?xml version="1.0"?> Encyclopedia Britannica

and at the end of the file:

not sure where to add those on the script either.

bill

Hi Bill,

These changes should overwrite or write to the new file adding the header or whatever you would call that.

set some_folder to choose folder
set XML_header to "<?xml version=\"1.0\"?>

Encyclopedia Britannica</main-titl
"
set XML_end to "
"
tell application “Finder”
try
set other_files to every file of entire contents of some_folder as alias list
on error
set other_files to first file of entire contents of some_folder as alias as list
end try
end tell
set file_spec to ((some_folder as string) & “XMLMerged.txt”) as file specification
set ref_num to (open for access file_spec with write permission)
try
(* – replaced the following to overwrite file
if (get eof ref_num) is not 0 then
read ref_num – move marker pass eof
end if
*)
set eof ref_num to 0 – overwrites file
write XML_header to ref_num
repeat with this_file in other_files
set this_ext to name extension of (info for this_file)
if this_ext is “xml” then
set t to “”
try
set t to read this_file
end try
write t to ref_num
write return to ref_num – just in case
end if
end repeat
write XML_end to ref_num
close access ref_num
on error err_mess
close access ref_num
display dialog err_mess buttons {“OK”} default button “OK”
error number -128
end try
beep 3

I don’t know about xml very much, but one thing I was unsure of was whether to write carriage returns or linefeeds after writing each file. You might want to change the line:

write return to ref_num – just in case

and change ‘return’ to ‘(axcii character 10)’

You might want to test it on a simple bunch of xml files and check out in bbedit what is wrong with the resulting text that you are getting the error.

gl,

I should have placed it in the AppleScript tags:


set some_folder to choose folder
set XML_header to "<?xml version=\"1.0\"?>
<document>
<main-title>Encyclopedia Britannica</main-titl
"
set XML_end to "</document>
"
tell application "Finder"
	try
		set other_files to every file of entire contents of some_folder as alias list
	on error
		set other_files to first file of entire contents of some_folder as alias as list
	end try
end tell
set file_spec to ((some_folder as string) & "XMLMerged.txt") as file specification
set ref_num to (open for access file_spec with write permission)
try
	(* -- replaced the following to overwrite file
	if (get eof ref_num) is not 0 then
		read ref_num -- move marker pass eof
	end if
*)
	set eof ref_num to 0 -- overwrites file
	write XML_header to ref_num
	repeat with this_file in other_files
		set this_ext to name extension of (info for this_file)
		if this_ext is "xml" then
			set t to ""
			try
				set t to read this_file
			end try
			write t to ref_num
			write return to ref_num -- just in case
		end if
	end repeat
	write XML_end to ref_num
	close access ref_num
on error err_mess
	close access ref_num
	display dialog err_mess buttons {"OK"} default button "OK"
	error number -128
end try
beep 3

gl,

kel,
I was getting the same results no matter if I used ‘return’ or ‘(ascii character 10)’.
the only problem I still have is each article has a header (first 2 lines in an XML file) that I do not want to be copied with the rest of the content.

so If I have:

<?xml version="1.0" ?> this is an article

All I want to be copied from each XML file is

this is an article

thanks
bill

Hi Bill,

Here’s an example of how you would read 1 file and just get the paragraph part:

set f to choose file
set t to read f
set o to offset of “” in t
set para_text to text o thru -1 of t

I’m assuming that you’re just trying to strip the header. It’s easy to look in the other script and find where it is writing the file information and insert the needed parts of this script in it. If you can’t do that, then write back.

gl,

There’s always text item delimiters as well:

set txt to paragraph 3 of "<?xml version=\"1.0\" ?>
<!DOCTYPE article SYSTEM \"article_demo.dtd\">
<article><para>this is an article</para></article>"
set od to AppleScript's text item delimiters
ASTID("<article><para>")
set t1 to text items of txt
ASTID("</para></article>")
set t2 to text items of item 2 of t1
ASTID(od)
set txt to item 1 of t2 --> "this is an article"

on ASTID(delim)
	set AppleScript's text item delimiters to delim
end ASTID

hi kel and all,
This project is a dictionary project…and I am getting a serious bug now. please help.
each article has an entry for the dictionary
apricot

the entry here is apricot in apricot, the bug I am running into is the entries are not ordered alphabetically in the final XML file.
within each folder the articles are organized alphabetically, but when they are merged in one huge XML file …these entries/articles are not organized in an alphabetical order. what modification on my script do I need to make to order the dictionary entires alphabetically.

thanks,
Bill

Browser: Firefox 1.5
Operating System: Mac OS X (10.4)

sorry…
regarding removing the header…I am not sure where to add the suggestion.
but I think the ordering alph. has a higher priority now…and will tackle with the header issue later.

bill

I’m not sure what your entries look like (confess I didn’t read the whole thread :rolleyes: ), but you can alphbetize it after the fact.

Here are two possibilities:

--  vanilla applescript sort of items in a list --
set the composer_list to {"Ellington", "Copland", "Bach", "Mozart"}
ASCII_Sort(the composer_list) --> {"Bach", "Copland", "Ellington", "Mozart"}

on ASCII_Sort(my_list)
	set the index_list to {}
	set the sorted_list to {}
	repeat (the number of items in my_list) times
		set the low_item to ""
		repeat with i from 1 to (number of items in my_list)
			if i is not in the index_list then
				set this_item to item i of my_list as text
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item comes before the low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end ASCII_Sort

OR

-- Faster shell sort (faster for large list)
set the_list to {"words", "of", "a", "list"}
set ascii_10 to ASCII character 10
tell (a reference to my text item delimiters)
	set {old_atid, contents} to {contents, ascii_10}
	set {the_list, contents} to {the_list as Unicode text, old_atid}
end tell
set the_list to (do shell script "echo " & quoted form of the_list & " | sort")'s paragraphs
--> {"a", "list", "of", "words"}