Python: 读取 ASCII 字符并点击输出
Python: Reading characters in ASCII and giving an output with clicks
我是 python 的新手,如果这是非常基本的或者完全不可能做到的事情,我很抱歉。
我想做的是创建一个脚本,该脚本将读取字符,然后根据读取的字符在 ASCII 二进制文件中输出左键或右键单击。左键单击为 0s,右键单击为 1s。所以字母 A 将是 01000001 将输出左键单击右键单击左键单击左键单击左键单击左键单击左键单击右键。此外,每个字符都需要反转,前面还有 1 个,后面有 10 个,所以 'A' 现在是 11000001010。
这就是我所拥有的,它确实有效(将其用作 applescript 中的 shell 脚本)每次点击需要 0.3 秒的延迟,每个字母需要 3 秒的延迟。
delay 1
set x to 300
set y to 300
do shell script "
/usr/bin/python <<END
import sys
import time
from Quartz.CoreGraphics import *
def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def rightClick(posx,posy):
mouseEvent(kCGEventRightMouseDown, posx,posy);
mouseEvent(kCGEventRightMouseUp, posx,posy);
def leftClick(posx,posy):
mouseEvent(kCGEventLeftMouseDown, posx,posy);
mouseEvent(kCGEventLeftMouseUp, posx,posy);
ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent); # Save current mouse position
#reads the letter A in ASCII (01000001)
rightClick(" & x & "," & y & "); #1 Tail
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - 8
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 7
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 6
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 5
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 4
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 3
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - 2nd bit
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 1st bit
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - Leading
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - Finish
time.sleep(0.3);
time.sleep(3);
#reads the letter B in ASCII (01000010)
rightClick(" & x & "," & y & "); #1 Tail
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 8
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - 7
time.sleep(0.3);
leftClick(" & x & "," & y & "); #1 - 6
time.sleep(0.3);
leftClick(" & x & "," & y & "); #1 - 5
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 4
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 3
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - 2nd bit
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 1st bit
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - Leading
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - Finish
time.sleep(0.3);
time.sleep(3);
mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position
END"
所以我想要做的是要么读取一个包含单词的 .txt 文件,要么让程序要求用户输入单词,它会自动转换为 ASCII,然后开始点击。那有可能吗?
您可以使用一个循环(针对字符串中的每个字母)和另一个循环(针对二进制文本中的每个字符)
set pos to "300, 300"
set t to quoted form of "AB"
do shell script "/usr/bin/python <<END
import binascii, sys, time
import Quartz.CoreGraphics as qcg
def mouseEvent(type, lrBtn, p):
theEvent = qcg.CGEventCreateMouseEvent(None, type, (p), lrBtn)
qcg.CGEventPost(qcg.kCGHIDEventTap, theEvent)
time.sleep(0.3)
def mousemove(p):
mouseEvent(qcg.kCGEventMouseMoved, qcg.kCGMouseButtonLeft, p)
def rightClick(lrBtn, p):
mouseEvent(qcg.kCGEventRightMouseDown, lrBtn, p)
mouseEvent(qcg.kCGEventRightMouseUp, lrBtn, p)
def leftClick(lrBtn, p):
mouseEvent(qcg.kCGEventLeftMouseDown, lrBtn, p)
mouseEvent(qcg.kCGEventLeftMouseUp, lrBtn, p)
tString=" & t & "
pos=" & pos & "
currentpos=qcg.CGEventGetLocation(qcg.CGEventCreate(None))
t = (bin(int(binascii.hexlify(tString),16)))[2:]
ascText = -len(t) % 8 * '0' + t
for i in range(0, len(ascText), 8): ### to every 8 digit
ascT = '1' + (ascText[i:i+8])[::-1] + '10'
for c in ascT:
if (c=='1'):
rightClick(qcg.kCGMouseButtonRight, pos)
else: leftClick(qcg.kCGMouseButtonLeft, pos)
time.sleep(3);
mousemove(currentpos) # Restore mouse position
END"
更新 :
转换 space 或其他一些字符给出 7 个二进制数字,脚本必须在它之前添加一个零。
脚本将转换字符串,而不是检查每个字符,每个字符给出 8 个二进制数字,只检查第一个字符。
使用编辑后的脚本
我是 python 的新手,如果这是非常基本的或者完全不可能做到的事情,我很抱歉。
我想做的是创建一个脚本,该脚本将读取字符,然后根据读取的字符在 ASCII 二进制文件中输出左键或右键单击。左键单击为 0s,右键单击为 1s。所以字母 A 将是 01000001 将输出左键单击右键单击左键单击左键单击左键单击左键单击左键单击右键。此外,每个字符都需要反转,前面还有 1 个,后面有 10 个,所以 'A' 现在是 11000001010。
这就是我所拥有的,它确实有效(将其用作 applescript 中的 shell 脚本)每次点击需要 0.3 秒的延迟,每个字母需要 3 秒的延迟。
delay 1
set x to 300
set y to 300
do shell script "
/usr/bin/python <<END
import sys
import time
from Quartz.CoreGraphics import *
def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def rightClick(posx,posy):
mouseEvent(kCGEventRightMouseDown, posx,posy);
mouseEvent(kCGEventRightMouseUp, posx,posy);
def leftClick(posx,posy):
mouseEvent(kCGEventLeftMouseDown, posx,posy);
mouseEvent(kCGEventLeftMouseUp, posx,posy);
ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent); # Save current mouse position
#reads the letter A in ASCII (01000001)
rightClick(" & x & "," & y & "); #1 Tail
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - 8
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 7
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 6
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 5
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 4
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 3
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - 2nd bit
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 1st bit
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - Leading
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - Finish
time.sleep(0.3);
time.sleep(3);
#reads the letter B in ASCII (01000010)
rightClick(" & x & "," & y & "); #1 Tail
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 8
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - 7
time.sleep(0.3);
leftClick(" & x & "," & y & "); #1 - 6
time.sleep(0.3);
leftClick(" & x & "," & y & "); #1 - 5
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 4
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 3
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - 2nd bit
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - 1st bit
time.sleep(0.3);
rightClick(" & x & "," & y & "); #1 - Leading
time.sleep(0.3);
leftClick(" & x & "," & y & "); #0 - Finish
time.sleep(0.3);
time.sleep(3);
mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position
END"
所以我想要做的是要么读取一个包含单词的 .txt 文件,要么让程序要求用户输入单词,它会自动转换为 ASCII,然后开始点击。那有可能吗?
您可以使用一个循环(针对字符串中的每个字母)和另一个循环(针对二进制文本中的每个字符)
set pos to "300, 300"
set t to quoted form of "AB"
do shell script "/usr/bin/python <<END
import binascii, sys, time
import Quartz.CoreGraphics as qcg
def mouseEvent(type, lrBtn, p):
theEvent = qcg.CGEventCreateMouseEvent(None, type, (p), lrBtn)
qcg.CGEventPost(qcg.kCGHIDEventTap, theEvent)
time.sleep(0.3)
def mousemove(p):
mouseEvent(qcg.kCGEventMouseMoved, qcg.kCGMouseButtonLeft, p)
def rightClick(lrBtn, p):
mouseEvent(qcg.kCGEventRightMouseDown, lrBtn, p)
mouseEvent(qcg.kCGEventRightMouseUp, lrBtn, p)
def leftClick(lrBtn, p):
mouseEvent(qcg.kCGEventLeftMouseDown, lrBtn, p)
mouseEvent(qcg.kCGEventLeftMouseUp, lrBtn, p)
tString=" & t & "
pos=" & pos & "
currentpos=qcg.CGEventGetLocation(qcg.CGEventCreate(None))
t = (bin(int(binascii.hexlify(tString),16)))[2:]
ascText = -len(t) % 8 * '0' + t
for i in range(0, len(ascText), 8): ### to every 8 digit
ascT = '1' + (ascText[i:i+8])[::-1] + '10'
for c in ascT:
if (c=='1'):
rightClick(qcg.kCGMouseButtonRight, pos)
else: leftClick(qcg.kCGMouseButtonLeft, pos)
time.sleep(3);
mousemove(currentpos) # Restore mouse position
END"
更新 : 转换 space 或其他一些字符给出 7 个二进制数字,脚本必须在它之前添加一个零。
脚本将转换字符串,而不是检查每个字符,每个字符给出 8 个二进制数字,只检查第一个字符。
使用编辑后的脚本