macosapplescripthammerspoon

how can I run a script when I create a new workspace on my Mac?


I want to add a sticky note to my workspace when I created it automatically,but I don't know what's the event when I created a new space?

Can I use applescript or Hammerspoon?

enter image description here


Solution

  • As far as I know, there is not a native keyboard shortcut that can create a new Desktop. Both Mission Control and Stickies do not have an AppleScript Dictionary and their processes has scripting terminology property are set to false. Therefore a UI Scripting solution is doable although maybe not favorable, and the only way I know of with vanilla AppleScript.

    The following was tested under macOS High Sierra with the macOS defaults in place for the keyboard shortcut to expose Mission Control, which is Control–Up Arrow (⌃↑). This setting is found at:

    System Preferences > Keyboard > Shortcuts > Mission Control > [√] Mission Control    ⌃↑

    The following example AppleScript code assumes the aforementioned setting to be true and enabled:

    Creates a new Desktop and sets focus to it:

    tell application "System Events"
        key code 126 using control down -- # Control-Up Arrow 
        delay 0.25
        tell group 2 of group 1 of group 1 of application process "Dock"
            click (every UI element whose description is "add desktop")
            delay 0.25
            click last UI element of list 1
        end tell
    end tell
    

    That's the easy part. The issue with Stickies is that the notes created with it are not pin-able to a specific Desktop and as such while you can programmatically create a new note on the new Desktop it's not going to stay there and the next time you open Stickies, all notes will be on the active Desktop where Stickies was opened. So, is there really any point in creating a new note on the newly created Desktop? My gut says no!

    That said, the following example AppleScript code does go through the steps and is only included because it was part of your question, but in my opinion it's not practical and is an exercise in futility.

    Creates a new Desktop, sets focus to it, and creates a new Stickies note:

    if running of application "Stickies" then
        tell application "Stickies" to quit
        delay 0.5
    end if
    tell application "System Events"
        key code 126 using control down -- # Control-Up Arrow 
        delay 0.25
        tell group 2 of group 1 of group 1 of application process "Dock"
            click (every UI element whose description is "add desktop")
            delay 0.25
            click last UI element of list 1
        end tell
        tell application "Stickies" to activate
        delay 1
        keystroke "n" using command down
        delay 0.25
        keystroke "This is a new Stickies note on a new Desktop."
    end tell 
    

    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 accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted. Note that the use of the try command as an error handler is very handy with UI Scripting.