Copying text from a Safari "View Source" window

I have a question… is it possible to come up with some kind of Applescript so you can copy certain text from a Safari “View Source” window?

I’m trying to create a catalog of images we’ve downloaded from a stock agency, and make it keyword-searchable. The words I need in the Page Source view, the metadata, always appears in the same way.

The first part is: <meta name=“keywords” content=

Then there are the set of keywords:

“Horizontal, One Person, Falling, Sky, Flock Of Birds, Flying, Cloudscape, Cloud, Building Exterior, Physical Pressure, stock images, royalty free images, stock photography, stock photos, inexpensive, istockphoto” />

and it always ends in istockphoto" />

So the whole thing is:

I should be able to come up with something that will find the first part, then copy all the text between the quotation marks, uhhhh… right? Easy-peasy… just a few lines of code… huh? :rolleyes:

I’ve very little experience with AppleScript… almost none in fact, and it looks as if I should be able to do some of this just using the Safari library, but I’m not having any luck so far.

Hi,

How about something like this. Have the page frontmost in Safari and run this script:

-- John Maisey -- [url=http://www.nhoj.co.uk]www.nhoj.co.uk[/url] -- 10 Mar 2010
tell application "Safari" to set myText to source of front document

set my text item delimiters to "<meta name=\"keywords\" content="
set myText to text item 2 of myText
set my text item delimiters to "istockphoto\" />"
set myText to text item 1 of myText
set my text item delimiters to {""}

return myText

Best wishes

John M

Hi John,

This worked pretty well.

I tweaked one line so that the last work, “istockphoto” would be included.


tell application "Safari" to set myText to source of front document

set my text item delimiters to "<meta name=\"keywords\" content="
set myText to text item 2 of myText
set my text item delimiters to "/>"
set myText to text item 1 of myText
set my text item delimiters to {""}

return myText

This gives me a Result that looks like:

""Food, Breakfast, Marmalade, Croissant, Pastry, Blue, French Cuisine, stock images, royalty free images, stock photography, stock photos, inexpensive, istockphoto" "

Now I’m trying to figure out how to take the results and put it on the clipboard so I can paste them into MS Excel… this is harder than it would seem… I’ve done several searches of the forum using the keywords “results” and “clipboard” plus others here and there, and have not really come up with anything that works.

So far, this is the best I have:

tell application "Safari" to set myText to source of front document

set my text item delimiters to "<meta name=\"keywords\" content="
set myText to text item 2 of myText
set my text item delimiters to "/>"
set myText to text item 1 of myText
set my text item delimiters to {""}

return myText


tell application "Script Editor"
	activate
	set myText to the clipboard
end tell

Plus, I’d like to get rid of the "" and " " at the beginning and end of the string, but that can easily be done by a Find and Replace in Excel.

But your script got be a huge step forward… Thanks again!

You could add the quotes to the text you are splitting it by. You’re also very close with the clipboard bit.

tell application "Safari" to set myText to source of front document

set my text item delimiters to "<meta name=\"keywords\" content=\""
set myText to text item 2 of myText
set my text item delimiters to "\" />"
set myText to text item 1 of myText
set my text item delimiters to {""}

set the clipboard to myText

John M

THAT’S IT!!!

Wow, looks so easy when someone who knows what they’re doing does it. :slight_smile:

Thanks so much!

Is there an “applescript for idiot’s-type” book you could recommend? I want to be smart like you!

Hi,
I’m not John M, but anyway I’ll respond to this post. If you want to learn AppleScript or AppleScript Studio quickly, easy and free, I think it’d be good for you to start by reading some tutorials here. I learned the language on this site too, and if you don’t understeand something, you can always ask a bit of help here.

Link to the tutorials:
http://www.macscripter.net/viewtopic.php?id=25631

I hope it helps,
ief2

Thanks for the suggestion, ief2. I appreciate it.

Now off to search for my next project. :slight_smile: