Haskell bindings for PyAutoGUI
These are straightforward Haskell bindings for PyAutoGUI, a library for automating user interaction tasks, using haskell-cpython.
This is just about the simplest possible example:
import AutoGUI
main = do
initialize
write "Hello, world!"
This doesn't just print Hello, world!
to the screen, but instead simulates a user typing it in.
The following Haskell and Python examples do the same thing:
import AutoGUI
import Control.Monad (forM_)
main :: IO ()
main = do
initialize
putStrLn "3 seconds until beeping begins"
sleep 3
forM_ [1..100] $ \i -> do
write "beep"
press [key|enter|]
sleep 0.5
import pyautogui
import time
print('3 seconds until beeping begins')
time.sleep(3)
for i in range(1, 101):
pyautogui.write('beep')
pyautogui.press('enter')
time.sleep(0.5)
Because not all valid Text
s are valid Key
s, we need a way to check that Key
s are valid when creating them. This leads to mkKey :: Text -> Maybe Key
. However, using the key
quasiquoter, we can sidestep having to use Maybe
by catching invalid keys at compile time. For example, [key|backspace|]
is a valid Key
which we can construct and check at compile time.
This is especially useful for some data that looks like this, where there are way too many values (and values with strange characters) for a sum type to be especially handy, but we want to check validity in some way. We generally know which keys we want to use at compile time.
pause :: Double -> IO ()
- Set a number of seconds to wait in between autogui actionsfailsafe :: Bool -> IO ()
- When set to true, move the mouse to the upper-left corner of the screen to throw a Python exception, and quit the programsleep :: Double -> IO ()
- Sleep for a given fractional number of seconds
size :: IO (Integer, Integer)
- (screenWidth, screenHeight) of the primary monitor in pixelsposition :: IO (Integer, Integer)
- (x, y) position of the mouseonScreen :: Integer -> Integer -> IO Bool
- Test whether (x, y) is within the screen size
write :: Text -> IO ()
- Write out some Text as though it were entered with the keyboardtypewrite :: Text -> IO ()
- Write out some Text as though it were entered with the keyboard, newline is entertypewriteKeys :: [Key] -> IO ()
- Write out some Text as though it were entered with the keyboard, newline is enterwriteWithInterval :: Text -> Double -> IO ()
- Write out some Text as though it were entered with the keyboard, with a specified number of seconds between keypressespress :: Key -> IO ()
- Simulate a keypresskeyDown :: Key -> IO ()
- Simulate holding a key downkeyUp :: Key -> IO ()
- Simulate releasing a keyhotkey :: [Key] -> IO ()
- Press a key combination
key :: QuasiQuoter
- This quasiquoter lets you use [key|enter|] at compile time, so you don't get a Maybe as you would from mkKeymkKey :: Text -> Maybe Key
keyToText :: Key -> Text
isValidKey :: Text -> Bool
keys :: Set Key
alert :: Text -> IO ()
- Show a box onscreen until dismissedconfirm :: Text -> IO Bool
- Show a box onscreen until a user hits OK or Cancel. Return True on OK, False on Cancel, and False if user closes the boxpassword :: Text -> IO Text
- Show a box onscreen, allowing user to enter some screened text. Return empty string if user closes the boxprompt :: Text -> IO Text
- Show a box onscreen, allowing user to enter some text. Return empty string if user closes the box
moveTo :: Integer -> Integer -> IO ()
- Move the mouse to an (x, y) positionmoveToDuration :: Integer -> Integer -> Double -> IO ()
- Move the mouse to an (x, y) position, over a number of secondsmoveRel :: Integer -> Integer -> IO ()
- Move the mouse relative to where it is nowmoveRelDuration :: Integer -> Integer -> Double -> IO ()
- Move the mouse relative to where it is now, over a number of secondsclick :: MouseButton -> IO ()
- Click the specified mouse buttonleftClick :: IO ()
- Left click the mousedoubleClick :: IO ()
- Double click the mousetripleClick :: IO ()
- Triple click the mouserightClick :: IO ()
- Right click the mousemiddleClick :: IO ()
- Middle click the mousemoveAndClick :: Integer -> Integer -> IO ()
- Move the mouse to some (x, y) position and click theredrag :: Integer -> Integer -> IO ()
- Clicks and drags the mouse through a motion of (x, y)dragDuration :: Integer -> Integer -> Double -> IO ()
- Clicks and drags the mouse through a motion of (x, y), over a number of secondsdragTo :: Integer -> Integer -> IO ()
- Clicks and drags the mouse to the position (x, y)dragToDuration :: Integer -> Integer -> Double -> IO ()
- Clicks and drags the mouse to the position (x, y), over a number of secondsdragRel :: Integer -> Integer -> IO ()
- Clicks and drags the mouse through a motion of (x, y)dragRelDuration :: Integer -> Integer -> Double -> IO ()
- Clicks and drags the mouse through a motion of (x, y)scroll :: Integer -> IO ()
- Scroll up (positive) or down (negative)mouseDown :: IO ()
- Press the mouse button downmouseUp :: IO ()
- Release the mouse button
locateOnScreen :: FilePath -> IO (Maybe (Integer, Integer, Integer, Integer))
- Return (left, top, width, height) of first place the image is foundlocateCenterOnScreen :: FilePath -> IO (Maybe (Integer, Integer))
- Return (x, y) of center of an image, if the image is found