forked from AndrewTerekhine/xkeys
/
SystemUtil.py
63 lines (51 loc) · 1.54 KB
/
SystemUtil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Set of system utils.
Author Andrew Terekhine
Since 2011-11-23
"""
import os
import subprocess
import string
import ClipboardUtil
import SendToUi
import Message
import Common
import SendToUi
"""
Gets window name by process id
"""
def getWindowNameByProcessId(processId):
windowName = os.popen("wmctrl -l -p | grep '" + processId + "'").read().rstrip()
return windowName
def getSelection():
storedClipboard = SendToUi.beforeSetClipboard()
text = ClipboardUtil.pasteFromClipboardXsel()
SendToUi.afterSetClipboard(storedClipboard)
return text
def getUserFolder():
return os.getenv("HOME")
def transformUrlBeforeOpen(url):
url = string.strip(url) # strips leading and trailing white spaces
if string.find(url, Common.WWW_PREFIX) == 0:
url = Common.HTTP_PREFIX + url
if string.find(url, Common.USER_FOLDER_PREFIX) == 0:
url = string.replace(url, Common.USER_FOLDER_PREFIX, getUserFolder())
# todo add customization
if string.find(url, "D:\Dropbox") == 0:
url = string.replace(url, "D:\Dropbox", "Dropbox")
url = string.replace(url, "\\", "/")
return url
def openSelection(app, url):
text = getSelection()
url = transformUrlBeforeOpen(url + text)
subprocess.Popen([app, url])
return None
def runSelection():
text = getSelection()
common = transformUrlBeforeOpen(text)
subprocess.Popen(common, shell=True)
return None
def getProcessOutput(commandText):
return os.popen(commandText).read().rstrip()