Skip to main content
areohbee
Legend
September 7, 2010
Question

Mac Question: Anybody know how to get services or keyboard shortcuts to work in a plugin?

  • September 7, 2010
  • 1 reply
  • 1351 views

I have a plugin that really benefits from being able to press a single key to issue a multi-key sequence.

I'm doing this on WIndows using AutoHotKey, and it works great on Mac using QuickKeys, but I'd like to find a way to do it without requiring the user to purchase QuickKeys. I've been able to use Automator to define a script that issues the desired keystrokes, and tie it to the keyboard as a shortcut initiated service, and this works in Lightroom proper, but is disabled when using my plugin.

Any ideas?

Thanks,

Rob

This topic has been closed for replies.

1 reply

areohbee
areohbeeAuthor
Legend
November 18, 2010

So now I have:

on the fly applescript (osascript -e  ... -e ... -e ...) to send various keystrokes to Lightroom on Mac -  that's working great although not fully validated.

November 18, 2010

Do you have to enable "Universal Access" in System Preferences or can you circumvent this?

How does a real call look like?

areohbee
areohbeeAuthor
Legend
November 18, 2010

Sorry I may have confused.

Original post was about getting keyboard shortcuts set up for the user, and the follow-up/reply post was about issuing keystrokes programmatically.

Regarding the former, maybe univesal access is required - dont really know.

Regarding the latter, the following code is part of the framework available at https://www.assembla.com/spaces/lrdevplugin/

It allows a plugin to send keystrokes like "p" to pick a photo, or "Cmd-S" to save metadata. There is a windows counterpart as well...

--[[
        Send key string verbatim to Lightroom.
       
        Uses applescript string passed to osascript.
--]]
function Mac:sendUnmodifiedKeys( keyStr, keyDowns, keyUps )
    local scriptTbl = {}

    scriptTbl[#scriptTbl + 1] = "-e 'tell application \"Lightroom\" to activate'"
    scriptTbl[#scriptTbl + 1] = "-e 'tell application \"System Events\"'"

     if keyDowns then
        tab:appendArray( scriptTbl, keyDowns )
    end

    scriptTbl[#scriptTbl + 1] = "-e 'keystroke \"" .. keyStr .. "\"'"

    if keyUps then
        tab:appendArray( scriptTbl, keyUps )
    end

    scriptTbl[#scriptTbl + 1] = "-e 'end tell'"

    local scriptStr = table.concat( scriptTbl, ' ' )
    local command = 'osascript'
    local params = scriptStr

    return self:executeCommand( command, params ) -- no targets, no output.
end

--[[
        Send mac-modified keystroke sequence to mac os / lightroom.
       
        Format examples:
       
            Ctrl-S
            Cmd-FS
            ShiftCtrl-S

        i.e. mash the modifiers together (in any order), follow with a dash, then mash the keystrokes together (order matters).
--]]
function Mac:sendModifiedKeys( modKeys )
    local k1, k2 = modKeys:find( '-' )
    local keyMods
    local keyStr
    if k1 then
        keyStr = modKeys:sub( k2 + 1 )
        keyMods = modKeys:sub( 1, k1 - 1 )
    else
        error( "No keystroke" )
    end
    local keyDownTbl = {}
    local keyUpTbl = {}
    if keyMods:find( 'Shift' ) then
        keyDownTbl[#keyDownTbl + 1] = "-e 'key down shift'"
        keyUpTbl[#keyUpTbl + 1] = "-e 'key up shift'"
    end
    if keyMods:find( 'Option' ) then
        keyDownTbl[#keyDownTbl + 1] = "-e 'key down option'"
        keyUpTbl[#keyUpTbl + 1] = "-e 'key up option'"
    end
    if keyMods:find( 'Cmd' ) then
        keyDownTbl[#keyDownTbl + 1] = "-e 'key down command'"
        keyUpTbl[#keyUpTbl + 1] = "-e 'key up command'"
    end
    if keyMods:find( 'Ctrl' ) then
        keyDownTbl[#keyDownTbl + 1] = "-e 'key down control'"
        keyUpTbl[#keyUpTbl + 1] = "-e 'key up control'"
    end
    return self:sendUnmodifiedKeys( keyStr, keyDownTbl, keyUpTbl )
end