Need help with reading xml
goal is to put each post into devonthinkpro.
using the tumblr v1 api because you dont need a key
set thePostStart to {start:0, num:50}
tell application id "DNtp"
set theXML to download markup from "http://joyofgif.tumblr.com/api/read?start=0&num=20" encoding "UTF-8"
end tell
tell application "System Events"
set rootXML to make new XML data with data theXML
set theElements to XML element 1 of (XML element 1 of rootXML)
set theName to value of XML attribute "name" of XML element "tumblelog" of XML element "tumblr" of rootXML
set thetitle to value of XML attribute "title" of XML element "tumblelog" of XML element "tumblr" of rootXML
set theTotalPosts to value of XML attribute "total" of XML element "posts" of XML element "tumblr" of rootXML
----
set theElements2 to XML element 2 of (XML element 1 of rootXML)
set theElementPosts to every XML element of theElements2
repeat with anElement in theElementPosts
--set thePostId to id of (get properties of item 1 of theElementPosts)
set thePostId to id of anElement
--set thePostId to id of item (i) of theElementPosts
set thePostURL to value of XML attribute "url" of anElement
set thePosturlwithslug to value of XML attribute "url-with-slug" of anElement
set thePostType to value of XML attribute "type" of anElement
--type video, photo
set theOtherUrl to value of XML attribute "url" of anElement
--Photo
try
set thePostPhotoURL to value of XML element "photo-url" of anElement
on error
set thePostPhotoURL to ""
end try
--Video
try
set thePostVideoURL to value of XML element "video-player" of anElement
on error
set thePostVideoURL to ""
end try
--to do - dont work right
try
set theElementsTest1 to every XML element of item 1 of theElementPosts
set theElementsTest2 to (every XML element of anElement whose name = "tag")
on error
--set theTag to ""
end try
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
try
set theTags to (text items of theTag)
on error
set theTags to {}
end try
set AppleScript's text item delimiters to od
set theTag to ""
set theElement1 to ""
set theAttribute1 to ""
set thetagtest to ""
end repeat
end tell
Model: mini
AppleScript: 2.12
Browser: Firefox 48.0
Operating System: Mac OS X (10.6.8)
I downloaded the DevonThink Pro demo and the script extracted correctly the elements related to tags.
What is wrong is the group of instructions usind text item delimiters to get the tag values
Here is the related set of instructions.
I added several log instructions to trace easily the behavior.
I added comments explaining what was wrong in your code.
--to do - dont work right
try
log "ask for tag" # ADDED
set theElementsTest1 to every XML element of item 1 of theElementPosts
log "ask for tag 2" # ADDED
set theElementsTest2 to (every XML element of anElement whose name = "tag")
# the variable theTag is not defined # ADDED
log "ask for tag 3" # ADDED
set theTags to {} # ADDED
if theElementsTest2 ≠{} then # ADDED
log "ask for tag 4" # ADDED
repeat with anItem in theElementsTest2 # ADDED
set end of theTags to (get value of anItem) # ADDED
end repeat # ADDED
log theTags # ADDED now you have the list of tags
end if
on error
--set theTag to ""
end try
log "ask for tag 5"
# wrong code from here to "ask for tag 8"
# wrong because theTag is defined ONLY if you got an error before which you didn't
# TIDs may split a text object but the code above gave only a list of XML elements
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
try
log "ask for tag 6"
set theTags to (text items of theTag) # Here theTag is defined only if you got an error before. But you didn't got one
log "ask for tag 7"
on error
set theTags to {}
end try
set AppleScript's text item delimiters to od
log "ask for tag 8"
Here is the end of the events log.
get value of XML element "video-player" of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4"
--> error number -1728 from XML element "video-player" of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4"
(*ask for tag*)
get every XML element of XML element 1 of XML element 2 of XML element 1 of XML data id "sans titre 4"
--> {XML element 1 of XML element 1 of XML element 2 of XML element 1 of XML data id "sans titre 4", XML element 2 of XML element 1 of XML element 2 of XML element 1 of XML data id "sans titre 4", XML element 3 of XML element 1 of XML element 2 of XML element 1 of XML data id "sans titre 4", XML element 4 of XML element 1 of XML element 2 of XML element 1 of XML data id "sans titre 4", XML element 5 of XML element 1 of XML element 2 of XML element 1 of XML data id "sans titre 4", XML element 6 of XML element 1 of XML element 2 of XML element 1 of XML data id "sans titre 4"}
(*ask for tag 2*)
get every XML element of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4" whose name = "tag"
--> {XML element 3 of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4", XML element 4 of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4", XML element 5 of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4"}
(*ask for tag 3*)
(*ask for tag 4*)
get value of XML element 3 of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4"
--> "The Photographers' Gallery"
get value of XML element 4 of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4"
--> "gif"
get value of XML element 5 of XML element 20 of XML element 2 of XML element 1 of XML data id "sans titre 4"
--> "bornin1987"
(*The Photographers' Gallery, gif, bornin1987*)
(*ask for tag 5*)
(*ask for tag 6*)
(*ask for tag 7*)
(*ask for tag 8*)
end tell
We aren’t using the same system but I feel that it deesn’t matter here.
Thanks for the reply!
Your post got me over a stumbling block.
I was hoping for some routines/techniques for working with xml.
I am aware of satimage osax and its xml but could not get it to load a url with parameters.
Cant seem to find examples written with it.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
-- build URL and create XML document
set theURL to current application's |NSURL|'s URLWithString:"http://joyofgif.tumblr.com/api/read?start=0&num=20"
set {theDoc, theError} to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theDoc = missing value then error (theError's localizedDescription() as text)
-- extract posts and work through them
set theElementPosts to (theDoc's rootElement()'s elementsForName:"posts")'s firstObject()'s elementsForName:"post"
repeat with anElement in theElementPosts
set thePostId to (anElement's attributeForName:"id")'s stringValue() as text
set thePosturlwithslug to (anElement's attributeForName:"url-with-slug")'s stringValue() as text
set thePostType to (anElement's attributeForName:"type")'s stringValue() as text
set thePostVideoURL to ""
set thePostPhotoURL to ""
if (thePostType = "photo") then
set thePostPhotoURL to (anElement's elementsForName:"photo-url")'s firstObject()'s stringValue() as text
else if (thePostType = "video") then
set thePostVideoURL to (anElement's elementsForName:"video-player")'s firstObject()'s stringValue() as text
end if
set theOtherUrl to (anElement's attributeForName:"url")'s stringValue() as text
set theTags to ((anElement's elementsForName:"tag")'s valueForKey:"stringValue") as list
-- do stuff with values
end repeat