Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to save the Safari front window contents by script



Hi all,

first of all, thanks to Yvan and Malcolm [1].

second, you can read below a different method to save Safari’s front window source (what I wanted to do) in a html file.

third, the download testing part will start :( right now!


In the mean time (between the mail sent time and your answers), I realized that I need to add a timing (wait) between sending the URL requests to Safari and the moment I save the document to disk: I saved nth time the 'nearly the same' file. But, a simple search in the AppleScriptLanguageGuide.pdf gives me the "delay" command. Now, I have to make some testings to get a proper wait time (delay to get hte asked document from the Yahoo file server).

You may ask me why I want so bad to read the documents later (off-line) and here why: I simply cannot read them on-line, I actually am getting stuff from a public WiFi (on the PowerBoog G4 Alu battery)... So I search a different way to get the data. When they will be in my local hard disk, I will read them | make some search on them.

tell application "Safari"
	(* Get a reference to the document *)
	set myDoc to document of front window
	
	(* Get the source of the page *)
	set mySrc to source of myDoc
	
	(* Get a file name *)
	set myName to "Message_" & "0001" & ".html" -- the # will be modified later
	
	tell application "Finder"
		(* Get a path to the front window *)
		set myPath to (target of front window) as string
		
		(* Get a file path *)
		set filePath to myPath & myName
		
		(* Create a brand new file *)
		set openRef to open for access (myPath & myName) with write permission
		
		(* Save the document source *)
		write mySrc to openRef
		
		(* Close the file *)
		close access openRef
	end tell
end tell


<quote from the Safari dictionary, Safari suite>
document n [inh. document; see also Standard Suite] : A Safari document.
   elements
   contained by application.

   properties
   source (Unicode text, r/o) : The HTML source of the document.
   text (text, r/o) : Text of the document. A copy. Modifications to text aren't reflected on the web page.
   URL (Unicode text) : The current URL of the document
</quote from the Safari dictionary>

To get the source (and the url) of the document, I used the above information, but I split the code in two part as you can see in the top of the above code; usually, I get troubles when I put this kind of instructions in only one line (but after a simple check, this is not the case; however, the code looks nice as is).


Nota: I want to use a script to download around 5550 messages from a Yahoo group I am registered and (try to) read them when I will be off-line. So my saved files names will be "Message_<number>.html" and the <number> will be computed in a variable (the value range will be 1 to 5550). So, if you want to use the code, think at your own file name; if you believe there is an error there(all files will get the same name: do not worry - thanks for thinking fine - I know.



Malcolm, here a start to get the file name (from the URL):

tell application "Safari"
	(* Save the original text item delimiters *)
	set originalATID to AppleScript's text item delimiters
	
	(* Get a reference to the document *)
	set myDoc to document of front window
	
	(* Get the source of the page *)
	set myURL to URL of myDoc
	
	try
		(* set the AppleScript' Text Delimiter *)
		set AppleScript's text item delimiters to "/"
		
		(* Get the last field *)
		set fileName to last text item of myURL
		
		(* Error checking *
		if fileName = "" then
			(* Compute the number of nested folders in myURL *)
			set depthURL to {count of text items of myURL}
			
			(* Get the last folder name *)
			set fileName to text item (depthURL - 1) of myURL
		end if
		
		(* Restore the original AppleScript's text item delimiters *)
		set AppleScript's text item delimiters to originalATID
		
		(* Display the file name *)
		return {fileName}
		
	on error
		(* Restore the original AppleScript's text item delimiters *)
		set AppleScript's text item delimiters to originalATID
	end try
	
	(* Restore the original AppleScript's text item delimiters *)
	set AppleScript's text item delimiters to originalATID
end tell

Nota: the value (number of nested folders, what I called depthURL) stored in depthURL is false (http://), but we do not care because we only use it to get the last - 1 field (the folder where the document is stored).
Also, some wrongly created URL can hold two "//", etc. Hey, who said that the code is fool proof ?


What I do not found, is the way to convert the internet encoding of %20 to a space " ", and so on with other characters...

Yes, some people do not encode their file names and so the file name holds a space, etc.

HTH,

Emile



[1] Both code have its own trouble while executed here. But they lead me to search how to get the document name. I remembered that I found it easely with Netscape... and fall into the code to save the document source (what I wanted) without dialog.

Yvan code: 
Error in System Events:
NSReceiverEvaluationScriptError: 4

I get the save as sheet dialog, AppleScript Text Editor open, behind.

Malcom code: does not return the title (document.title returns an empty string)


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-users/email@hidden
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.