2013-04-04 105 views
2

我在Ubuntu 12.04上的WINE上運行Foxit Reader。我想將文本複製並粘貼到書籤中,但我需要將其大寫(例如,融合變爲Fusion)。我想按F5並運行一個python腳本。我知道這是可能的Autokey,但後者有一個documented bug剪貼板處理。在Linux中獲取和設置X11剪貼板

所以,現在我正在尋找Autokey的剪貼板替代品。如果我的python腳本運行一個shell,那麼shell可能會訪問剪貼板? xclip看起來很有希望,但其文檔中提到:「從標準或一個或多個文件中讀取數據,並將數據作爲X選擇粘貼到X應用程序中。」我不需要標準或文件;我需要適當的X11剪貼板(又名選擇)。

簡而言之,python或shell如何讀取現有的X11剪貼板?

回答

2
xclip -o | sed 's/^./\U&/g' | xclip -i 

這將讀取X剪貼板,利用的內容和覆蓋它

+0

我用下面的縮進代碼爲SE:'XCLIP -o | sed's:^::g'| xclip選擇剪貼板。 – 2013-04-04 22:51:36

+0

這比我的詳細。謝謝! – 2013-04-04 22:57:50

+0

@醇厚 - 黃色,如果沒有更好的表現出來,它解決了你的問題,可以自由地接受它作爲答案;-)(p.s.我刪除了-e,因爲它是沒有必要的) – 2013-04-05 10:07:17

1

我意識到-o參數讀取的選擇,但你必須指定你需要:

xclip -selection clipboard -o 

從那裏,我用這個StackOverflow answer。它很好地工作。

#read clipboard, avoid autokey's get_selection bug 
tag = subprocess.Popen(["xclip","-selection", "clipboard", "-o"],stdout=subprocess.PIPE).communicate()[0] 

#https://stackoverflow.com/questions/764360/a-list-of-string-replacements-in-python 
mapping = { "'":'', ',':'', '"':'', ';':'', '(':'', ')':'', '.':'', '-':' '} 
for k, v in mapping.iteritems(): 
    tag = tag.replace(k, v) 

#Camelcase, remove spaces, and append Caesar tag 
tag=tag.title().replace(' ','')+"_"