Add selected text to current TextEdit document

Hi hi dear Mr. Nick,

Is it possible to add selected text to the specific Mac TextEdit?
if the existing text file end like「I love Popclip」,
and I selected “and Nick” form somewhere else,
it auto become to「I love Popclip and Nick」?

thank uuu <3

1 Like

Something like:

#popclip 
name: TextEdit Append
icon: a
after: show-status
applescript: |
  tell application "TextEdit"
    set theDocument to first document
    set text of theDocument to (text of theDocument & " {popclip text}")
  end tell
3 Likes

Wow you are the God of Popclip!

but it seems need to open the file to do that.

You’ll need to have the file you want to append to open, yes.

Adapting this to Pages failed. Thoughts?

#popclip
name: Pages Append
icon: P
after: show-status
applescript: |
tell application “Pages”
set theDocument to first document
set text of theDocument to (text of theDocument & " {popclip text}")
end tell

I suspect it’s an AS error, as compiling it in Script Editor gives syntax error:
Expected expression, property or key form, etc. but found unknown token.

The problem is the quotation marks around the word Pages. You have curly quotation marks but AppleScript requires straight quotes.

(Probably happened if typed into an editor with “smart quotes” turned on).

This with straight quotes returns “Invalid YAML” in TextEdit as plain text.

#popclip
name: Pages Append
icon: P
after: show-status
applescript: |
tell application “Pages”
set theDocument to first document
set text of theDocument to (text of theDocument & " {popclip text}")
end tell

Thoughts?

When composing a snippet it is important to ensure that the snippet itself is a valid YAML document. When using a multi-line string in YAML, with the pipe operator (|), it is necessary to indent each line of the string with spaces. Like this:

#popclip
name: Pages Append
icon: P
after: show-status
applescript: |
  tell application "Pages"
    set theDocument to first document
    set text of theDocument to (text of theDocument & " {popclip text}")
  end tell

Note also that in the forum editor, I typed ``` on the lines before and after the code to format it as a code block. This should help avoid the possibility of the forum software adding smart quotes.

The above code should work. However, I recommend using the newer style snippet format, which avoids the fiddly indentation requirement, like this:

-- #popclip
-- name: Pages Append
-- icon: P
-- after: show-status
-- language: applescript
tell application "Pages"
  set theDocument to first document
  set text of theDocument to (text of theDocument & " {popclip text}")
end tell

An advantage of this format is that the whole snippet is valid AppleScript code so you can edit it in the Script Editor:

2 Likes

Thanks - is there a link to documentation for the new format?

1 Like

Item 6 at: New features for making extensions in PopClip 2022.12

There’s also some explanation about it in the main docs at https://github.com/pilotmoon/PopClip-Extensions#header-snippets

1 Like