在Mac OS上处理剪贴板内容?


0

我想看看是否有可用的工具或脚本可以动态修改剪贴板内容。

通常在我编写代码时,我想采用一串文本并将其作为一个slug。而不是将内容重新键入slug格式,如果在复制时将其更改为slug,那将是非常棒的。

例如:“与朋友一起烧烤”→“烧烤与朋友”

知道任何可以帮助解决此问题的应用程序或Applescripts吗?非常感激。

Answers:


0

我已经将这里这里的脚本组合在一起创建了一个脚本,它将对剪贴板中的任何内容进行重击。

set theclip to the clipboard contents
on normalize(the_string)
    set p_script to ¬
        "# -*- coding: utf-8 -*-
import unicodedata, sys

def normalize(x):
    normal_form_1 = 'NFKD'
    normal_form_2 = 'NFC'
    x = unicodedata.normalize(normal_form_2, x)
    x = x.lower()
    x = x.replace(u'ß', u'ss')
    x = x.replace(u'å', u'aa')
    x = unicodedata.normalize(normal_form_1, x)
    x = u''.join([c for c in x if not unicodedata.combining(c)])
    x = x.encode('utf-8')
    return x
arg = sys.argv[1].decode('utf-8')
x = normalize(arg)
print x"

    set p_script to quoted form of p_script
    set the_string to quoted form of the_string

    return (do shell script ("python -c " & p_script & " " & the_string))
end normalize
on change_case(this_text)
    set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    set the source_string to "abcdefghijklmnopqrstuvwxyz"
    set the new_text to ""
    repeat with this_char in this_text
        set x to the offset of this_char in the comparison_string
        if x is not 0 then
            set the new_text to (the new_text & character x of the source_string) as string
        else
            set the new_text to (the new_text & this_char) as string
        end if
    end repeat
    return the new_text
end change_case
on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

set theresult to normalize(theclip)
set theresult to replace_chars(theresult, " ", "-")
set theresult to change_case(theresult)

编辑1如果要将字符串强制用作URL,请将其添加到该脚本的末尾:

set theresult to replace_chars(theresult, "%", "%25")
set theresult to replace_chars(theresult, "<", "%3C")
set theresult to replace_chars(theresult, ">", "%3E")
set theresult to replace_chars(theresult, "#", "%23")
set theresult to replace_chars(theresult, "{", "%7B")
set theresult to replace_chars(theresult, "}", "%7D")
set theresult to replace_chars(theresult, "|", "%7C")
set theresult to replace_chars(theresult, "\\", "%5C")
set theresult to replace_chars(theresult, "^", "%5E")
set theresult to replace_chars(theresult, "~", "%7E")
set theresult to replace_chars(theresult, "[", "%5B")
set theresult to replace_chars(theresult, "]", "%5D")
set theresult to replace_chars(theresult, "`", "%60")
set theresult to replace_chars(theresult, ";", "%3B")
set theresult to replace_chars(theresult, "/", "%2F")
set theresult to replace_chars(theresult, "?", "%3F")
set theresult to replace_chars(theresult, ":", "%3A")
set theresult to replace_chars(theresult, "@", "%40")
set theresult to replace_chars(theresult, "=", "%3D")
set theresult to replace_chars(theresult, "&", "%26")
set theresult to replace_chars(theresult, "$", "%24")

0

我使用这样的函数:

f() {
  (. ~/Scripts/f; "$@")
}

ff() {
  pbpaste | f "$@" | pbcopy
}

(我~/Scripts/f更常使用emacs和OS X文本视图中的函数。)

~/Scripts/f 可能包含这样的函数:

slug() {
  LC_ALL=C tr A-Z a-z | LC_ALL=C tr -cs a-z0-9 - | sed 's/^-//;s/-$//'
}

tr -c补充并将tr -s多个连字符压缩成单个连字符。


0

我有一个脚本,对文件名执行类似的操作,但我用下划线替换空格。你可以使用类似的东西:

pbpaste | sed -e 's/ /-/g' | awk '{print tolower($0)}' | pbcopy 

结合一点点AppleScript形成:

do shell script "pbpaste | sed -e 's/ /-/g' | awk '{print tolower($0)}' | pbcopy" 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.