Thanks, everyone, for your replies. It seems this is far more complex than I originally thought.
I’ve tried each of the examples provided and I do get the results outlined, but I’m unsure how to combine these so that either by using curl or by calling the webpage I can capture only the bits of text between the delimiters.
For instance, Regulus6633’s following suggestion worked fine for the given example:
set t to "{\"responseData\":{\"translatedText\":\"Ciao Mondo\"},\"responseDetails\":\"\",\"responseStatus\":200,\"matches\":[{\"id\":\"0\",\"segment\":\"Hello World\",\"translation\":\"Ciao Mondo\",\"quality\":\"70\",\"reference\":\"Machine Translation provided by Google, Microsoft, Worldlingo or MyMemory customized engine.\",\"usage-count\":1,\"subject\":\"All\",\"created-by\":\"MT!\",\"last-updated-by\":null,\"create-date\":\"2012-10-28\",\"last-update-date\":\"2012-10-28\",\"match\":0.85}]}"
getTextBetweenDelimiters(t, "{\"responseData\":{\"translatedText\":\"", "\"},\"responseDetails\"")
on getTextBetweenDelimiters(theText, firstDelim, secondDelim)
try
set {tids, text item delimiters} to {text item delimiters, firstDelim}
set a to second text item of theText
set text item delimiters to secondDelim
set b to first text item of a
set text item delimiters to tids
return b
on error
return theText
end try
end getTextBetweenDelimiters
I then tried to combine this with Yvan’s script to get:
set t to do shell script "curl " & quoted form of "http://mymemory.translated.net/api/get?q=Hello%20World!&langpair=en|it"
getTextBetweenDelimiters(t, "{\"responseData\":{\"translatedText\":\"", "\"},\"responseDetails\"")
on getTextBetweenDelimiters(theText, firstDelim, secondDelim)
try
set {tids, text item delimiters} to {text item delimiters, firstDelim}
set a to second text item of theText
set text item delimiters to secondDelim
set b to first text item of a
set text item delimiters to tids
return b
on error
return theText
end try
end getTextBetweenDelimiters
The problem being, of course, how to deal with spaces and punctuation (i.e. if I put in some other, longer text and don’t insert %20 where there are spaces, it will fail. I assume the same will happen if I input any punctuation as well).
Would I just be better off having the script open a webpage and work with it from there? That seems to work very well:
set t to the clipboard
getTextBetweenDelimiters(t, "{\"responseData\":{\"translatedText\":\"", "\"},\"responseDetails\"")
on getTextBetweenDelimiters(theText, firstDelim, secondDelim)
try
set {tids, text item delimiters} to {text item delimiters, firstDelim}
set a to second text item of theText
set text item delimiters to secondDelim
set b to first text item of a
set text item delimiters to tids
return b
on error
return theText
end try
end getTextBetweenDelimiters
But I’m unable to then copy the final output to the clipboard so that it can be pasted to a give file. I tried using:
set clipboard to getTextBetweenDelimiters
This didn’t, however, work.
Can anyone advise what the best course of action would be?
Thanks,
Bowjest