I’ve learned a lot from this site, and have received some great help from the forums and the tutorials. But there are some basic stuff that I keep getting caught on, and it is taking me forever to do seemingly simple things. If you can, please help me put the final touches on this script.
I have an Automator action that gets some .xml files and places them in ~/Desktop/gcal. The files’ names are all similar to: “P31625.xml”.
Now I have to convert these into an .ics format. Right now the script is working on each file separately, which doesn’t make sense. I should probably concatenate them and then do the editing. I looked at the ‘cat’ command, but I haven’t found how to pass the list of files to the command (syntax: cat file1.xml file2.xml > outputfile.xml).
The script that I have loops through each file, fixing the line endings to CRLF, then deleting a few tags that I don’t need. But there are things that I need to add to the file, and I can’t figure out how to get it to do that at a specified place. I have used Adam Bell’s excellent tutorial and key snippets to frankenstein some code to search for text within specified tags because some of the text needs to be duplicated (some URL links, for example).
The final file should be a combination of all the individual .xml files, but in the .ics format. I’ve included a short sample of the .xml file, a basic .ics file (line endings are CRLF), and the script I currently have (don’t laugh).
Current Applescript:
set myFolder to ((path to desktop as Unicode text) & "gcal")
tell application "Finder" to set theFiles to (every file of folder myFolder whose name extension is "xml")
set myFolder2 to "Xamego:Users:mlafleur:Desktop:gcal:" --this is needed for TextWrangler to open below
--probably combine all files here to avoid the Repeat loop: cat file1.txt file2.txt > file1and2.txt
#set joinCommand to "cat " & quoted form of theFiles & " > joined.xml" --doesn't work
#do shell script joinCommand
repeat with j from 1 to the count of theFiles
tell application "Finder" to ¬
set thisFilePP to quoted form of POSIX path of (item j of theFiles as alias)
tell application "Finder" to ¬
set thisName to (name of item j of theFiles)
set theCommand to "sed -i .1bak -e 's/\"/\\\\\"/g' " & thisFilePP --I think this is no longer needed given the Find/Replace below
do shell script theCommand
tell application "Finder" to ¬
set myFile2 to myFolder2 & thisName as alias
tell application "TextWrangler"
open myFile2
tell text 1 of text window 1
set currentDoc to (read (test1))
set ex1 to extractBetween(currentDoc, "<xls>", "</xls>") of me -- extract the URL
set tURL to "http://www.cepal.org" & ex1
#now find a way to insert this into the text, with \\r\\n
set searchOptions to {search mode:literal, starting at top:true}
replace "><" using ">\\r\\n<" options searchOptions
------------
#at begining of file add "BEGIN:VCALENDAR\\r\\nMETHOD:PUBLISH\\r\\nX-WR-TIMEZONE:America/Santiago\\r\\nPRODID:-//Apple Inc.//iCal 3.0//EN\\r\\nCALSCALE:GREGORIAN\\r\\nX-WR-CALNAME:CEPALTEST\\r\\nVERSION:2.0\\r\\nX-APPLE-CALENDAR-COLOR:#B027AE" --how?
---------
#add "BEGIN:VEVENT\\r\\nSEQUENCE:0" before each event --how?
replace "<pagina>" using "" options searchOptions
replace "<abstracto>" using "" options searchOptions
replace "</abstracto>" using "" options searchOptions
replace "<noticia>" using "" options searchOptions
replace "<autor>1</autor>" using "" options searchOptions
replace "<idioma>ES</idioma>" using "" options searchOptions
replace "<Contacto>" using "" options searchOptions
replace "</Contacto>" using "" options searchOptions
replace "<agrupadores>" using "" options searchOptions
replace "</agrupadores>" using "" options searchOptions
replace "</noticia>" using "" options searchOptions
replace "</pagina>" using "" options searchOptions
replace "<?xml version=\\\"1.0\\\" encoding='ISO-8859-1'?>" using "" options searchOptions --update this
replace "<Titulo>" using "DESCRIPTION:" options searchOptions
replace "</Titulo>" using "" options searchOptions
replace "<link>" using "UID:http://www.cepal.org" options searchOptions
replace "</link>" using "" options searchOptions
replace "</jdoid>" using "\\r\\nEND:VEVENT" options searchOptions --to end the event
------------
#need to add "END:VCALENDAR" at the EOF
---------
end tell
save text document thisName
close text window 1
end tell
end repeat
to extractBetween(SearchText, startText, endText)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to startText
set endItems to text of text item -1 of SearchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text of text item 1 of endItems
set AppleScript's text item delimiters to tid
return beginningToEnd
end extractBetween