正在载入...

存档

‘Script’ 分类的存档

把Win键利用起来,快速启动程序

2008年11月18日

vista 有个很好的功能就是可以用win+1-0这十个快捷键启动快速启动栏中的程序。其使用autohotkey的话xp也可以很方便的拥有这个功能。

脚本很简单:

1
2
3
#1::run path_of_firefox
#2::run path_of_tm2008
#3::run path_of_CTerm

试一下,很好用。如果想要再多来点功能呢?比如:

程序最小化的情况下,不打开新程序,直接激活已打开的程序。
程序已经激活的情况下,最小化程序。

只要用一个函数即可:

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
RunOrActivateOrHidden(Target, WinTitle = "")
{
	SplitPath, Target, TargetNameOnly
	Process, Exist, %TargetNameOnly%
	If ErrorLevel > 0
		PID = %ErrorLevel%
	Else
		Run, %Target%, , , PID
	If WinTitle <>
	{
		SetTitleMatchMode, 2
		IfWinNotActive,%WinTitle%
		{
			WinWait, %WinTitle%, , 3
			WinActivate, %WinTitle%
		}
		else{
			sleep,300
			WinMiniMize,%WinTitle%
		}
	}
	else
	{
		IfWinNotActive, ahk_pid %PID%
		{
			WinWait, ahk_pid %PID%, , 3
			WinActivate, ahk_pid %PID%
			IfWinActive,ahk_class switchwin2
				send,{enter}
		}
		else
			WinMiniMize,ahk_pid %PID%
	}
}

函数说明:

Line 3:SplitPath将路径分为两部分
Line 4:Process检测是否程序已经运行
Line 5-8:运行程序
Line 9:是否通过标题匹配
Line 12-20:激活或者最小化程序,Line 18的延时是为了防止最小化程序后程序又被激活
Line 22-33:与12-20行的作用一样,只不过是通过pid匹配

快捷键部分将run换成RunOrActivateOrHidden函数调用即可。

Script, 软件

用AHK在Windows下实现KDE风格的窗口控制

2008年11月15日

前几天用习惯了Linux以后回到Win下觉得很不爽,特别是移动窗口太过不自由了。于是找了个脚本实现Linux下的窗口控制效果。

脚本效果:
alt+左键任意位置拖拽窗口
alt+右键最小化窗口
alt+中键关闭窗口
alt+左键双击切换最大化和普通状态

首先是找了ahk帮助文档中的一个脚本Easy Window Dragging (requires XP/2k/NT)

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
; Note: You can optionally release Capslock or the middle mouse button after
; pressing down the mouse button rather than holding it down the whole time.
; This script requires v1.0.25+.
 
~MButton & LButton::
CapsLock & LButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin% 
if EWD_WinState = 0  ; Only if the window isn't maximized 
    SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return
 
EWD_WatchMouse:
GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U  ; Button has been released, so drag is complete.
{
    SetTimer, EWD_WatchMouse, off
    return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D  ; Escape has been pressed, so drag is cancelled.
{
    SetTimer, EWD_WatchMouse, off
    WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
    return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX  ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return

试了一下这个脚本,感觉很不错。拖动起来很平滑。决定在这个脚本的基础上做调整了。
这个脚本有个问题,窗口最大的时候,也能拖动窗口,就不是很美观。所以首先要解决这个问题。

使用函数WinGet来获取窗口状态,并判断:

1
2
3
4
5
6
WinGet,winstat,MinMax,ahk_id %EWD_MouseWin%
if(winstat <> 1)
{
	WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
	SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
}

另外是实现alt+左键双击的判断,用了小众软件中提到的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Keywait, LButton, , t0.2
if errorlevel = 1
	return
else
{
	Keywait, LButton, d, t0.1
	if errorlevel = 0
	{
		if(winstat = 1)
			WinRestore,ahk_id %EWD_MouseWin%
		else
			WinMaximize,ahk_id %EWD_MouseWin%
	}
}

这样左键拖拽的问题与双击切换窗口大小的问题就解决了。另外就是加入右键和中键的功能,这个比较简单:

1
2
3
4
5
6
7
8
9
10
11
ALT & RButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinMinimize,ahk_id %EWD_MouseWin%
return
 
ALT & MButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinClose,ahk_id %EWD_MouseWin%
return

试了试,效果很满意:)

最终脚本下载

Script, 软件

饮水思源文件上传工具 1.5

2008年10月22日

1.5版只支持CTerm 3.3.17,PerfectWorks精力有限(功力太浅),无暇维护太多个版本,希望大家谅解。
1.5版已经集成在CTerm饮水思源版里,推荐直接使用。

三步安装:

  1. 关闭CTerm
  2. 将文件解压到CTerm 3.3.17版文件夹下,覆盖同名文件
  3. 开启CTerm,安装结束

使用方法:

  1. 设置好CTerm的自动登录功能(注意用户名的大小写必须正确);
  2. 在资源管理器中选择需要上传的文件并复制,支持QQ等截图软件直接截图;
  3. 在发帖时点击“传图按钮或者按下快捷键(默认为Ctrl+Alt+U);
  4. 在状态栏可以看到文件上传状态,上传完成后地址自动复制到剪贴板;
  5. 如果上传失败,请查看CTerm目录下的upload.log查看失败原因。

可能的出错原因:

  1. 文件大于1MB
  2. 文件名中包含中文
  3. 未在发帖界面使用该工具
  4. 未使用自动登录
  5. 自动登录用户名大小写不正确

更新日志:
2008.10.23
1.5 released

  1. 改正 剪贴板格式兼容性问题
  2. 改正 因无法解决密码明文存储问题,暂时取消了非自动登录使用
  3. 加强 使用CTerm内建python库,程序运行更快,体积从6mb缩减为50k
  4. 加强 新的界面,使用了对话框确认操作和状态栏显示上传状况
  5. 其他 程序代码更加清晰

2008.10.12
1.0 released

Script ,

饮水思源文件上传工具 1.0

2008年10月12日

牢骚:算是python的小作业了吧,虽然折腾了我两天折腾到要吐……
另外:感谢xXx同学的帮忙调试,被折腾的想吐的人不止我一个……

YSSYFileUploader
CTerm专用的水源文件上传器,可以上传所有类型的文件。只能用于饮水思源bbs,广大CTerm同学再也不用为了传图开web了。

源代码在压缩包里

下载:饮水思源CTerm文件上传

压缩包内有CTerm 3.3.17.317和CTerm 3.299两个版本的安装文件。推荐使用这两个版本的CTerm。其他版本切勿直接覆盖,见手动修改部分。

安装有两种方法:

1.需要开启CTerm的自动登录。
关闭CTerm。将压缩包内文件按目录结构解压至CTerm文件夹下覆盖原有文件。注意原文件的备份。打开CTerm,安装完毕。

2.不需使用CTerm的自动登录,但需要手工设置账号和密码。
关闭CTerm。将压缩包内文件按目录结构解压至CTerm文件夹下覆盖原有文件。注意原文件的备份。
用记事本打开CTerm目录/script/upload.py,将第七行和第八行更改为

1
2
username = "水源用户名"
password = "水源密码"

打开CTerm,安装完毕。

使用方法:
1.资源管理器中选中需要上传的文件,Ctrl+C。CTerm窗口下按ctrl+alt+u,文件上传后址,自动粘贴至剪贴板。
2.使用截图工具截图至内存,CTerm窗口下按ctrl+alt+u,文件自动上传后,地址自动粘贴至剪贴板。

可能导致上传失败的原因:
1.文件名有中文。程序暂不支持中文文件名上传,请确保文件名为英文/数字/下划线等常规字符。
2.过于频繁使用本工具。如果一天内使用本工具次数太频繁(大概五十次以上),会被web水源暂时封禁一天……被web封禁以后不影响term的登陆,但是到今天24:00之前都无法再进行web登陆,也无法再使用本工具。
3.出现窗口一闪就消失的话,有可能是文件大小太大。

其他版本的手工安装:
像自动安装一样覆盖除了script/ct_system.py以外的所有文件。
用记事本打开CTerm目录/script/ct_system.py,找到以下内容

1
2
3
4
5
6
7
8
9
10
11
12
def OnDataCome(ID):
    #print "session %ddata comes"%ID
    #print
    #print GetText(ID,0,23)
    #print
    try:
        dataEvents[ID].set()
        dataEvents[ID].clear()
    except KeyError:
        pass
    #print 'KeyError'
    #print dataEvents, ID

添加第9-12行,注意使用tab键以保持缩进的正确

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def OnDataCome(ID):
    #print "session %ddata comes"%ID
    #print
    #print GetText(ID,0,23)
    #print
    str = GetText(ID,0)
    str = str[str.index(" [")+2:]
    str = str[:str.index("]")]
    if str:
        fp = open("YSSYFileUploader/boardstate.txt","wb")
        fp.write(str)
        fp.close()
    try:
        dataEvents[ID].set()
        dataEvents[ID].clear()
    except KeyError:
        pass
    #print 'KeyError'
    #print dataEvents, ID

然后即可使用。如果想不使用自动登录,参见安装方法2修改upload.py

下载:饮水思源CTerm文件上传

Script, 交互设计 ,

Python tutor 笔记

2008年10月10日

http://doc.chinahtml.com/Manual/Python/tut/node2.html

前言
名称起源:BBC的“Monty Python’s Flying Circus”节目,
Python是一门解释性脚本语言,
面向对象,
语句组织依赖于缩进而不是{}或者begin/end,
不需要声明变量或者参数。

Python解释器
调用解释器
1.直接启动:
[perfectworks@Debian ~]# python
2.调用脚本:
[perfectworks@Debian ~]# python script
3.直接运行语句:
[perfectworks@Debian ~]# python -c command[arg]…

参数传递:sys.argv[0]脚本名,sys.argv[1]第一个参数。

多行结构:主提示符:<<<,从属提示符...

Python脚本
单行注释:#

开头写入:

1
#! /usr/bin/env python

编码指定:

1
# -*- coding: utf-8 -*-

不直接支持GB/GBK/GB18030/ISO-10646(新版好像有所改进?)

运算
直接支持整数/浮点/复数运算,可以用变量_调出上一次的运算结果。
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
>>>

字符串与运算
单引号,双引号,三引号,
字符串分行,行末加\,
三引号与raw行:保留一切格式,行末不需加\。

字符串用+连接,用*循环,用[a:b]切出部分字符串。

字符串长度:len(str)。

Unicode字符串,引号前加u,可以用Unicode代码输入。

链表
类似数组,用中括号[]标记以逗号分隔的一系列值。
可以当做堆栈/队列使用。
链表方法:
append(x)
extend(x)
insert(i,x)
remove(x)
pop([i])
index(x)
count(x)
sort()
reverse()

filter(),返回调用函数后为true的元素;
map(),返回调用函数后的所有结果;
reduce(),调用前一次的处理结果作为下一次运算的函数参数。

链表推导式:[function(x) for x in linkList],将linkList中的每一个元素作为function的参数传入后,将返回结果输出。

元组(Tuples),序列(Sequences)
用小括号围住,以逗号分割。元组是不可改变的链表。
序列拆封:
>>> t = 12345,54321,'hello'
>>> x,y,z = t

字典(Dictionaries)
Hash表,用大括号围住,以逗号分隔,’key’:value对

循环

1
2
3
4
5
6
for key,value in dict.itmes():
   print k,v
for pos,value in enumerate(array):
   print pos,value
for valueA,valueB in zip(arrayA,arrayB):
   print valueA,valueB

流程控制

1
2
3
4
5
6
if a>b:
   print a
elif a==b:
   print a
else:
   pass

函数

1
2
3
4
5
6
def func(love,qmt="beauty")
   return love
 
func(love="yc")
 
func("yc")

赋值失败:缺少必要参数,关键字后面出现非关键字赋值,参数重复赋值,未知关键字

模块

1
2
3
4
5
6
7
import package # 引入package.py
package.test() # 调用package.py中的test函数
from package import * # 导入模块快定义中所有命名
test() # 调用了package.test()
dir(package) # 返回package中所有的定义
 
import package.pkg # 引入/package/pkg.py,package目录下比如有__init__.py

输入输出
格式化字符串,str()与repr()
格式化输出 print ‘%2d %3d %4d’ % (x,x*x,x*x*x)

文件读写

1
2
3
4
5
6
7
8
fp = open('filename','mode')
fp.read()
fp.readline()
fp.readlines()
fp.write(string)
fp.tell()
fp.seek()
fp.close()

数据类型的序量化:

1
2
pickle.dump(obj,fp)
obj = pickle.load(fp)

错误处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class ImpossibleError(Exception): # 自定义错误类
   def __init__(self,value):
       self.value = value
   def __str__(self):
       return repr(self.value)
 
try:
   print "trying"
   raise ImpossibleError, 'nothing is impossible!' # 强制抛出异常
   break
except ImpossibleError: # except加类名,捕获异常
   print "that's impossible"
except: # 捕获所有异常
   print "that's impossible,too"
 
try:
   print "trying"
   raise ImpossibleError, 'nothing is impossible!'
   break
finally:
   print "the impossible thing will happen" # 无论try是否有异常都会被执行

1
2
3
4
5
6
7
8
9
10
11
12
13
class MyClass: # 定义一个类
   def __int__(self): # 构造函数
       pass
   def __iter__(self): # 迭代器
       return self
   def next(self):
       if self.index == 0:
           raise StopIteration
       self.index = self.index - 1
       return self.data[self.index]
class MyClass2rd(MyClass): # 继承
   def __int__(self): # 构造函数
       pass

python支持多继承

发生器看的不是很明白

结束
不是结束,而是开始

Script

python调用本地程序

2008年10月8日

有三种比较简单的方法,以调用notepad查看upload.log为例

1
2
3
os.system("notepad upload.log")
win32api.ShellExecute(0,"","notepad","upload.log",".",1)
WinExec("notepad upload.log", 1)

详细参见手册

Script