Copying and pasting

I know very little about AppleScript and need help.

The first part of my script collects text from 180 web pages that reside on my hard disk. The second part manipulates the text collected. I’m using Internet Explorer 5.0 to open individual web pages and Keyquencer Lite to simulate Cmd+A and Cmd+C keystrokes. Next, I activate Tex-Edit Plus and use Keyquencer Lite to simulate Cmd+V. The text I’m collecting ends up in a Tex-Edit Plus document. Is there a better way to extract text from 180 web pages than the method described above?

I would like to access the clipboard from within the Script Editor. What is the syntax used to copy (not append) a text string to the clipboard? What is the syntax used to copy clipboard text to a Script Editor variable? Nothing works for me, so far.

ErikN

Safari provides a way to do it without keystrokes (text of document 1), but I think there is not a similar functionality in OS 9 (unfortunatelly).

To place text into the clipboard, simply use:

set the clipboard to "whatever"

In your particular, you can use this:

tell application "Internet Explorer" to set x to selected text
tell application "Tex-Edit Plus" to set contents of window 1 to x

I’m not sure if there is anything else that will render the pages for you without opening them. However, there is no need to use the clipboard and emulate keystrokes for this. The following will get the text from the body of the webpage:


tell application "Internet Explorer"
	set bodyText to do script "document.body.outerText"
end tell

Now you’ve got the raw text of the webpage saved into the variable called bodyText.

If the method I described above returns text that is not well-formatted enough and you still have to emulate copy-paste, you’d just use the following to get the clipboard into a variable:


set bodyText to (the clipboard)

This returns formatted text, though. If you want plain text, here’s a useful handler I use to convert almost anything into plain text (including lists, etc). It will only work on English version, though, since it depends on the error message starting with "Can’t get character 1 of ":


on CoerceToString(incomingObject)
	-- version 1.1, Daniel A. Shockley, http://www.danshockley.com
	set errMsgLead to "Can't get character 1 of "
	if class of incomingObject is string then
		set {text:incomingObject} to (incomingObject as string)
		return incomingObject
	else if class of incomingObject is integer then
		set {text:incomingObject} to (incomingObject as string)
		return incomingObject as string
	else if class of incomingObject is real then
		set {text:incomingObject} to (incomingObject as string)
		return incomingObject as string
	else
		-- LIST, RECORD, or unknown
		try
			set listText to (first character of incomingObject)
		on error message
			--tell me to log message
			set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, errMsgLead}
			set objectString to text item 2 of message
			set AppleScript's text item delimiters to od
			--tell me to log objectString
			set newString to text 1 thru -2 of objectString
			set {text:newString} to (newString as string)
		end try
		
		return newString
	end if
end CoerceToString