keskiviikko 13. elokuuta 2014

About GUI scripting

GUI scripting refers to graphical user interface scripting, which basically means making your mouse and keyboard act automatically. This is very handy to know when you find yourself doing repetitive mouse movements, clicks and keystrokes again and again. Why be a machine in front of a machine?


But unfortunately it's not possible to do complete GUI scripting with only using applescript, since applescript's mouse behavior is restricted to only supported applications, like Safari. I consider my mouse not being interested in what it clicks and I want arbitrary control over it, so I found two solutions for that: cliclick and MouseTools.

cliclick is my main choice and here's how that works:

--you need to have the application cliclick downloaded and set to the desktop for this script to work.
set cliclick to POSIX path of ((path to desktop as text) & "cliclick")
leftClick(cliclick, 25, 10)
on leftClick(getCliclick, x, y)
do shell script getCliclick & space & "c:" & x & "," & y
end leftClick

Then I use MouseTools to move the mouse slowly, which is necessary in some cases. Here's how that works:

--you need to have the application MouseTools downloaded and set to the desktop for this script to work.
set mouseTools to POSIX path of ((path to desktop as text) & "MouseTools")
moveMouseWithMouseTools(mouseTools, 25, 10, 1000)
on moveMouseWithMouseTools(getMouseTools, x, y, z)
do shell script getMouseTools & space & "-x " & x & " -y " & y & " -mouseSteps " & z
end moveMouseWithMouseTools

One particularly cool feature of OSX: press shift+cmd+4. If your keyboard shortcut settings are set to default you should see coordinates in your mouse pointer.

To strike keystrokes or key codes you need to call an application called "System Events". I've written a tiny code to get a current date keystroked whenever I need it:

tell application "System Events" to keystroke ((do shell script "date '+%Y%m%d'") - 20000000 as string)

To keystroke a function key or special character you need to find out it's key code. You'll find those easily with this application: http://manytricks.com/keycodes/

Sometimes I use the clipboard for transferring data and there's basically two ways to use it. One is by using genuine applescript:

set the clipboard to "some text" --there's several options to replace "some text" part
set clipboardContent to get the clipboard
tell application "System Events" to keystroke clipboardContent

The other way is more GUI scripting approach:

--select something on your desktop
tell application "Finder" to activate
tell application "System Events"
keystroke "c" using command down
keystroke "v" using command down
end tell

Ei kommentteja:

Lähetä kommentti