Win10 & Android Auto
Plan to turn smartphones into workers.
Automate Windows with AHK
send email using AHK
API (App password) Set up ref: {Link}
Script Ref: {Forum Link}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#Requires AutoHotkey v1.1.37.02 sendEmail(from, to, cc, bcc, subj, body, server, port, user, pwd) { pmsg := ComObjCreate("CDO.Message") pmsg.From := from, pmsg.To := to, ; pmsg.CC := cc, pmsg.BCC := bcc, pmsg.Subject := subj, pmsg.TextBody := body fields := Object(), fields.smtpserver := server, fields.smtpserverport := port, fields.smtpusessl := True fields.sendusing := 2 ; cdoSendUsingPort fields.smtpauthenticate := 1 ; cdoBasic fields.sendusername := user, fields.sendpassword := pwd, fields.smtpconnectiontimeout := 60 schema := "http://schemas.microsoft.com/cdo/configuration/", pfld := pmsg.Configuration.Fields For field, value in fields pfld.Item(schema field) := value pfld.Update() pmsg.Send() } sendEmail("YOUgmail.com", "OTHER@gmail.com", "hi", "", "test email", "test body 240317-2105","smtp.gmail.com", 465, "YOU@gmail.com", "APPPWD"); |
auto hot key
Advantages: AHK use way less computation resources than python scripts.
I mainly use it to replace repetitive works like data inputing or skipping error dialog. The challenge is most reference online now are for AHK 1.0, while 2.0 has more rigorous syntax (rather than a command lines in AHK 1.0). I may share my code as an example here later. Comment below to let me know if you d like to see.
Here are some quick demos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Requires AutoHotkey v2.0-a #SingleInstance Force #SuspendExempt ^Esc:: { MsgBox "Pause & Suspend" Suspend Pause } ^+!R:: { MsgBox "Script Reloaded" Reload } #SuspendExempt False |
1 |
F7:: testCode()<br><br>getClickCoord(prompt:=""){<br>CoordMode "Mouse", "Screen"<br>Loop {<br>MouseGetPos &XPos, &YPos<br>ToolTip "Right Clip to Select " prompt " X:" XPos "Y" YPos , XPos, YPos<br>if GetKeyState("RButton"){<br>ToolTip<br>break<br>}<br>}<br>return [XPos, YPos]<br>}<br><br> |
1 |
testCode()<br>{<br>; drop down list for functions<br><br>MyGui := GUI()<br>DDL := MyGui.Add("ComboBox", "vFuncToRun", ["iterSeederNSetter", "exportWhenFinish", "inputV", "matchElevation", "testGetMouse", "matchProp", "setPointSeeder", "ignoreBatchError", "test"])<br>OkBtn := MyGui.Add("Button", "Default w80", "OK")<br>OkBtn.OnEvent("Click", funcSwitch)<br>funcSwitch(*){<br>MyGui.Hide()<br>str:=DDL.Text<br>;~ MsgBox str<br>switch<br>{<br>case str="iterSeederNSetter": iterSeederNSetter()<br>case str="exportWhenFinish": exportWhenFinish()<br>case str="inputV": inputV()<br>case str="matchElevation": matchElevation()<br>case str="testGetMouse": getClickCoord("for test")<br>case str="matchProp": matchProp()<br>case str="setPointSeeder": setPointSeeder()<br>case str="ignoreBatchError": ignoreBatchError()<br>case str="test": MsgBOx "Test done"<br>}<br>MyGui.Destroy()<br>}<br>MyGui.show()<br><br>} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
ignoreBatchError(){ Loop { if WinExist("Error"){ ; note down current window unique ID ActWin:= WinActive("A") WinActivate("Error") ; Test code ; MouseMove 346, 119 Send "{Enter}" ; switch back to previous window WinActivate("ahk_id" ActWin) } ; each path takes 10s so minimum check frequency is 20s ; to save the system usage sleep 2111 } } |
AHK relies on GUI, which necessitate an active graphic session when remoting a windows server. A round about to keep GUI rendering is below. Save it to a .cmd
file and run as admin. Replace %%
with %
if running in command line.
1 |
for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (tscon.exe %%s /dest:console) |
Automatic win10
Imitate user input
Python imitate users\’ operations on Win10: CSDN
Realized by PyUserInput package.
Using Pycharm to develop a simple simulator of Mouse and Keyboard Input.
And wrap it up to window executable file: .exe!!! (Believe me. This is much harder than coding itself)
Keyboard & Mouse Simulation
Package: pynput
Chinese Introduction: https://www.jb51.net/article/155520.htm
Official doc: https://pypi.org/project/pynput/
The basic idea of using pynput is
listen to the event of a real user with pynput.mouse.Listener or pynput.keyboard.Listener
Then do the step automatically by
pynput.mouse.Controler and pynput.keyboard.Controler
Notice!!!
If you are a windows user
Make sure you install pynput 1.6.8 version. Or you will not be able to wrap it to a exe file.
The code is shown below. (Type it in the cmd)
1 |
pip3 install pynput==1.6.8 |
You can facilitate the wrapping by putting the external package imported in the .py scripts into the same directory.
In my case, I put ‘pynput’ into the same directory.
Another hint for wrapping is about import.
Import pacakge as ‘from … import …‘ will import the method or class independently, which can reduce your .exe file size in most cases.
E.g.
1 2 3 4 5 6 7 |
from time import sleep from pynput.mouse import Listener # will take less space than import time import pynput.mouse.Listener as Listener |
.py to .exe
The popular version
Package: pyinstaller
If you use any third-party package, such as NumPy, pandas, etc., you should copy their package files to the same directory as the .py file going to be packed. 【Pic_Ref】
Locate CMD to the directory ready to be packed with all files and packages inside, and type in the command as the example shown below.
1 |
pyinstaller test.py -F -console -i img.ico |
Frequently used commands are listed in the table below.
Function | Short Code | Long code |
wrap in one file (Suitable for simple scripts) | -F | –onefile |
wrap in one directory (Easy to maintain) | -D | –onedir |
run with a console (Good for debugging) | -c | –console |
run without a console | -w | –windowed |
attach icon (.ico file) | -i ./img.ico | –icon=./img.ico |
A beginer-friendly GUI version
Package: auto-py-to-exe
Video tutorial from PythonSimplified: https://www.youtube.com/watch?v=Y0HN9tdLuJo
Pycharm GUI interactive design (Not referred this time)
Package: QtGUI, etc.
Link: https://www.cnblogs.com/lightbc/p/14650292.html
Source Code of this trial
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 |
# a simple mimic scripts by Arnold ARC from time import sleep from pynput.mouse import Listener from pynput.mouse import Controller from pynput.mouse import Button from pynput.keyboard import Key ms = Controller() from pynput.keyboard import Controller bt = Button kb = Controller() key = Key def on_click(x, y, button, pressed): print('{0} {1} at {2}'.format('Pressed' if pressed else 'Released', button, (x, y))) if not pressed: return False def get_loc(): with Listener(on_click=on_click) as listener: listener.join() def inquire(): times = int(input('how many times to do?\n')) x = int(input('what is the x coordinate of the click?\n')) y = int(input('what is the y coordinate of the click?\n')) my_key = key.f5 interval = int(input('how many seconds are there between each two operations?\n')) return times, x, y, my_key, interval def run(times, x, y, my_key, interval): for i in range(times): ms.position = (x, y) ms.click(bt.left, 1) kb.press(my_key) kb.release(my_key) sleep(interval) print('The scripts has run for {0} times.\n'.format(i + 1)) if __name__ == '__main__': start = True while start: go = True while go: get_loc() holder = str(input("Press 'Enter' to probe another location.\n" "Enter 'stop' and press 'Enter' to stop picking coordinates.\n")) go = (False if holder == 'stop' else True) Times, X, Y, My_key, Interval = inquire() run(Times, X, Y, My_key, Interval) start = input("Enter 'exit' to quit\n""Press 'Enter' to run again\n") start = (False if start == 'exit' else True) |
Miscellaneous
This is my first project done on PyCharm.
It will create a new virtual environment each time, which will keep the base env and package tidy.
What impressed me most is the auto-reformation. (Alt+Enter+F12)
The window layout with console, terminal, etc, is born for large project development like web or software.
I really like it as a development tool.
But I also realize I don’t need it for most works I need to do at the current stage. And it is hungry for my computational resources.
So I guess the best way is to learn from the format of coding and windows layout combination.
Schedule run
Set schedule program in win10: 简书
With GUI !
## Automatic Android
Android Auto
scrcpy电脑控制手机
[项目源-github]
【快速入门-知乎】
下方自带的readme就已经足够详细了
手机端先开启所有USB调试相关选项
Release中下载最新版后,此处以win10为例
常用参考
js常用运算规则:
https://www.cnblogs.com/hujinzhong/p/11549615.html
判断变量类型
type of [Var]
入门与部署:
该链接中的apk下载已经失效【AutoJS部署入门-CSDN】
该文包含AutoJS免费版下载链接【AutoJS实现淘宝自动看广告收喵币-知乎】
运行
运行时可以加入环境变量或是每次都需要先进入当前所在路径打开cmd或powershell
按照对应连接方式,输入命令,并即时在手机上通过验证
1 2 3 4 5 6 7 8 |
# USB连接调试 .\adb.exe usb # 同一局域网下,开放端口 .\adb.exe tcpip 5555 # 替换DEVICE_IP为手机的ip地址 adb connect DEVICE_IP:5555 |
也可以将对应路径增加到系统环境变量下
添加至环境变量(可选)
图形界面(user-friendly)
【图形界面设置环境变量-百度百科】
运行输入
,添加adb和scrcpy所在的根路径即可sysdm.cpl
<code>进入</code>
高级设置(Advanced)
<code>修改</code>
环境变量(Environment Variables)
<code>,点击</code>
Path
命令行(Geek最爱)
1 2 3 4 5 6 7 8 9 10 11 12 |
# 查看某环境变量下路径,如Path set Path # 追加路径,如PATH下追加 “d:\xxx” set PATH=%PATH%;d:\xxx # 添加环境变量,如xxx=aa set xxx=aa # 将环境变量(如xxx)的值置为空 set xxx = |
追加完切记重启cmd后才能生效。
其他参考内容:
【ABD Installer-脚本之家】
【win10配置JAVA环境-CSDN】
【scrcpy使用教程简介-CSDN】
常用快捷键
MOD默认代表左Alt,可自行修改
常用如下
1 2 3 4 5 |
MOD+P 唤醒键 或 熄屏时单机鼠标右键 MOD+O 关闭手机屏幕但继续镜像 MOD+Shifth+O 打开手机屏幕 MOD+i 测量帧率 MOD+x/v/c 剪切复制粘贴,和电脑剪贴板完全互通 |
完整版输入
命令查看.\scrcpy.exe --help
常用scrcpy consoler命令
完整版输入命令同样见–help指令结果
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 |
--always-on-top Make scrcpy window always on top (above other windows). --max-fps value -p, --port port[:port] Set the TCP port (range) used by the client to listen. Default is 27183:27199. -r, --record file.mp4 Record screen to file. The format is determined by the --record-format option if set, or by the file extension (.mp4 or .mkv). --record-format format Force recording format (either mp4 or mkv). --shortcut-mod key[+...]][,...] Specify the modifiers to use for scrcpy shortcuts. Possible keys are "lctrl", "rctrl", "lalt", "ralt", "lsuper" and "rsuper". A shortcut can consist in several keys, separated by '+'. Several shortcuts can be specified, separated by ','. For example, to use either LCtrl+LAlt or LSuper for scrcpy shortcuts, pass "lctrl+lalt,lsuper". Default is "lalt,lsuper" (left-Alt or left-Super). -S, --turn-screen-off Turn the device screen off immediately. -t, --show-touches Enable "show touches" on start, restore the initial value on exit. It only shows physical touches (not clicks from scrcpy). -w, --stay-awake Keep the device on while scrcpy is running, when the device is plugged in. |
实战:抖音自动脚本开发
点击Auto.js悬浮窗后选择出现的蓝色按钮,点击布局范围分析后选择领喵币按钮查看控件信息
核心是实现文字识别和导出,下列为参考:
- 【AutoJS识别微信聊天记录】
- 【淘宝喵币&蚂蚁森林】
- 【文件操作】
- 【AutoJS官方文档】便于查找对应函数的操作
- 【javascripts的主要数据类型与运用】 包含较多适合我这样的初学者的讲述
分析他人代码时,用Notepad++(结合Astyle实现自动排版)。
【Astyle使用-CSDN】
最佳的编写和调试方式:局域网下的VSCode电脑编写,远程推送手机运行
【VSCode的配置】
【VSCode入门教程-CSDN】
包含安装和git版本控制。
【VSCode官方下载地址与文档】
个人感想
代码的学习方式,就是通过案例了解大致功能,官方文档查询具体的实现方法,最后通过编程验证。
With AndroidViewClient & Culebra
Analogous to Marco in MS-Office.
Intuitive Graphic interface & user operation recording.
鲲鹏数据抓取
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 |
<pre class="wp-block-code">``` # coding: utf-8 # 点击屏幕微信图标启动微信,点击第一个联系人/群,发送一个报时消息 import sys import os import re import time from com.dtmilano.android.viewclient import ViewClient def test(): # 连接手机 device, serialno = ViewClient.connectToDeviceOrExit() vc = ViewClient(device, serialno) # 按HOME键 device.press('KEYCODE_HOME') time.sleep(3) # 找到微信图标 vc.dump() weixin_button = vc.findViewWithTextOrRaise(u'微信') # 点击微信图标 weixin_button.touch() time.sleep(10) # 找到第一个联系人/群 # 可以使用UI Automator Viewer查看到对应第一个联系人/群的resource-id为com.tencent.mm:id/auj vc.dump() group_button = vc.findViewByIdOrRaise(com.tencent.mm:id/auj) # 点击进群 group_button.touch() time.sleep(5) # 找到输入框并输入当前时间 vc.dump() vc.findViewByIdOrRaise(com.tencent.mm:id/aep).setText('Now:{}'.format(time.strftime('%Y-%m-%d %H:%M:%S'))) time.sleep(3) # 点击发送按钮 vc.dump() vc.findViewWithTextOrRaise(u'发送').touch() if __name__ == '__main__': test() |
Android Debug Bridge (ADB)
ADB link to Android, control with Python: CSDN
Battery & Charging
Battery has the best life span using between 50% to 80%.
ASUS battery health patch. I am using UX370UAF, Flip. It should support Battery Health app, but i cannot find the patch in MS store. Thanks to this {Zhihu post} which shares the patch from official customer support {download link}
Amazfit band
You may find many post about myamazfit.ru, the website is closed {see this reddit post}. However, a more convenient website shows up allow online compilation of ROM {https://amazfitbip.ngxson.com/}. Thanks to MNVolkov {Github page}
To write the ROM, following apps are needed:
Paid:
Tools&Amazfit
Notify for Amazfit (tested can connect and claim can get Auth Key but haven’t got it)
Free:
GadgetBridge (need Auth Key)
A general hint is do this with a backup device as these app requires so many permissions that may comprise your data privacy.