Friday, September 3, 2010

Some Applescripts to work with iRed Lite and the Apple Remote #1

Script to Open Movie Folder and Zoom In

The purpose of this script, is to open a folder of "Movies to be Watched" from an Apple Remote (or emulation thereof) to allow a movie to be played without using the full keyboard.  The runs as an "Action" from iRed Lite software, on the "Mouse Control" layer.  Double-clicking the right key on the remote triggers the script, as the per the screenshot below from iRed Lite.


This script uses the "Universal Access" Zoom feature to zoom in on a Finder window to makes it easier to read the file info from typical TV viewing distance (even with 40" screen).

The position of the zoomed-in portion of the screen is not predictable as it depends on the initial mouse cursor position.  Therefore, a major portion of this script is devoted to positioning the mouse cursor at the exact centre of the screen, prior to zooming in.  This is complicated by the lack native cursor movement commands in Applescript (although they are available with various add-on software).

Various sites/blogs suggest use of another Universal Access feature which allows cursor control via the keys on the numeric keypad.  Some people were not in favour of this approach, fearing they would lose use of the numeric keybad, although it is possible to turn the feature on and off with 5 presses of the Option key - which could also be done as part of a script, if required.  In our case, our Logitech keyboard doesn't have a numeric keypad so there was no penalty in permanently enabling the cursor control feature via system preferences.

The next issue was to determine the key codes for the keys in the numeric keypad, so they could be used for cursor control.  Although utilities are available to display the key code when you press a key, these would not work for us as there are no physical keys to press.  (We don't have a standard Mac keyboard either.)  Fortunately, I found a site which included a diagram of the keyboard with key codes on it.  An extract from this site is shown below.  The numeric keypad has an advantage in that it provides for diagonal cursor movement (via the 1, 3, 7, and 9 keys) in addition to up/down/left/right (via the 2, 4, 6 and 8 keys).

The strategy to position the cursor in the centre of the screen is to first move it to the top left corner, irrespective of its starting position, to move it to a known location.  Then, it is possible to precisely move the cursor to the centre of the screen.

By combining diagonal and left/right movement, as per the script below, it is possible to create a diagonal movement with the appropriate angle to suit a 16:9 screen ratio.  This is not essential, but it looks nice.

Once the cursor is positioned in the centre of the screen, it is then simple to zoom in and open the required Finder window, and set its bounds to fit nicely in the visible portion of the screen.

To use this script with iRed Lite, we have included it on the Mouse Control layer activated by double clicking the right key on the remote, in place of the original "Right 8' (move right 8 pixels) Action.  (The double click feature for letf/right cursor movement is largely superflous as the single-click left rights keys can be pressed multiple times or held down for larger cursor movements.  The double-click on the left key is also potentially available to activate some other script or action if required.)

In use, once the Finder window appears displaying the list of available movies, the cursor control and OK keys on the remote can be used to open the required movie with the default application (VLC in our case).  Once VLC launches, iRed Lite automatically switches its layer setting to the VLC layer and the remote button functions are remapped to new functions tailored to VLC.  Further mouse control is not possible unless the layer is changed using the Menu button on the remote (standard iRed Lite functionality).

In the Mouse Control layer of iRed Lite, we also changed the basic mouse movement keys from 4 pixel to 8 pixels movements.  The 8 pixel actions are already defined in iRed Lite.  The larger steps, we feel, provide better mouse movement on a large (Full HD) screen.

Here is the script associated with the "Open Movies" action in iRed Lite"

-- Close any existing Finder windows
tell application "Finder"
close every window
end tell
-- Zoom screen out to show full screen.
tell application "System Events"
keystroke "-" using {command down, option down}
end tell
-- Position cursor prior to zooming in screen.
-- This requires use of Numeric Keypad for mouse movement
-- in System Preferences - Universal Access
-- Logitech keyboard has no numeric keypad anyway.
tell application "System Events"
-- First, move cursor diagonally to top left corner.
-- Repeat enough times for any starting position.
-- Move diag. 9 times + left 7 times to match 16:9 screen
-- with minimum key presses.
repeat 120 times
repeat 9 times
key code 89  -- move cursor up & left
end repeat
repeat 7 times
key code 86 -- move cursor left
end repeat
end repeat
--Then, move diagonally to screen centre.
repeat 60 times
repeat 9 times
key code 85 -- move cursor down and right
end repeat
repeat 7 times
key code 88 -- move cursor right
end repeat
end repeat
end tell
-- Now it is time to zoom in towards centre of screen.
-- Zoom ratio set in Universal Access prefs: approx. 1.8
tell application "System Events"
keystroke "+" using {command down, option down}
end tell
-- Lastly, open the Movies folder
tell application "Finder"
open "Expansion Drive:Movies to be watched"
-- tell the front Finder window
-- set text size to 16
-- end tell
--Make finder window centred and half HD width & height
set the bounds of the front Finder window to {480, 270, 1440, 810}
end tell

No comments:

Post a Comment