popclip插件开发

2022/10/04 Tools 共 2969 字,约 9 分钟

popclip插件开发

js开发插件

官方教程

现有插件和其代码: https://github.com/pilotmoon/PopClip-Extensions

Extension Snippets

简单版本,选择后就可以弹出安装窗口了

# popclip js + multi action example
name: Markdown Formatting
requirements: [text, paste]
actions:
- title: MdInlineCode
  icon: circle filled IC
  javascript: popclip.pasteText('`' + popclip.input.text + '` ')  
- title: MdCode
  icon: circle filled C
  javascript: popclip.pasteText('\n```sh\n' + popclip.input.text + '\n```')  
- title: Markdown Bold # note: actions have a `title`, not a `name`
  icon: circle filled B
  javascript: popclip.pasteText('**' + popclip.input.text + '**')
- title: Markdown Italic
  icon: circle filled I
  javascript: popclip.pasteText('*' + popclip.input.text + '*') 

Extension Snippets with Python

# popclip shellscript nested in an applescript 
name: CopyNotes
applescript: do shell script "python3 ~/github/scripts/py/copy_notes/popclip_using.py '{popclip text}' H2"

"osascript -e 'display notification \"开发机通知\" with title \"开发机消息\"'"

/Users/mafulong/github/scripts/py/copy_notes/popclip_using.py

# popclip shellscript nested in an applescript 
name: Say
applescript: do shell script "echo $PATH > ~/abxx1"


# popclip shellscript nested in an applescript 
name: CopyNotes
actions:
- title: Markdown Note
  icon: circle filled H2
  applescript: do shell script "python3 ~/github/scripts/py/copy_notes/popclip_using.py '{popclip text}' H2"
- title: Markdown Note
  icon: circle filled H3
  applescript: do shell script "python3 ~/github/scripts/py/copy_notes/popclip_using.py '{popclip text}' H3"
- title: Markdown Note
  icon: circle filled T
  applescript: do shell script "python3 ~/github/scripts/py/copy_notes/popclip_using.py '{popclip text}' TXT"
- title: Markdown Note
  icon: circle filled H
  applescript: do shell script "python3 ~/github/scripts/py/copy_notes/popclip_using.py '{popclip text}' HREF"
- title: Markdown Note
  icon: circle filled C
  applescript: do shell script "python3 ~/github/scripts/py/copy_notes/popclip_using.py '{popclip text}' CODE"

可以直接选择复制。

对应python 脚本

# coding: utf8
# MIT License

import json
import uuid
import os
import subprocess
import time
import sys

filename = "/Users/mafulong/Documents/copyNotes.md"
try:

    maction = [
        ("H2", lambda x: "\n## " + x + "\n"),
        ("H3", lambda x: "\n### " + x + "\n"),
        ("TXT", lambda x: x+"\n\n"),
        ("HREF", lambda x: f"[{x}]({x})\n"),
        ("CODE", lambda x: f'''```scala\n{x}\n```\n\n''')
    ]

    with open(filename, "a+") as f:
        timenow = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        if len(sys.argv) < 3:
            f.write(timenow + "------" + "参数不全\n\n")
        else:
            selecttext = sys.argv[1]
            action = sys.argv[2]
            res = None
            for (a, b) in maction:
                if a == action:
                    res = b(selecttext)
                    break
            if res:
                f.write(res)
except Exception as ex:
    with open(filename, "a+") as f:
        f.write(ex)


但注意这种方式对长度有限制,因此长文本不可行。

Extension Package

多段一起复制时会丢换行,手动复制却没啥问题,可能是typora做的。

那就继续维持复制模式吧。

临时文件

{
  "identifier": "com.mfl.popclip.extension.md_notes",
  "name": "MdNotes",
  "icon": "quiver.png",
  "popclipVersion": 3785,
  "description": "Send the selection to Python.",
  "note": "7 May 2022: Updated to clip as code block when holding ⌥.",
  "captureHtml": true,
  "actions":[
    {
      "title": "Md note",
      "icon": "circle filled H1",
      "scriptInterpreter": "python3",
      "shellScriptFile": "quiver.py"
    }
  ]
}

filename = "/Users/mafulong/Documents/copyNotes.md"
import os
with open(filename, "a+") as f:
    text = os.environ['POPCLIP_TEXT']
    f.write(text)

Search

    Table of Contents