How to Copy/Paste between two apps?

I posted this into wrong forum. My apologies. Here goes:

Ok… I’m taking the easy way out by posting a message here.

I am trying to develop an AppleScript for the first time with Safari browser.

I need a script that will automatically get HTML source of the current page and copy and paste that source into either TextEdit or BBEdit.

I can’t seem to find the proper term to copy and paste. Here’s what I wrote so far - pathetic, I know. Recording in AppleScript Editor doesn’t work with Safari.

tell application “Safari”
set page_source to the source of document
end tell

tell application “TextEdit”
set document to page_source
end try
end tell

Many thanks!


You should reference correctly the items, according to its hierarchy:

tell application "Safari"
   set page_source to the source of document 1
end tell
--> which is equal to:
tell application "Safari" --> application
   tell document 1 --> item
      set page_source to source --> property
   end tell
end tell

--> And the same for TextEdit:
tell application "TextEdit"
   set text of document 1 to page_source
end tell
--> * you do not need "activate" the apps...

Or, 1-line-economic-code:

tell application "TextEdit" to set text of document 1 to («class conT» of document 1 of application "Safari")

More specifically, as far as TextEdit is concerned, a ‘document’ is more than a string of characters (which is what you get from the source of document 1 in Safari).

Therefore you need to be more explicit in telling TextEdit what you want to do.

The dictionary for Text Edit defines a ‘document’ as:

Class document: A TextEdit document.
Plural form:
Super classes:
Sub classes:
	class type class  [r/o]  -- (inherited from the ?item? class) The class of the object.
	properties record  -- (inherited from the ?item? class) All of the object's properties.
	path Unicode text  -- (inherited from the ?document? class) The document's path.
	modified boolean  [r/o]  -- (inherited from the ?document? class) Has the document been modified since the last save?
	name Unicode text  -- (inherited from the ?document? class) The document's name.
	text text  -- (inherited from the ?document? class) The text of the document.

so you can see why setting document 1 to a string of text won’t work - it doesn’t have all the other properties of a document that TextEdit wants (path, name, modified flags, etc.).

By telling TextEdit to set the text of document 1 to page_source, it knows exactly what to do and where to put the source.

You’ll find this very common in AppleScript. You need to be explicit in what you want to do and you need to think a little bit on the application’s level (e.g. what does TextEdit think a document is, not just whatyou think it is.