IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    用AppleScript在Mac系统下实现按键精灵的功能以及在游戏中的运用

    Xcodev发表于 2016-03-11 02:33:57
    love 0

    什么是AppleScript

    AppleScript是Mac系统提供的一个脚本语言,这个脚本语言简单易读,稍微懂一点脚本语言就可以快速学会了。AppleScript可以让我们的日常工作流程化,简化繁琐的固定输入,自动化完成一般的日常工作。
    AppleScript提供了很多很多强大的功能,本文就只简单的介绍一下如何用它来实现类似Windows下按键精灵的功能。

    前提工作

    如果想要让AppleScript自动帮我们按键,点击鼠标。首先还是要在设置里“启用辅助设备的控制”,否则AppleScript就没有权限控制我们的键盘、鼠标这些辅助设备。
    打开“系统偏好设置”->“辅助功能”页面,然后勾选“启用辅助设备的控制”。

    image

    如何按键

    在Mac系统中有个叫“System Events”的App,AppleScript可以让它做按键或者点击鼠标的动作。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    tell application "TextEdit"
    activate
    end tell
    tell application "System Events"
    tell process "TextEdit"
    set frontmost to true
    end tell
    key down shift
    keystroke "e"
    delay 1
    key up shift
    keystroke "e"
    keystroke return
    end tell

    1-3行脚本用来运行系统自带App“文本编辑器”,第6-8行是将文本编辑器设置为前台App,这样我们的键盘鼠标消息才可以发到“文本编辑器”的进程上去。key down是按下某个按键不放,key up是松开某个按键,keystroke是点按一下某个按键。第10-13行相当与按shift+e,这样在文本编辑器中就输入的大写的“E”,然后14行就是又按了一下“e”,就紧接着输入了一个小写的“e”。15行按了一下回车,那么“文本编辑器”会换行。

    如何点击鼠标

    点击鼠标脚本和按键脚本类似,不同的是用了click at命令。如:

    1
    2
    3
    4
    5
    6
    tell application "System Events"
    tell process "xxx"
    set frontmost to true
    end tell
    click at {100,200}
    end tell

    这个脚本运行后就是在屏幕坐标点为{100,200}的地方点击一下鼠标左健。

    游戏《魔兽世界》中的运用

    做为一个爱玩游戏的技术宅,这些功能非常适合运用到游戏当中。所以,呵呵。一个运用就是防止暂离,《魔兽世界》战场中是不容许暂离的,一旦暂离就会被清除战场,如果你没节操挂机的话,就要每隔一段时间手动操作一下人物,比如按一下空格跳跃一下。使用以下这个脚本就可以在挂机的时候防止暂离了。

    AntiAFK.scptd

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    tell application "System Events"
    repeat while 1 is 1
    if UI elements enabled then
    tell process "World of Warcraft-64"
    set frontmost to true
    end tell
    end if
    keystroke " "
    delay (random number from 50 to 75)
    end repeat
    end tell

    这个脚本会每隔50-75秒,按一下空格跳跃一次。这样就不会有暂离状态了。之所以用随机50-75秒,是为了防止固定操作容易被反外挂程序检测到。

    另外我还写一个刷地精声望的脚本

    地精声望.scptd

    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
    tell application "System Events"
    repeat while 1 is 1
    if UI elements enabled then
    tell process "World of Warcraft-64"
    set frontmost to true
    end tell
    end if
    keystroke "z"
    delay 2
    if UI elements enabled then
    tell process "World of Warcraft-64"
    set frontmost to true
    end tell
    end if
    keystroke "c"
    delay 2
    if UI elements enabled then
    tell process "World of Warcraft-64"
    set frontmost to true
    end tell
    end if
    keystroke "x"
    delay 2
    set r to (random number from 1 to 10)
    delay r
    end repeat
    end tell

    这个脚本是不停的按Z、X、C三个按键,可以根据情况绑定相应的技能。贫瘠之地有个勇士岛,岛上的一间屋子里有个叫“嘉维伊船长”的人,杀了他可以得到不少地精声望,而且他刷新很快,1-2分钟刷一次。这就让这个脚本来自动刷声望成为可能。
    这个Z按键我绑定选目标攻击的WOW宏,WOW宏内容如下:

    1
    2
    /tar 嘉维伊船长
    /startattack [exists,nodead]

    X按键我绑定了公会战旗,这样就可以多得到点声望了。而且可以全程有战旗覆盖。
    C按键我绑定了群体攻击大技能,这样就可以在群体技能冷却后杀死额外的小怪收获额外的声望。
    然后,你要走到“嘉维伊船长”的刷新点面对他后运行脚本,这样就可以睡觉刷声望了。虽然效率不是很高,但缺很省心,毫不费劲,最重要的是不占用我们的时间。毕竟“时间就是金钱,我的朋友。”



沪ICP备19023445号-2号
友情链接