How to paste the clipboard to a specific row in BBEdit file

Is it possible to expand my text macro in such a way as to tell it to paste the system-clipboard (always) to a specific row (row84) in an existing BBEdit file?

Usage: the latest entry will always be at top and the procedure would simplify the process.

Can this be done with KM or maybe AppleScript?

/
best regards,
OmarKN

You could use Select or show a menu item to trigger the menu item Go > Line number… (or, if you prefer, use the Type a keystroke action to type the shortcut, ⌘ J).

1 Like

There are (at least) two ways of AppleScripting this:

tell application "BBEdit"
	tell document 1
		set contents of line 84 to (get the clipboard)
	end tell
end tell

...or:

tell application "BBEdit"
	tell document 1
		tell line 84
			paste
		end tell
	end tell
end tell
4 Likes

document 1 seems to be a placeholder, but the path of the file didn't help,
like so

tell application "BBEdit"
	tell "/Users/okn/SitesMacHD…n-Realities.php"

How to tell the AScr. which file it is, or should document 1 be the topmost file?

/okn

In addition to being AppleScriptable, BBEdit is AppleScriptRecordable.

Give this a try:

  1. In BBEdit open a document with at least 84 lines.

  2. Open Script Editor and click the round red Record button.

  3. Switch back to BBEdit using something like command + tab.

  4. Either select the Go menu and click Line Number menu item or press command + J as @kevinb suggested.

  5. Type in 84 and click the Go button.

  6. Switch back to Script Editor and click the square Stop button.

Script Editor should type something similar to @Nige_S's script for you. This is what I got:

It probably won't be exactly what you had in mind, but I find it sometimes shows me useful classes, properties and syntax.

4 Likes

document 1 is the frontmost document, the one that's currently active.

If you want to target a document by its properties (name, file path, or similar) you can filter every document with the appropriate whose clause then use the first item of the resulting list:

tell application "BBEdit"
	tell item 1 of (every document whose name is "Solo A1.kmmacros")
		return (count characters)
	end tell
end tell

You'll want to include error checking etc, but that's the basics.

Yes I got

tell application "BBEdit"
	activate
	select insertion point before line 84 of document 1
end tell

However, in your example, you start with the Finder, and I need the path, how did you do it?

How to start with Finder?

(I could probably adapt your Finder section for the PATH.)

/okn

This I set up:

DO I need to define 'document 1 as MYFILENAME?

tell application "BBEdit"
	tell item 1 of (every document whose name is "MYFILENAME.php")
		return (count characters)
		select insertion point before line 84 of document 1
		
	end tell
end tell

No.

You're always targeting something in AS. So

tell application "BBEdit"
...

...is setting the target of the next command(s) to be BBEdit.

Similarly,

select insertion point before line 84 of document 1

...makes the target of select insertion point to be document 1, even if you are already targeting a different document.

Your code also includes a return statement -- your script will exit there and not even execute the select insertion point line.

You want something like:

tell application "BBEdit"
	tell item 1 of (every document whose name is "MYFILENAME.php")
		select insertion point before line 84
	end tell
end tell

...although you still need to insert your Clipboard text in some way.

Why do you need a path, specifically?

What you need is some way to distinguish "the document I want to add text to" from all the other documents you might have open in BBEdit. If it will always be the frontmost document then use document 1. If it will always have a certain name and that name will be unique amongst your open documents then you can use the form item 1 of (every document whose name is "MYFILENAME.php"). If you want precise targeting you can use the file path, remembering that BBEdit expects a Mac-style path and that you can't use whose so have to check each document in turn to find your match:

tell application "BBEdit"
	repeat with eachDoc in (get every document)
		if (file of eachDoc) as text is "Macintosh HD:Users:nigel:Desktop:For Forum:Solo A1.kmmacros" then
			select insertion point before line 84 of eachDoc
			exit repeat
		end if
	end repeat
end tell

If you are opening the document too, save a reference at that stage:

tell application "BBEdit"
	set theDoc to (open file "Macintosh HD:Users:nigel:Desktop:For Forum:Solo A1.kmmacros")
	select insertion point before line 84 of theDoc
end tell

As you can see, there are many ways to do things depending on what you actually want to achieve. And, as always in macro writing or programming, the more precise your plan the quicker you'll be able to create a working solution.

...leaves too much to guess-work, so you are either going to have to pick from the various options and make them work for you or write a more complete plan.

1 Like

This works like a dream!

The Clipboard is pasted in the following step with native KM.

Then I don't need a path, it is probably enough to call different/ second file, every document whose name is …, such as MYFILENAME_2.php

So there will be several macro/ AScr. iterations to do the paste/ insert to other files.

This is really very elegant. Thank you!

/okn

Hej @CRLF The fact that AppleScript is recordable is a game changer.

That way it is possible experiment with AScr. - at least for the most rudimentary examples/ usages.

Thank you.

/okn

1 Like

Yeah, sorry about that.

I should have mentioned that the Finder is also recordable.

I should have had you press the Script Editor's record button as step 1 to capture the Finder's Open path.

1 Like

Remember that it is inefficient to alternate AS and KM Actions in one macro because each AS has an instantiation cost in both time and resources (if that's what you are actually doing). So you don't want to:

AS to set insertion point in doc1
KM to Paste
AS to set insertion point in doc2
KM to Paste
...etc

Better, if you can, to either set up all your documents in one go then use KM's "Bring Window to Front" and "Paste" Actions:

...or, since BBEdit is "plain text", collect your text into KM variables then do everything in one AS:

Note that you don't need to use the "first item of a list of documents named...." format, which was me incorrectly carrying over similar from Finder. BBEdit is quite OK with you targeting the document by name, even when you have multiple windows open on the same document.

This is very interesting, to be able to collect different clipboards and paste them into different documents in one go!

My case is slightly different, and I would prefer to deal only with one receiving file (FILE A) at a time.

So, for another sort of semantics /or text content, an adjusted macro (different keys) will be created and sent to another file (FILE B).

I like to test your last AS example even if it is only for 1 file.

And AS has been around for so long, and it's still running, but KM is best!