How to automate right clicking Finder's dock icon and choosing action? I'd want to set a global shortcut key for the "Go to Folder..." action available from Finder's dock icon.

Right now I figured out how to trigger the same action from Finder's menu bar via AppleScript:

tell application "Finder" to activate

tell application "System Events"
    keystroke "g" using {shift down, command down}
end tell


however I don't like this solution because if there's a space with Finder windows open, and I'm in another space without Finder windows open, that causes to switch spaces.

Instead, right clicking Finder's dock icon and selecting "Go to Folder..." keeps me in the space I'm in.

How to automate this action and set it as a global keyboard shortcut?
First a word about global keyboard shortcuts... It should stand to reason that any user assigned global keyboard shortcut has to be unique to both the OS and any application that has focus when the keys are pressed, otherwise it will not function as desired. While one would like to have to press as few keys as possible, nonetheless using a four key sequence is probably going to be the best bet to avoid stepping on a default keyboard shortcut that is already assigned to the OS and or any app that has focus when the keys are pressed. Also making the fourth key something other then the typical alpha keys may be more beneficial in avoiding something already assigned.

Keep in mind though that the default, developer assigned, keyboard shortcuts of most any given application can be modified in order to free up a particular combination that one may want to use. Personally I try to avoid doing this, however to make changes, the target menu command can be added to: System Preferences > Keyboard > Shortcuts > App Shortcuts

As an example, Go to Folder…  of the Go menu in Finder can be added and its default ⇧⌘G can be modified to e.g. ⌃⇧⌘G so the default keyboard shortcut can be used in this case to trigger the example AppleScript code used in this answer with e.g. FastScripts.

Apple has made it increasingly more difficult to use an Automator Service in particular cases and as such, this particular use case, I personally use and recommend FastScripts. Note that I have no affiliation with the developer of FastScripts and am just a satisfied user of the product.

The following example AppleScript code used in conjunction with FastScripts will do as you asked under the expressed conditions in your OP and you will only need to add FastScripts to System Preferences > Security &amp; Privacy > Privacy > Accessibility in order to have this script function. In other words, if FastScripts is the only app in Accessibility for Security &amp; Privacy this script will function as intended.

That said, if you want to test the example AppleScript code before saving it, you do need to add Script Editor, but it's not required for the actual functioning under FastScripts. I mention this because when going the Automator Service route it can require adding two or more apps to get this same script just to run, however it will still not function as desired as an Automator Service and why FastScripts is being recommended.

Example AppleScript code:

tell application "System Events"
    tell UI element "Finder" of list 1 of application process "Dock"
        perform action "AXShowMenu" 
        click menu item "Go to Folder…" of menu 1
    end
end tell


Using Script Editor, I saved the code above as Go to Folder.scpt in ~/Library/Scripts and in FastScripts assigned it the keyboard shortcut ⌃⇧⌘G until such time I find it conflicts with any other app I use.

Noted that this was tested and works as stated on my system running macOS High Sierra 10.13.4 regardless of what app had focus when the keys were pressed.

I also did not need to use a delay command between perform action ... and click menu item ... however note that the value of any delay command will need to be adjusted for your system, and or additional delay commands may or may not be needed. Adjust the value(s) of and or add/remove the delay command(s) as appropriate.



Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.