I’ve been searching throughout google and tried to search this board but could not find a solution. I am new to applescript and I can’t figure out where to start.
I have a file with around 200 images, I’ve already used automator to change each name into sequential order (ex: page_xxx.jpg). I would like to take each file name and place it into an xml text file built like this:
here is an example to create a XML-file with your requested structure.
The list nameList contains the filenames
set nameList to {"image1.jpg", "image2.jpg", "image3.jpg"}
set LF to ASCII character 10
set Pag to "<page>"
set noPag to "</page>"
set Num to "<num>"
set noNum to "</num>"
set XMLtext to "<pages>" & LF
repeat with theItem in nameList
set XMLtext to XMLtext & indent(1) & Pag & LF
set XMLtext to XMLtext & indent(2) & Num & theItem & noNum & LF
set XMLtext to XMLtext & indent(1) & noPag & LF
end repeat
set XMLtext to XMLtext & "</pages>" & LF
on indent(indent_level)
set indent_string to ""
repeat indent_level times
set indent_string to indent_string & tab
end repeat
return indent_string
end indent
choose file name with prompt "Save XML as:" default name "untitled.xml"
set xmlFile to result
choose file with prompt "Create XML file using this file:" without invisibles
set fileList to paragraphs of (read result)
if fileList's last item is "" then set fileList to items 1 thru -2 of fileList -- in case the file ends with a blank line
set newLineTab to ASCII character 10 & tab
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ("</num>" & newLineTab & "</page>" & newLineTab & "<page>" & newLineTab & tab & "<num>")
set xmlText to "<pages>" & newLineTab & "<page>" & "<num>" & fileList & "</num>" & newLineTab & "</page>" & (ASCII character 10) & "</pages>"
set AppleScript's text item delimiters to ASTID
try
open for access xmlFile with write permission
set fileRef to result
write xmlText to fileRef
close fileRef
on error errMsg number errNum
try
close fileRef
end try
display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel"} default button 1
end try
display dialog "Script finished." buttons {"Open XML File", "OK"} default button 2
if (button returned of result) is "Open XML File" then tell application "Finder" to open xmlFile
set f to choose folder -- the folder of images
set file_list to list folder f without invisibles
set thePages to {}
repeat with this_file in file_list
set thisNum to {class:XML element, XML tag:"num", XML contents:contents of this_file}
set thisPage to {class:XML element, XML tag:"page", XML contents:thisNum}
set end of thePages to thisPage
end repeat
--
set theXML to ¬
{class:XML element, XML tag:"pages", XML contents:thePages}
--
generate XML theXML
The result:
"<?xml version=\"1.0\"?>
Picture 1.pdf
Picture 2.pdf
Picture 3.pdf
"
I don’t know how to keep the “num” tags on one line or if it can be done, but maybe you can try and figure it out if you like.