<commit_before>---
layout: post
category: Tools
title: popclip插件开发
tags: Tools
---

## popclip插件开发

[js开发插件](https://sspai.com/post/73404)



[quickref](https://quickref.me/)

[case-anything](https://github.com/mesqueeb/case-anything)

 [The Noun Project](https://thenounproject.com/)

[SF Symbols](https://sfsymbols.com/)

[ICU specification](https://unicode-org.github.io/icu/userguide/strings/regexp.html)



[官方教程](https://github.com/pilotmoon/PopClip-Extensions)



看起来可以js, applescript, shell实现。不能python...
<commit_msg>add logbook<commit_after>---
layout: post
category: Tools
title: popclip插件开发
tags: Tools
---

## popclip插件开发

[js开发插件](https://sspai.com/post/73404)

[官方教程](https://github.com/pilotmoon/PopClip-Extensions)

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



### Extension Snippets

```sh
# 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 脚本

```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做的。

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

临时文件

```sh
{
  "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"
    }
  ]
}

```



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


{'fraction_non_alphanumeric': 0.15633912367124708, 'fraction_numerical': 0.01011148561057817, 'mean_word_length': 3.175324675324675, 'pattern_counts': {'":': 12, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6641222', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1460, 'n_words': 326}