How to combine data of all XML files of a folder in a 1 text file!

Hi:

I got a small project. I’m getting multiple folder from the client containing a small XML file in each. The XML file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<from>thomas.wiedemer@kkmedia.com</from>

<jobname>P2_WT1304g_vier_elemente</jobname>

<priority>2</priority>

<timezone>CEST</timezone>

<year>2013</year>

<month>5</month>

<day>29</day>

<hour>14</hour>

I’m trying to fetch and combine the data from multiple XML files to a single text file and send it as attachment from Microsoft Outlook.

I tried something but can’t do:

set XMLFile to ((choose file) as string)

set documentName to XMLFile & “.txt” as text

set folderPath to “Volumes:Macintosh HD:Users:imac3:Desktop:XML Test:”

tell application “System Events”

tell XML element 1 of contents of XML file XMLFile

    set myMail to (value of (XML elements whose name is "from")) as string

    set myJob to (value of (XML elements whose name is "jobname")) as string

    set myPriority to (value of (XML elements whose name is "priority")) as string

    set myYear to (value of (XML elements whose name is "year")) as string

    set myMonth to (value of (XML elements whose name is "month")) as string

    set myDay to (value of (XML elements whose name is "day")) as string

    set myHour to (value of (XML elements whose name is "hour")) as string

end tell

end tell

tell application “TextEdit”

make new document

set txtMail to "From: " & "        " & myMail & "

"

set txtmyJob to "Job Name: " & "    " & myJob & "

"

set txtMyPriority to "Priority: " & "        " & myPriority & "

"

set txtMyDate to "Date: " & "        " & myDay & "/" & myMonth & "/" & myYear & "

"

set txtHour to "Hour: " & "        " & myHour

set text of document 1 to txtMail as text

make new word at end of document 1 with data txtmyJob & txtMyPriority & txtMyDate & txtHour

--save document 1 in file ("Volumes/Macintosh HD/Users/imac3/Desktop/XML Test/Test1.txt")

save document 1 in file (folderPath & documentName)

close window

quit

end tell

Model: iMac
AppleScript: 2.4.3
Browser: Firefox 21.0
Operating System: Mac OS X (10.7)

Hi,

try it like this. used the xmlparser you preferred.

set theFolder to choose folder --assuming all xml's in one topfolder
--get all files of type xml, even in subfolders
tell application "Finder"
	set allXMLs to files of entire contents of theFolder whose name extension is "xml"
end tell

set resString to ""

repeat with f from 1 to count of allXMLs
	set XMLFile to (item f of allXMLs) as text
	
	tell application "System Events"
		
		set firstXmlElementContent to every XML element of (XML element 1 of contents of XML file XMLFile)
		repeat with e from 1 to count of firstXmlElementContent
			set currE to item e of firstXmlElementContent
			set {currName, currValue} to {name, value} of currE
			
			if e is not equal to (count of firstXmlElementContent) then
				set currDelimiter to ", "
			else
				set currDelimiter to ""
			end if
			
			set resString to resString & currName & ": " & currValue & currDelimiter
		end repeat
		
	end tell
	set resString to resString & return & return
end repeat

set targetTxt to (theFolder as text) & "allInOne.txt"
try
	open for access file the targetTxt with write permission
	write resString to file targetTxt starting at eof
	close access file the targetTxt
on error
	try
		close access file the targetTxt
	end try
end try

This’ll not be the fastest script :slight_smile:

I can’t for the bare life of me make TextEdit take it.

Script works fine, until I try to save it with TextEdit, it mumbles about wrong encoding. :o

on run
	local XMLFilePath, documentName, folderPath
	
	
	set XMLFilePath to (choose file)
	
	
	
	tell application "System Events"
		set documentName to name of XMLFilePath
		if documentName ends with ".xml" then
			set documentName to text 1 thru -5 of documentName
		end if
		set documentName to documentName & ".txt"
		set XMLFilePath to XMLFilePath as text
		
		tell XML element 1 of contents of XML file XMLFilePath
			
			set myMail to (value of (XML elements whose name is "from"))
			set myMail to item 1 of myMail as text
			
			set myJob to (value of (XML elements whose name is "jobname")) as text
			
			
			set myPriority to (value of (XML elements whose name is "priority"))
			set myPriority to item 1 of myPriority as text
			
			set myYear to (value of (XML elements whose name is "year"))
			set myYear to item 1 of myYear as text
			
			set myMonth to (value of (XML elements whose name is "month"))
			set myMonth to item 1 of myMonth as text
			
			set myDay to (value of (XML elements whose name is "day"))
			set myDay to item 1 of myDay as text
			
			set myHour to (value of (XML elements whose name is "hour"))
			set myHour to item 1 of myHour as text
		end tell
		
	end tell
	
	set txtMail to ("From: " & "        " & myMail & linefeed) as text
	
	set txtmyJob to ("Job Name: " & "    " & myJob & linefeed) as text
	
	set txtMyPriority to ("Priority: " & "        " & myPriority & linefeed) as text
	
	set txtMyDate to ("Date: " & "        " & myDay & "/" & myMonth & "/" & myYear & linefeed) as text
	
	set txtHour to "Hour: " & "        " & myHour as text
	
	
	tell application "TextEdit"
		
		make new document
		set text of document 1 to txtMail & txtmyJob & txtMyPriority & txtMyDate & txtHour
		set folderPath to ((choose folder) as text)
		set the path of document 1 to folderPath & documentName
		save document 1 as text
		close window
		quit
		
	end tell
end run

Edit
I rewrote the last part to use TextWrangler, which is free, and now it works all right.

tell application "TextWrangler"
		activate
		make new document
		set text of document 1 to txtMail & txtmyJob & txtMyPriority & txtMyDate & txtHour
		set folderPath to ((choose folder) as text)
		save document 1 to folderPath & documentName
		close document 1
		quit
		
	end tell

Try this:

set folderPath to (choose folder) as text -- outside tell block
tell application "TextEdit"
	make new document
	set text of document 1 to txtMail & txtmyJob & txtMyPriority & txtMyDate & txtHour
	save document 1 in file (folderPath & documentName)
	close window 1 saving no
	quit
end tell

Your code will fail if it’s a copy of TextWrangler from the Apple Store. You need to say:

	save document 1 to file (folderPath & documentName)

as the text is plain text, AppleScript’s read/write commands from Standard Additions should be sufficient

Hello.

Thanks Shane, I figured it out in the mean time.
(Good to know about TextWrangler from the AppStore!) Thanks.

Telling TextEdit the path of its document is inappropriate! :slight_smile: I saw that when I ran some other scripts.

Anyways, here is a version that uses sed, and TextEdit.

set XMLFilePath to (choose file)
tell application "System Events" to set documentName to name of XMLFilePath
if documentName ends with ".xml" then
	set documentName to text 1 thru -5 of documentName
end if
set documentName to documentName & ".txt"
tell application "TextEdit"
	make new document
	set text of its front document to (do shell script " cat " & quoted form of POSIX path of XMLFilePath & " |tr -d ' ' |sed -n 's_<from>\\(.*\\)</from>_From:	\\1_p
s_<jobname>\\(.*\\)</jobname>_Jobname:	\\1_p
s_<priority>\\(.*\\)</priority>_Priority:	\\1_p
s_<timezone>\\(.*\\)</timezone>_Timezone:	\\1_p
s_<year>\\(.*\\)</year>_Year:	\\1_p
s_<month>\\(.*\\)</month>_Month:	\\1_p
s_<day>\\(.*\\)</day>_Day:	\\1_p
s_<hour>\\(.*\\)</hour>_Hour:	\\1_p
'")
	
	set savepath to ((choose folder) as text) & documentName
	# 	set path of document 1 to savepath
	save document 1 in savepath
	close document 1
end tell

Thank you very much to all for good and quick response.

I tried the script written by Hans and AppleScript shown me the error"

Thanks again for your support again.

KK

I got same error while I was trying to run the script written by McUsrII:

in 5th line:

is not a valid macpath which starts from top:

“Macintosh HD:Users:Username:Desktop:Folder:”

Did you really use the choose folder command ¿

Hi Hans:

Actually I replaced “choose folder” with the folder path.

My automation applicaton Enfocus PowerSwitch cannot choose the folder itself. I have to specify a default path in script for XML files.

so simply make a new apple script just including
choose folder

then run the script, choose the folder you want to insert to your script and
copy and paste the result (should be alias “Macpath:hjjh:…”) of the applescript window to the targetscript

You could use the classic Finder specifier chain


tell application "Finder"
	set allXMLs to files of entire contents of folder "XMLs" of folder "XML_Test" of desktop whose name extension is "xml"
end tell

Of course. But I think it’s worth making the point that although just supplying a path to the save and open commands has worked in some apps for some time, it doesn’t work with sandboxed apps – you have to use an alias of file. So it’s probably a good habit to get (back) into.

Hello all:

Thank you very much for support from all of you…

Here I got the right script:

set watchFolder to "Users:imac3:Desktop:XML_Test:LR:"

tell application "System Events"
	
	set filesToProcess to POSIX path of (files of folder watchFolder whose visible is true and name extension is "xml")
	
	set xmlFileName to name of file (item 1 of filesToProcess)
	
	set textFileName to text 1 thru 5 of xmlFileName & ".txt"
	
end tell



set outputTextFile to "Users:imac3:Desktop:XML_Test:TXTs:" & textFileName



set outputText to ""

repeat with thisFile in filesToProcess
	
	tell application "System Events"
		
		set xmlFile to XML file thisFile
		
		tell xmlFile
			
			tell XML element 1
				
				value of XML element "from"
				
				
				
				set outputText to outputText & my evenWhitespacer("From", value of XML element "from")
				
				set outputText to outputText & my evenWhitespacer("Job Name", value of XML element "jobname")
				
				set outputText to outputText & my evenWhitespacer("Priority", value of XML element "priority")
				
				set displayDate to value of XML element "day" & "/" & value of XML element "month" & "/" & value of XML element "year"
				
				set outputText to outputText & my evenWhitespacer("Date", displayDate)
				
				set outputText to outputText & my evenWhitespacer("Hour", value of XML element "hour")
				
				
				
			end tell
			
		end tell
		
		set outputText to outputText & return & return
		
	end tell
	
	
	
	try
		
		-- just in case a file pointer got left open
		
		close access outputTextFile
		
	end try
	
	
	
	set fp to open for access outputTextFile with write permission
	
	write outputText to fp starting at ((get eof of fp) + 1)
	
	close access fp
	
end repeat



on evenWhitespacer(tag, val)
	
	set gap to 20
	
	set tag to tag & ":"
	
	set offsetLength to gap - (length of tag)
	
	-- yes, that's a string of empty spaces below.  needed for white space adjustment
	
	set spacer to text 1 thru offsetLength of "                        "
	
	return tag & spacer & val & return & return
	
end evenWhitespacer

Thank you all again…