使用AppleScript和Automator在键盘上显示上下文菜单

时间:2015-03-24 06:14:56

标签: macos applescript contextmenu keyboard-shortcuts

我正在尝试找到一种方法来在Mac上使用Yosemite 在没有触摸鼠标/触摸板的情况下调出Finder中的上下文菜单。

a context menu
上下文菜单。

经过对此问题的广泛研究后,唯一可能的路线似乎是将AppleScript与Automator一起使用,并为其指定键盘快捷键。

下面的AppleScript在stackoverflow上找到,如果我在Automator中运行它,它会调出桌面上某个文件的上下文菜单(而不是当前选择的文件。)

tell application "System Events"
    tell process "Finder"
        set target_index to 1
        set target to image target_index of group 1 of scroll area 1
        tell target to perform action "AXShowMenu"
    end tell
end tell


Automator截图

但是我无法使用键盘快捷方式 此外,我需要确保它带来当前所选文件的菜单。

有人可以提供一些有关如何做到这一点的见解吗?

2 个答案:

答案 0 :(得分:1)

这将打开当前所选桌面文件的上下文菜单:

tell application "Finder"
    set sel to get the selection

    if (sel is {}) then
        log "Nothing selected! Can't proceed"
        return
    end if

    set target_item_name to the name of (item 1 of sel) 
end tell

tell application "System Events"
    tell process "Finder"
        tell group 1 of scroll area 1
            set target to the first image whose value of attribute "AXFilename" is target_item_name
            tell target to perform action "AXShowMenu"
        end tell
    end tell
end tell

*在脚本编辑器中测试10.8.5

答案 1 :(得分:0)

您可以在此处阅读以下脚本:MacScripter / right click

 # Copyright © 2012 - 2015 McUsr
 run showRightClickMenu
 script showRightClickMenu
    on run
        set mouseLoc to (do shell script "~/opt/bin/MouseTools -location")
        set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
        tell mouseLoc to set {mouseX, mouseY} to {it's text item 1, it's text item 2}
        set {mouseX, mouseY} to {(mouseX as integer), 1200 - (mouseY as integer)}

        tell application id "sevs"
            set frontProcessName to name of every process whose frontmost is true
            --    tell a to set aa to (get its name)
            set wnCount to count of windows of process named frontProcessName
            if wnCount > 0 then
                tell window 1 of process named frontProcessName
                    set wnPos to its position
                    set wnsize to its size
                end tell
                set {wnX, wnY, wnWidth, wnHeight} to {item 1 of wnPos, item 2 of wnPos, item 1 of wnsize, item 2 of wnsize}

                set {leftBound, RightBound, upperBound, lowerBound} to {wnX + 1, (wnX + wnWidth - 21), wnY + 50, (wnY + wnHeight - 51)}
                if mouseX ≥ leftBound and mouseX ≤ RightBound then
                else if mouseX < leftBound then
                    set mouseX to leftBound
                else
                    set mouseX to RightBound
                end if

                if mouseY ≥ upperBound and mouseY ≤ lowerBound then
                else if mouseY < upperBound then
                    set mouseY to upperBound
                else
                    set mouseY to lowerBound
                end if

            end if
        end tell
        set mouseLoc to "c" & mouseX & " " & mouseY
        do shell script "~/opt/bin/cliclick " & mouseLoc
        set AppleScript's text item delimiters to astid
    end run
 end script