Modification of safari selection to script

OK, I admit I’m new to all this but hopefully quick to learn.

Could some one help me convert Rob’s script: “safari selection to script” to “safari selection to BBEdit” without all the commented info at the beginning of the document it creates in BBEdit?

In other words just the selected text would appear in BBEdit and nothing else. Changing the script to run in BBEdit isn’t the issue, it’s getting rid of all the heading info.

I promise I’ll watch carefully and learn quickly.

Thanks,
Bill

Here’s the basic code:

tell application "Safari" to ¬
	set selection_ to (do JavaScript "getSelection()" in document 1) as string

if selection_ is not "" then
	tell application "BBEdit 6.5" to ¬
		make new text window with properties {text:selection_}
else
	display dialog "Nothing selected in Safari's front window."
end if

– Rob

see http://scriptbuilders.net/category.php?search=1398

Rob:

I may be doing something wrong but when I run your code with a selection in Safari nothing happens. If I run it without a selection in Safari, it does produce the dialog which says nothing is selected. Script Editor returns the result:

“text window 1 of application “BBEdit””

Any ideas

Bill

Bill, Rob’s code works for me. But it won’t work on text that is in a form. Try highlighting the line of text below, and then run the script…

Here is some text for Bill to test Rob’s script.

Bill,

I just tested the code again and it works as expected. Do you have JavaScript enabled in Safari? Do any of the other Safari selection scripts work for you? Did you try Greg’s script?

– Rob

I do have JavaScript enabled.

Hmm

Bill

Greg & Rob:

I beg your pardon!

Since the script doesn’t activate BBEdit, I didn’t realize it’s been dumpiing the copied text into BBEdit all along … Ouch!

Sorry guys and thanks again.

Bill

This will grab the entire source of the front Safari document.

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

– Rob

As far as I know, the only way to do that is with UI scripting. UI scripting is included in Panther and requires the (no longer available) beta version of System Events in Jaguar.

tell application "Safari" to activate
tell application "System Events" to keystroke "c" with {command down}
set cb to the clipboard

– Rob

This requires GUI scripting (built-in on 10.3). Open a source window, select some text, then run this script:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

This must be something new in Panther’s UI scripting vocabulary because it won’t even compile for me with the System Events beta in Jaguar. Nice!

Jon, does this work even if Safari isn’t frontmost?

– Rob

Yes, as long as the source window is the front window in Safari, it doesn’t matter if Safari is frontmost. I’m surprised this doesn’t compile in 10.2 with the GUI scripting beta.

Jon

Which script did you try? Which version of OS X are you running? Which version of System Events (use the script below to find out)?

tell application "System Events" to version
display dialog "System Events version: " & (result as text)

– Rob

Did you go to the Universal Access pref pane and “Enable access for assistive devices”? It’s required for GUI scripting.

– Rob

I’m getting the same error that Rob gets with Jon’s script, but it goes away after I select some text in the view source window.

  1. Copy/Paste the script and run = the error (NSReceiver… 4)
  2. open a view source window from Safari, run script with no text selected = error (NSReceiver… 4)
  3. select some text in the view source window, run the script and it works.

http://scriptbuilders.net


[This script was automatically tagged for color coded syntax by Script to Markup Code]

You’re doing better than me. It won’t even compile here. :rolleyes:

– Rob