Python:读取ASCII字符并通过单击给出输出

Python:读取ASCII字符并通过单击给出输出,python,macos,applescript,Python,Macos,Applescript,我是python的新手,如果这是非常基本的,或者是完全不可能做到的,那么很抱歉 我要做的是创建一个脚本,该脚本将读取字符,然后根据读取的字符以ASCII二进制格式输出左键或右键单击。左键单击为0,右键单击为1。因此字母A将是01000001,它将输出左键单击右键单击左键单击左键单击左键单击左键单击右键单击。此外,每个字符都需要颠倒,前面加1,后面加10,所以“A”现在是11000001010 这就是我所拥有的,它确实有效(在applescript中将其用作shell脚本),每次单击需要0.3秒的

我是python的新手,如果这是非常基本的,或者是完全不可能做到的,那么很抱歉

我要做的是创建一个脚本,该脚本将读取字符,然后根据读取的字符以ASCII二进制格式输出左键或右键单击。左键单击为0,右键单击为1。因此字母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"
延迟1
将x设置为300
将y设置为300
执行shell脚本“
/usr/bin/python您可以使用一个循环(用于字符串中的每个字母)和另一个循环(用于二进制文本中的每个字符)

将pos设置为“300300”
将t设置为“AB”的引用形式
执行shell脚本“/usr/bin/python您可以使用一个循环(用于字符串中的每个字母)和另一个循环(用于二进制文本中的每个字符)

将pos设置为“300300”
将t设置为“AB”的引用形式

do shell script“/usr/bin/python真是太棒了,谢谢Jack。加载时间也好多了,我猜我加载了很多不需要的库。你知道有没有办法同时读取“”空格字符(00100000)?太棒了,谢谢Jack。加载时间也快多了,我猜我加载了很多不需要的库。你知道有没有办法也读取“”空格字符(00100000)?太棒了,谢谢Jack。加载时间也好多了,我猜我加载了很多不需要的库?你知道有没有办法也读取“”空格字符(00100000)?
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"