Paste into BBEdit in Background

Hi folks. Got a strange one for ya.

I’m using Chrome to research. I want to take the current URL (which I can get via applescript) and paste it into the current focus in a BBEdit file that’s taking my notes. The challenge is that I don’t want to have to go into BBEdit to run a script that does this procedure. I want to stay in Chrome, and just fire off the current URL, and a <keystroke code 76> (that’s a new line) to BBEdit.

This is what I currently have, but it’s not working, because of no focus on BBEdit as a target:


tell application "Google Chrome" to set myLoc to (get URL of active tab in front window)
set the clipboard to myLoc
delay 1

tell application "BBEdit"
	tell application "System Events" to keystroke "v" using command down in current window
	tell application "System Events" to key code 76
end tell

Any ideas helpful without activating BBEdit, THEN re-activating Chrome? I know there’s an answer out there.

Cheers

Hi.

BBEdit’s scriptable anyway, so there’s no need to use GUI Scripting or the clipboard:

tell application "Google Chrome" to set myLoc to (get URL of active tab in front window)

tell application "BBEdit"
	tell front text window
		set existingText to its text
		set its text to existingText & (myLoc & linefeed)
	end tell
end tell

OK, how do I get BBEdit to focus on the current location? I don’t want to append the url to the current text.

I originally was using a LaunchBar action that pasted. That’s why I chose the function.

Does this do what you want?

tell application "Google Chrome" to set myLoc to (get URL of active tab in front window)

tell application "BBEdit"
	tell front text window
		set currentSelection to its selection
		set chrOffset to currentSelection's characterOffset
		set currentSelection's contents to (myLoc & linefeed)
		select insertion point before character (chrOffset + (count myLoc) + 1)
	end tell
end tell

Or, come to think of it, a simpler solution:

tell application "Google Chrome" to set myLoc to (get URL of active tab in front window)

set the clipboard to myLoc & linefeed

tell application "BBEdit" to paste

Excellent. Perfect. Very direct, knowing that BBEdit has internal application focus already.

Cheers