Re: Safari source code
Re: Safari source code
- Subject: Re: Safari source code
- From: kai <email@hidden>
- Date: Sun, 20 Nov 2005 03:53:16 +0000
On 19 Nov 2005, at 22:11, dor kelman wrote:
How do I get the source code of a website in html from my Safari
browser
onto TextEdit. After I already opened the source window on safari I
did:
tell application "Safari"
set x to document "Source of http://news.google.com/" as text
end tell
tell application "TextEdit"
run
set text of document "Untitled" to x
end tell
I got an error that spoke gibberish to me.
Is that really the name of the Safari document, dor? Either way, I'm
afraid you can't coerce a Safari document to text. Instead, you'll
need to get the required document property (in this case, source).
For example, (each in a Safari tell block)...
------------------------------
text of document "Google News"
------------------------------
document 1's source
------------------------------
URL of first document
------------------------------
front document's name
------------------------------
Once that's sorted out, you might then encounter an issue in the
second part of your script, since it's not currently possible to
refer to unsaved TextEdit documents by name. (The index methods shown
above are fine, though.)
Interestingly, it should even be possible to transfer data from one
application to the other in a single line:
----------------
tell application "Safari" to copy front document's source to text of
application "TextEdit"'s front document
----------------
A tell statement is still needed above, because Safari's 'source'
property is an application-specific term. (Outside a Safari tell
block, the term will be regarded as a variable.) However, 'text' is a
different matter:
----------------
set text of application "TextEdit"'s front document to text of
application "Safari"'s front document
----------------
One final point, in case you should be trying to script the opening
of a URL in Safari. The combination of a slow internet connection,
heavy traffic and an uncached, heavy-duty web page, might result in
Safari's progress falling behind that of the script - which could
then throw an indignant error if the data turns up too late. Little
of this is 100% foolproof - but the risk of errors can be reduced
significantly by inserting delays at certain critical stages in the
script. Here's an example:
----------------
on SafariData from targetURL given source:source
tell application "Safari" to tell document 1
open location targetURL
repeat until name starts with "Loading" or name starts with "Untitled"
delay 0.2
end repeat
repeat while name starts with "Loading" or name starts with "Untitled"
delay 0.2
end repeat
if |source| then return source
its text
end tell
end SafariData
set t to SafariData from "http://www.apple.com/" with source
tell application "TextEdit" to make new document with properties
{text:t}
----------------
(To get text only: < set t to SafariData from "http://www.apple.com/"
without source >)
---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden