Copy filename to clipboard

If I copy selected text from a text file, is there a way to append the filename to the clipboard so that when I paste, the filename will paste above the selected text?

I imagine there is a relatively easy way to do this with an applescript, but I only know a little applescript- if anyone could point me in the right direction with where to start, it would be very helpful.

Thank you!

Yes, this is very possible (and it seems like I’ve rewritten it it over and over again :smiley: ).
What do you intend to do with the output?

I just want to paste the output into another text file, but I want the filename of the original text file to print above the pasted text.

This is a simple example, but bear in mind you will lose formatting.

tell application id "DNtp"
	if exists (content record) then
		set recordName to name of content record
		set the clipboard to (recordName & return & (get the clipboard))
	end if
end tell

This is also not checking to see if the current document is the source of the clipboard content. This is assuming you’ve copied something to the clipboard already.

You could also do something like this…

tell application id "DNtp"
	if exists (content record) then
		set selectedText to (selected text of think window 1)
		tell content record
			set currentText to plain text
			set recordName to name
			if selectedText ≠ currentText then
				set the clipboard to (recordName & return & selectedText)
			end if
		end tell
	end if
end tell

This gets the name and the selection of the current document.

This is wonderful! Thank you! Is there a way to make the filename a link to the file itself?

Yes, but…

Remember that computers are incredibly stupid. They are just blazingly fast at following instructions! They need to be told eactly what to do, as each process involves different steps or have different difficulties. For example, if you were making a pie, there are different requirements and variations in steps if you were making a pumpkin pie versus an apple pie.

You said…

What is a “text file”?

  • Plain text? Won’t support active links. (PS: This is what I hear when I hear “text file”.)
  • Markdown? Requires specific formatting to create an active link.
  • HTML? Requires specific formatting and needs to be inserted under-the-hood.
  • RTF? Requires very strict specific formatting and needs to be inserted under-the-hood.

So each of these options presents different things to consider and (try to) code for.
Does that make sense?

Now, what kind of “text file” are you referring to? :smiley:

It would be an RTF file.

RTF is much more difficult to implement since it has to be inserted into the internals of the document or the link will be pasted as text. You’d also need to be targeting a specific RTF file to make this happen.

Just copying the item link (via right-click or Edit menu), allows pasting into an RTF document with a valid link.