CodeWalrus

Development => PC, Mac & Vintage Computers => Topic started by: Yuki on October 30, 2016, 03:37:08 AM

Title: Tool-Assisted Speedrunning Mac Applications
Post by: Yuki on October 30, 2016, 03:37:08 AM
Eh, I'm not sure anyone here have Macs but guess it'd be useful to put this here.

I've recently started playing with AppleScript, which is a pretty powerful thing that lets you script pretty much anything on your system, akin to bash shell scripts, except you can also script supported (and unsupported?) GUI applications to do things like clicking on a particular button or opening a particular menu. So of course, you can use it to TAS the whole system, why not?

Here's one script that I wrote for Undertale, there's a basis to TAS games, right now it pretty much starts the game and names the player as there's no real "key up/down" feature, or at least, it's broken... But at least it starts as soon as it's launched. To run this script you must have bought Undertale on Steam and have not saved yet.

Maybe it can be used to TAS calc games, that would be fun.

Code (AppleScript) Select

-- Keypress function, gets keycode, delay time (in 15ths of a second) and repeats in arguments
on kp(keycode, dtime, x)
tell application "System Events"
repeat x times
key code keycode
delay dtime / 15
end repeat
end tell
return
end kp

-- Useful key codes for Undertale
set z to 6
set x to 7
set c to 8
set kleft to 123
set kright to 124
set kdown to 125
set kup to 126

tell application "System Events"
-- Detects whether UI elements are enabled, if not we prompt the user to enable it in System Preferences then quit
set isEnabled to UI elements enabled
if isEnabled is false then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.security"
display dialog "This script uses macOS' GUI Scripting feature, which is currently disabled. Please enable Script Editor to control your computer in the security pane." with icon 1 buttons {"Cancel"} default button 1
return
end tell
end if
-- Opens Undertale from Steam (close Undertale first for optimum sync)
do shell script "open steam://run/391540"
-- Now we wait until the window is shown
repeat until window "UNDERTALE" of process "UNDERTALE" exists
end repeat
-- Make sure it's on top (kinda useless but we never know)
set frontmost of process "UNDERTALE" to true
end tell
-- Now press those keys super fast
kp(z, 45, 1)
kp(z, 1, 2)
kp(kright, 1, 2)
kp(kdown, 1, 1)
kp(z, 1, 1)
kp(kleft, 1, 3)
kp(kdown, 1, 5)
kp(z, 1, 1)
kp(kright, 1, 3)
kp(kup, 1, 2)
kp(z, 1, 1)
kp(kleft, 1, 3)
kp(kdown, 1, 2)
kp(z, 1, 1)
kp(kdown, 1, 1)
kp(kright, 1, 1)
kp(z, 1, 1)
kp(kright, 1, 1)
kp(z, 1, 1)


I do believe you can do that on other operating systems, Xorg in particular have a tool for simulating keypresses and mouse clicks, Windows probably have a program for that as well.
Title: Re: Tool-Assisted Speedrunning Mac Applications
Post by: Dream of Omnimaga on October 30, 2016, 04:34:03 PM
It amazes me that of all things, they left out two of the most important keys >.<

Interesting, though. I didn't know AppleScript could do that
Title: Re: Tool-Assisted Speedrunning Mac Applications
Post by: Yuki on October 30, 2016, 04:49:00 PM
As I tested, there is a key down/up function, but it only works with modifier keys. And the letter A, for some reason. Same for some command-line app I found. But yeah, otherwise, that's interesting.

I also tried it with a version of TilEm2 I compiled, but unfortunately, it doesn't even seem to detect the window.
Title: Re: Tool-Assisted Speedrunning Mac Applications
Post by: Yuki on November 13, 2016, 11:20:27 PM
I ported the whole thing in Python, it works quite nicely now. Also added a WIP of Undertale in there. It's not extremely stable, but it should work.

https://github.com/juju2143/tas