Question

On My iMac I usually have several desktops that I use for separate things.

While I prefer to have the Dock on my main space to access apps easier, I much prefer to have the Dock hide on all other spaces, except for the main one.

I know, that I can just do Option-Command -D to hide/unhide the dock whenever, but is there a way, to have different Dock settings in different spaces?

Was it helpful?

Solution

I know, that I can just do Option-Command -D to hide/unhide the dock whenever, but is there a way, to have different Dock settings in different spaces?

No, the preferences of the Dock are global between Desktops/Spaces, however, there is a programatic way to automate the toggling of the autohide preference of the Dock as you switch between Desktops/Spaces.

This requires an application to be memory resident and able to respond to the target event.

As an example, you could roll your own in Xcode, or use any third-party application capable of responding to a Desktop/Space changed event, determine which Desktop/Space you are on, and then toggle the autohide preference of the Dock to hide/show it based on the conditions, effectively pressing the ⌥⌘D keyboard shortcut programmatically to avoid doing it manually.


With that said, I'll offer you a way that works for me and utilizes a free third-party application named Hammerspoon, along with using the free third-party Hammerspoon module hs._asm.undocumented.spaces to have access to the undocumented Spaces API.

Some Lua code, which is what Hammerspoon uses, along with some AppleScript code.

The example Lua code and example AppleScript code, shown further below, was tested under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes all necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

In addition to Hammerspoon, the third-party Hammerspoon module hs._asm.undocumented.spaces to have access to the undocumented Spaces API, the Lua code and AppleScript code, as is and provided further below, all necessary Security & Privacy settings for operational control, one preference setting in particular is required to be unchecked in System Preferences > Mission Control:

  • [ ] Automatically rearrange Spaces based on most recent use

This is of course to keep the main working Desktop/Space as the first one in the list, thus having a numeric ID of 1, as ascertained by the module that provides access to the undocumented Spaces API.

This also assumes the Apple default keyboard shortcut to hide/show the Dock is in play.

Option-Command-D: Show or hide the Dock.

    From: Mac keyboard shortcuts

Also, as coded, only a single display/monitor was used. Additional coding may/would be required for a multi display/monitor arrangement. However, not being able to test at this time, it is outside the scope of the solution offered herein.



  • Note that the download and install instructions assume Safari and default preference [√] Open “safe” files after downloading checked.

Download Hammerspoon

Download hs._asm.undocumented.spaces



Note: During the initial install and setup of Hammerspoon and the hs._asm.undocumented.spaces module you will be presented with a number of Security & Privacy related dialog boxes and notifications to which you need to reply with an affirmative type response, e.g., [Allow], [Open], [OK], [Allow Anyway]. If the choice is [Move to Trash] or [Cancel] choose [Cancel], or if the choice is [Open], [Move to Trash], [Cancel] choose [Open]!



Install Hammerspoon

  • Open the Downloads folder and drag and drop the Hammerspoon application bundle into Applications.

  • Before you open Hammerspoon, add it to: System Preferences > Security & Privacy > Privacy > Accessibility

    • Hint: Leave System Preferences open and unlocked as you will need it again shortly.

Open Hammerspoon from Applications or Spotlight, then:

From the Hammerspoon menu (hammer icon) on the menu bar, click it, then click Preferences…, and set them as follows:

Hammerspoon Preferences

Behavior:

  • [√] Launch Hammerspoon at login
  • [√] Check for updates
  • [ ] Show dock icon
  • [√] Show menu icon
  • [ ] Keep Console window on top
  • [ ]Send crash data (requires restart)

Accessibility: Accessibility is enabled. Your all set!
         Enable Accessibility (Should be grayed out.)

Close Hammerspoon Preferences.



Install the hs._asm.undocumented.spaces module.

Open: Downloads

Open the hs._asm.undocumented.spaces-master folder to verify the name of the included spaces-vX.Y.tar.gz file against the commands used in the next install step in Terminal.

Open Terminal and run the following commands:

  • The current version of spaces-vX.Y.tar.gz, as of the day of this answer, is: spaces-v0.2.1.1-universal.tar.gz

    cd ~/.hammerspoon
    tar -xzf ~/Downloads/hs._asm.undocumented.spaces-master/spaces-v0.2.1.1-universal.tar.gz
    

Close: Terminal (Assumes no error from previous command.)



Adding the Lua code to Hammerspoon

Open the Hammerspoon configuration file by clicking the Open Config command from the Hammerspoon menu (hammer icon) on the menu bar.

Add the example Lua code, from below, to the init.lua configuration file and save the document, then close it.



Important Steps

In this next step the init.lua configuration file will get reloaded from the Reload Config command on the Hammerspoon menu (hammer icon) on the menu bar.

Note: The Reload Config command will fail because the internal.so module cannot be verified.

Click: Cancel

Then on the System Preferences > Security & Privacy > Privacy > General pane:

  • “internal.so” was blocked from use because it is not from an identified developer.

Click: Allow Anyway

Close the Hammerspoon Console window and the Hammerspoon notification.

Again, click the Reload Config command on the Hammerspoon menu (hammer icon) on the menu bar.

It should not error this time, but click Open to the message:

  • macOS cannot verify the developer of “internal.so”. Are you sure you want to open it?

Phew! That's almost it! Your almost done and ready to go! Only one more security step to address when you switch spaces for the first time.

  • “Hammerspoon” wants access to control “System Events”. Allowing control will provide access to documents and data in “System Events”, and to perform actions within that app.

    Hammerspoon needs to send events to automate applications.

Click: OK



Yeah! Now you are done and ready to change Desktop/Space and the Dock will then automatically hide on all Desktops/Spaces except the main one.



Note: The example Lua code for Hammerspoon's init.lua configuration file is presented in two sections, the first is just the necessary Lua code and the second is commented code with URLs to each relevant Hammerspoon API and the hs.asm.undocumented.spaces module.

Example Lua code:

spaces = require("hs._asm.undocumented.spaces")

function spaceHasChanged()
    local asCommand = "tell application \"System Events\" to return autohide of dock preferences"
    local ok, theDockIsHidden = hs.osascript.applescript(asCommand)
    if ok then
        local spaceNumericID = spaces.activeSpace()
        if spaceNumericID == 1 and theDockIsHidden then
            hs.eventtap.keyStroke({"cmd", "alt"}, "d")
        elseif spaceNumericID > 1 and not theDockIsHidden then
            hs.eventtap.keyStroke({"cmd", "alt"}, "d")
            local asFile = "/.hammerspoon/Scripts/FSAWWODS.applescript"
            local ok, status = hs.osascript.applescriptFromFile(os.getenv("HOME") .. asFile)
            if not ok then
                local msg = "An error occurred running the FSAWWODS.applescript script."
                hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
            end
        elseif spaceNumericID == "" or spaceNumericID == nil then
            local msg = "An error occurred getting the active space ID! Review Console log."
            hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
        end
    else
        local msg = "An error occurred getting the value of autohide for the Dock."
        hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
    end
end

spaceWatcher = hs.spaces.watcher.new(spaceHasChanged)
spaceWatcher:start()
-- spaceWatcher:stop()



    -- The multi-line commented section below is the 
    -- commented code above, with URLs to each relevant 
    -- Hammerspoon API and asm.undocumented.spaces module.
    -- This is provided for users new to Hammerspoon 
    -- to help the user understand what the code is doing.

 --[[ 

    -- This module provides Hammerspoon with access to the undocumented Spaces API.
    -- https://github.com/asmagill/hs._asm.undocumented.spaces
     
spaces = require("hs._asm.undocumented.spaces")

    -- User created function for use with 'hs.spaces.watcher' submodule.
    -- Each time the Desktop/Space changes this function is called.
    -- https://www.hammerspoon.org/docs/hs.spaces.watcher.html

function spaceHasChanged()

        -- Use AppleScript to get the value of the 'autohide' preference of the Dock.
        -- https://www.hammerspoon.org/docs/hs.osascript.html#applescript

    local asCommand = "tell application \"System Events\" to return autohide of dock preferences"
    local ok, theDockIsHidden = hs.osascript.applescript(asCommand)
    
    if ok then
    
            -- The AppleScript command executed without error.
            
            -- Get the numeric ID of the currently active space.
            -- https://github.com/asmagill/hs._asm.undocumented.spaces
    
        local spaceNumericID = spaces.activeSpace()
        
            -- Toggle Hide/Show of the Dock as required. 
        
        if spaceNumericID == 1 and theDockIsHidden then 
        
                -- Press ⌥⌘D to unhide the Dock.
                -- https://www.hammerspoon.org/docs/hs.eventtap.html#keyStroke
                
            hs.eventtap.keyStroke({"cmd", "alt"}, "d")
            
        elseif spaceNumericID > 1 and not theDockIsHidden then

                -- Press ⌥⌘D to hide the Dock.
                -- https://www.hammerspoon.org/docs/hs.eventtap.html#keyStroke
                
            hs.eventtap.keyStroke({"cmd", "alt"}, "d")
            
                -- Use AppleScript to work around an apps window that does not 
                -- go to its own Desktop/Space when placed in full screen view.
                -- This is included and coded for VLC media player. Other apps
                -- that fall into this category will need to have their own
                -- code added to ~.hammerspoon/Scripts/FSAWWODS.applescript
                --https://www.hammerspoon.org/docs/hs.osascript.html#applescriptFromFile
                
            local asFile = "/.hammerspoon/Scripts/FSAWWODS.applescript"
            local ok, status = hs.osascript.applescriptFromFile(os.getenv("HOME") .. asFile)

            if not ok then

                    -- An error occurred running the FSAWWODS.applescript file.
                    -- https://www.hammerspoon.org/docs/hs.notify.html#new
                    -- https://www.hammerspoon.org/docs/hs.notify.html#send
                
                local msg = "An error occurred running the FSAWWODS.applescript script."
                hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
                
            end
            
        elseif spaceNumericID == "" or spaceNumericID == nil then
        
                -- An error occurred getting the active space ID.
                -- https://www.hammerspoon.org/docs/hs.notify.html#new
                -- https://www.hammerspoon.org/docs/hs.notify.html#send
        
            local msg = "An error occurred getting the active space ID! Review Console log."
            hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
            
        end
        
    else
    
            -- An error occurred getting the value of autohide for the Dock.
            -- https://www.hammerspoon.org/docs/hs.notify.html#new
            -- https://www.hammerspoon.org/docs/hs.notify.html#send
        
        local msg = "An error occurred getting the value of autohide for the Dock."
        hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
        
    end
    
end

    -- Each time the Desktop/Space changes, run the user created function.
    -- https://www.hammerspoon.org/docs/hs.spaces.watcher.html
    
spaceWatcher = hs.spaces.watcher.new(spaceHasChanged)
    
    -- Start the 'spaceWatcher'. The commented 'spaceWatcher:stop()' line is there for
    -- convenience to uncomment/comment it and 'spaceWatcher:start()' and reload the
    -- config file from the Reload Config command from the Hammer icon on the menu bar.

spaceWatcher:start()
-- spaceWatcher:stop()

--]]
  • Note: Because of the comments the Lua code is long. It ends with --]], so make sure you highlight all of it when copy and paste it to the init.lua configuration file.
  • Note: This example Lua code has been updated to include additional Lua code used in conjunction with the example AppleScript code in the Update to Original Answer section below.


Update to Original Answer

This update is to address an issue found after posting the original answer.

Applications whose window when placed in fullscreen view and is not moved to its own Desktop/Space and hides the Dock will cause the Lua code as originally written to show the Dock, the opposite of what's wanted.

The application in question is VLC media player. There may also be other applications that do the same thing and additional code can be added to accommodate as/when needed.

Note that if you do not use VLC media player you should still do this so the framework is in place to accommodate any application that will require such a workaround.

I have updated the original Lua code I posted, and you will need to copy and paste it into the init.lua configuration file, replacing the original code in your init.lua configuration file with it and save it, but do not reload it until the next steps are taken. Use the information in the Adding the Lua code to Hammerspoon section above to add the updated Lua code.

Adding the AppleScript script to Hammerspoon

In Terminal:

mkdir -p ~/.hammerspoon/Scripts
touch ~/.hammerspoon/Scripts/FSAWWODS.applescript
open ~/.hammerspoon/Scripts/FSAWWODS.applescript

Close: Terminal (Assumes no error from previous command.)


In Script Editor:

Add the example AppleScript code, shown below, to the opened FSAWWODS.applescript document, then save it, and close the document.

Close: Script Editor

With that done, you can now click the Reload Config command on the Hammerspoon menu (hammer icon) on the menu bar, as the update is completed.



Example AppleScript code:

--  # 
--  # NOTE: Hammerspoon does not allow the use of the
--  #       AppleScript line continuation character.
--  #       Therefore code must be written in long form.
--  #       It also doesn't like the use of some characters
--  #       such as symbols for modifier keys and others
--  #       in comments and possibly in code as well.
--  # 
--  # Name: FSAWWODS.applescript
--  # 
--  #       Full Screen Application Window Without Own Desktop/Space
--  #
--  # Purpose:
--  #
--  # The purpose of this script is to workaround an issue in 
--  # hiding the Dock when an application is in fullscreen view
--  # and is not moved to its own separate Desktop/Space.
--  # 
--  # Currently this script is configured for the VLC media player.
--  # Each application that has this issue will need its own code.
--  # The code will be different based on the specific application.
--  # 
--  # The issue with VLC is that when in fullscreen view it hides
--  # the Dock thus causing the Lua code to unhide it. I suspect
--  # other apps that do the same thing will need to press the
--  # Option-Command-D keyboard shortcut again in order to counter
--  # the Lua code from unhiding it in similar scenarios.
--  # 
--  # VLC is only app I have encountered this issue with so far.
--  # 


tell application "System Events"
    set |fmAppName| to name of process 1 whose frontmost = true
end tell

if |fmAppName| is "VLC" then
    tell application "System Events" to tell process "VLC"
        tell menu item "Fullscreen" of menu 1 of menu bar item "Video" of menu bar 1
            set isEnabled to value of attribute "AXEnabled"
            set isFullscreen to value of attribute "AXMenuItemMarkChar"
        end tell
        if isEnabled and isFullscreen is not missing value then
            keystroke "d" using {option down, command down}
        end if
    end tell
else if |fmAppName| is "NameOfApp" then
    --  # Add additional 'else if' as needed.
    --  # Code for NameOfApp app goes here.
end if


Notes:

Just a reminder that this update is only applicable to applications whose window when placed in fullscreen view is not moved to its own Desktop/Space and also hides the Dock.

Because of this particular issue, the Dock will appear to bounce as the value of its autohide preference is toggled by the extra ⌥⌘D needed in this instance. It is still better than having to manually press ⌥⌘D all the time.

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top