It works well.
So now I have a draft of the code which I need :
set theURL to "http://support.apple.com/kb/TS5482?viewlocale=fr_FR"
set rawXML to do shell script "curl -L " & theURL
if rawXML does not contain "s.pageName=" & quote & "acs::kb::404 error::we're sorry." then
if rawXML does not contain "<div class=" & quote & "mod-date" & quote & ">" then
# It's not a page "Welcome to Apple Support"
set lineWithDate to first paragraph of item 2 of my decoupe(rawXML, "<div class=" & quote & "mod-date" & quote & ">")
set theModDate to item 2 of my decoupe(lineWithDate, quote & space)
set lineWithNumNote to first paragraph of item 2 of my decoupe(rawXML, "var documentId = '")
set theNumNote to item 1 of my decoupe(lineWithNumNote, "';")
set lineWithTitle to first paragraph of item 2 of my decoupe(rawXML, "s.pageName=")
set theRawTitle to item -1 of my decoupe(lineWithTitle, "::")
if theRawTitle contains "&#" then
tell application "ASObjC Runner"
set theRawTitle to modify string theRawTitle so it is unencoded for XML
end tell
end if
if text -11 thru -1 of theRawTitle is in {" (fr_fr)" & quote & ";", " (en_us)" & quote & ";"} then set theRawTitle to text 1 thru -11 of theRawTitle
end if
end if
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
I will search in pieces of code already posted by Shane Stanley if I find a better way to unencode the strings looking like :
“iPhoto doesn&# 39;t show an option to share to Weibo in OS X Yosemite (fr_fr)";”
I inserted a space before 39 so that the embedded entity isn’t deciphered by the message parser.
Yvan KOENIG (VALLAURIS, France) jeudi 6 novembre 2014 20:36:19
Maybe the parsing is easier for you with the the XML Suite of System Events? I figured first that plutil was ok, if you were to just extract some keys you knew of with it. But alas, many xml files seems to be broken after they added json support to it (plutil)…
ASObjC Runner does the job.
I’m just in search of a way to do that triggering Objective C because Shane wrote that ASObjC Runner is more or less obsolete with Yosemite.
Yvan KOENIG (VALLAURIS, France) vendredi 7 novembre 2014 19:38:24
Shane means that in Yosemite you’re able to run AppleScriptObjC code in scripts without using libraries but there are a lot of complex functions in ASObjC Runner that makes the tool very useful though
No, you understood correctly. But this is one of those things that is not directly accessible using ASObjC. The solution is to use the ASObjCExtras framework, a new version (1.1.0) of which I released the other day. (The previous version could do it too, but there’s an AppleScript bug that complicated things.)
So the equivalent of the ASObjC Runner command would be:
set unTitre to current application's SMSFord's stringFrom:unTitre makingIt:"UnecodedForXML"
And the script would need the line:
use framework "ASObjCExtras"
The framework can be in ~/Library/Frameworks, /Library/Frameworks, or the /Contents/Frameworks folder of a bundled applet or .scptd file.
Most of Runner’s functions that aren’t accessible directly from vanilla ASObjC have been transferred to the ASObjCExtras framework. That means many of the list and text commands, along with the trigonometry stuff. ASObjCExtras was written to be the successor of Runner.
ASObjC Runner still runs OK, and may keep doing so for years – but it also may fail. I’d rather people start moving away from it now, than when it happens.
ASObjCExtras also covers two of the current shortcomings of ASObjC simply: the ability to convert dates, and the fact that coercion of numbers to reals loses precision.
Thanks.
In fact, the simple fact to quote the link was solving the problem.
do shell script "curl -L 'http://support.apple.com/kb/HT1014?viewlocale=fr_FR&locale=en_US'"
I don’t understand what’s the need for that because there is nothing requiring to be quoted in the link.
In practice, I will play safe and use your code.
Yvan KOENIG (VALLAURIS, France) samedi 8 novembre 2014 12:54:40
Sometimes it is useful to have a useragent string, here are two ways: one for Safari to generate it, and something you can save as a html file, load into your browser of choice, and copy the user agent string to clipboard from there. (Stolen from w3schools.com.)
tell application "Safari" to tell current tab of window 1 to set the clipboard to (do JavaScript "navigator.userAgent;")
I always use a user-agent string with curl or wget, unless I’m just testing something. In general it’s a good practice to keep the host from knowing you’re not a browser.
The JavaScript is handy. Thanks.
Note that you can change the user-agent in Safari using the Develop menu and get a few of the common ones.
The ampersand and is-equal-to symbols in the URL are special characters in Bash and will be interpreted and/or substituted. Therefore it’s better to quote URLs with single quotes, or even better, use quoted form of.
I am encountering an other problem.
My script triggering curl grab the contents of hundreds of technical notes by groups of 100.
The extracted datas are written in a text file.
I take care to clean the used variables after the treatment of each group.
It’s not sufficient, the memory used by the script is growing so that it may reach 3 Gbytes.
When it reach such level, I force the script to quit then restart it for a new cycle.
My understanding is that AppleScript doesn’t free the memory allocated to the variables.
Is there a way to force it to do this required cleaning ?
Yvan KOENIG (VALLAURIS, France) mercredi 12 novembre 2014 13:28:04
I don’t think that the problem is inside your script. I think Script-Editor is responsible for this as well (if you’re using script editor). When a script is run in script editor and makes used of script addition commands, somehow the results of the commands of the script additions are not freed but they remain retained. The memory leak won’t happen if the script is run by osascript or by itself. I found this issue a while ago when I wrote my own scripting addition and scared me watching it eating memory, then after some digging I found out that it wasn’t my scripting addition causing this problem but script editor itself.
here a small example:
repeat 100000 times
offset of "a" in "MacScripter"
end repeat
The code above will eat up to about 132MB ram. When you re-run the command it will eat up 132MB ram again, and again. It’s a bug that’s in AppleScript-Editor for a long time.
Now save the script and (add a delay at the end so you can see what happens to the memory). Then run it outside script editor ( I used osascript command line utility). Now you’ll see that the script eats no memory at all. On my machine it’s stable at 2MB.
Running in the Editor was useful to be sure that everything was OK.
As It’s right now, the next time, I will save it as an application and run the app.
With it, i put my hands upon more than 35.000 technical notes.
Knowing them will be useful to find explanations to some oddities which are referenced but which I ignored.
Yvan KOENIG (VALLAURIS, France) mercredi 12 novembre 2014 14:55:19