• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to save the Safari front window contents by script
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to save the Safari front window contents by script


  • Subject: How to save the Safari front window contents by script
  • From: email@hidden
  • Date: Sat, 3 Mar 2007 14:37:41 +0100 (CET)

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   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:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: How to save the Safari front window contents by script
      • From: KOENIG Yvan <email@hidden>
  • Prev by Date: Re: shell scripting
  • Next by Date: Re: How to save the Safari front window contents by script
  • Previous by thread: Re: How to save the Safari front window contents by script
  • Next by thread: Re: How to save the Safari front window contents by script
  • Index(es):
    • Date
    • Thread