Extract text displayed on a web page, if the text is a certain color

Hi, I’m trying to make AppleScript extract some of the text that is being displayed on a Safari web page.

I can make AppleScript extract text based on the font of the text. However I get an error message when I try to extract text based on the color of the text.


Here is the web page:

[code]

Red Text
This text is red Courier
This text is blue Verdana-Bold
[/code] ------------------------------------------------

I wrote an AppleScript called Safari_courier_font_text.scpt. It extracts text whose font is courier. Here is the content of that AppleScript:

tell application "Safari" activate set courier_text to (characters of text of document 1 whose font is "Courier") & return display dialog (courier_text) as string end tell
I display the web page, and then run the AppleScript. That AppleScript works fine.


I also created a file called safari_test_color.scpt. Here is the script:

[code]tell application “Safari”
activate
display dialog (characters of text of document 1) as text

set redColor to {65535, 0, 0} as RGB color
set red_text to ((characters of text of document 1) whose color = redColor)

end tell[/code]
I display the web page, and then run the AppleScript.

The display dialog works fine. (The display dialog is before the check for the color, to make sure that the “(characters of text of document 1)” is correct.)

However after the display dialog, I get this message:
AppleScript Error
Safari got an error: Can’t make color into type
reference.

Also, these characters are highlighted in my script:
(characters of text of document 1) whose color = redColor


What should I do, to make AppleScript extract text from the web page, based on color?

I’m running
Mac OS X 10.5.6 (9G55)
Safari Version 3.2.1 (5525.27.1)
Script Editor Version 2.2.1 (100.1)
on an iMac G5.

Thank you.

Oops, I guess I should have clicked on the “Applescript” button for my HTML and for my AppleScripts, instead of using the
tags, right?

Thanks.

Hi, I’m not sure how complicated your web page is but give this ago.


extractText("red")

on extractText(theColour)
	set theSource to (do shell script "curl " & quoted form of ("http://yourwebpagehere.com"))
	set AppleScript's text item delimiters to {"  <div style=\"color:" & theColour & "; font-family:'courier';\">"}
	set theText to text item 2 of theSource
	set AppleScript's text item delimiters to {"</div>"}
	set theText to text item 1 of theText
	set AppleScript's text item delimiters to {""}
end extractText

Hendo, thank you very much! :slight_smile:

Do you know why I get “Safari got an error: Can’t make color into type reference.” when I run in safari_test_color.scpt?

I’d like to check the color of the text that’s displayed on a web page, instead of checking the html source. If I check the color of the text that’s displayed, then I don’t have to change my AppleScript when I change my html. Is there a way to check the color of the text that’s displayed on the web page (not the html)?

Safari_courier_font_text.scpt checked the font that was displayed on the web page, but I’m having problems checking the color.

Thanks.

:smiley: Jacques, that works great!!! Thank you so much.

The shade of red on my web page is {65535, 0, 0}. When I used that shade in the AppleScript that you gave me, the script worked fine.

Thank you.

Can anyone tell me how to get selected text in a web page from “Firefox”? (for further processing, ofcourse). I simply want plain text.
From what I know, Firefox does not have much support for applescripts and there is hardly anything in the Firefox dictionary.