Show context menu on Mac with keyboard using AppleScript and Automator

落花浮王杯 提交于 2019-12-08 07:54:20

问题


I am trying to find a way to bring up the context menu in Finder on a Mac with Yosemite without touching the mouse/touchpad.


A context menu.

After extensive research on this issue, the only possible route seems to be using AppleScript with Automator, and assign keyboard shortcut to it.

The AppleScript below was found on stackoverflow, if I run it inside the Automator, it would bring up the context menu on one of the files on the desktop (not the file currently selected.)

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 screenshot

But I am having trouble getting it to work with keyboard shortcut.
Also I will need to make sure that it brings the menu for the currently selected file.

Can someone provide some insight about how this can be done?


回答1:


You can read about the script below here: 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



回答2:


This will bring up the context menu of the currently selected file of the desktop:

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

*Tested on 10.8.5 in script editor



来源:https://stackoverflow.com/questions/29226133/show-context-menu-on-mac-with-keyboard-using-applescript-and-automator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!