How to create keyboard shortcuts



  • A number of people have asked for keyboard shortcuts for Movie Mode, Disable, etc.

    There you are:

    (*************************************************************************
    
    Shortflux - menu shortcuts for f.lux on OS X
    
    Allows picking items from the f.lux menubar menu, with hotkeys or other
    automation. Works even if the menubar icon is hidden with Bartender.
    Especially useful for toggling "Movie mode" or "Disable for an hour".
    Set the menu item you want in the Settings section below. You can make
    multiple copies of the script with different names, to trigger different
    functions.
    
    Paste this into Script Editor, then save it as a script or an
    application, depending how you will trigger it. Then use your favourite
    launcher app, such as Automator, Karabiner, BetterTouchTool, Alfred,
    FastScripts, QuicKeys, and so on, to run it. There may be a delay of
    several seconds before the menu item is picked, during which you should
    hold your breath.
    
    Shortflux Version 1.0a.20160131.0
    by Elhem Enohpi 2016 - Artistic Licence 2.0
    f.lux is a trademark of Flux Software LLC
    Elhem Enohpi and Shortflux are not associated with Flux Software LLC
    
    *************************************************************************)
    
    -- Settings --
    
    property mainItem : "Color Effects"
    -- set to "Preferences...", "Color Effects", "Disable", etc.,
    -- make sure to use quote marks.
    
    property subItem : "Movie mode"
    -- set to submenu item, if there is one. Use "for this app" with Disable,
    -- to toggle disable for the current application.
    
    -- end of Settings --
    
    
    if mainItem is "Disable" and subItem is "for this app" then set subItem to 3
    tell application "System Events" to tell process "Flux"
    	click menu bar item 1 of menu bar 1
    	tell menu 1 of menu bar item 1 of menu bar 1
    		select menu item mainItem
    		delay 0.6
    		click menu item mainItem
    		if menu 1 of menu item mainItem exists then
    			tell menu 1 of menu item mainItem
    				select menu item subItem
    				delay 0.6
    				click menu item subItem
    			end tell
    		end if
    	end tell
    end tell
    


  • Thank you.. This works.. Any way to make it faster?



  • @musafirsafwan said in How to create keyboard shortcuts:

    Thank you.. This works.. Any way to make it faster?

    Try this:

    -- Settings --
    
    property mainItem : "Disable"
    -- set to "Preferences...", "Color Effects", "Disable", etc.,
    -- make sure to use quote marks.
    
    property subItem : "for an hour"
    -- set to submenu item, if there is one. Use "for this app" with Disable,
    -- to toggle disable for the current application.
    
    -- end of Settings --
    
    if mainItem is "Disable" and subItem is "for this app" then set subItem to 3
    
    tell application "System Events"
      tell application process "Flux"
        tell menu bar 1
          tell menu bar item 1
            try
              with timeout of 0.1 seconds
                perform action "AXPress"
              end timeout
            end try
          end tell
        end tell
      end tell
    end tell
    do shell script "killall 'System Events'"
    tell application "System Events"
      tell application process "Flux"
        tell menu bar 1
          tell menu bar item 1
            tell menu 1
              tell menu item mainItem
                perform action "AXPress"
              end tell
              if menu 1 of menu item mainItem exists then
                tell menu 1 of menu item mainItem
                  tell menu item subItem
                    perform action "AXPress"
                  end tell
                end tell
              end if
            end tell
          end tell
        end tell
        if mainItem is "Preferences..." then set frontmost to true
      end tell
    end tell
    


  • Hi @ttime

    I have tried many ways including many other scripts on the web,
    however this is the only one that actually works.
    Impressive work!
    Others were not able to trigger menu bar icons as apparently they are not "named".

    Anyway,
    I am just trying to play around with your script so that it would work together with apps like bartender. Is there any way to achieve so?

    Regards,
    Jazor



  • @Jazor-CH-Ho said in How to create keyboard shortcuts:

    I am just trying to play around with your script so that it would work together with apps like bartender. Is there any way to achieve so?

    I don't have Bartender, but if the script doesn't work with Bartender then probably Bartender is messing up the menu structure.

    When it comes to identify GUI elements like for example menu items, there are a couple of tools that make your life easier:

    • Script Debugger: A really superior replacement for Apple's Script Editor. It shows you lots of valuable information on GUI elements. Well worth the price if you are regularly applescripting.
    • Accessibility Inspector: Shows you real-time information on GUI elements. Comes with Xcode, free.
    • UI Browser: Similar to Accessibility Inspector but way more comfortable.

    – Tom



  • @ttime
    Thanks for the reply.

    I have managed to find a way using Alfred. All I did was telling Alfred to press Bartender's hotkey then run your script, and it worked.

    Thanks again for your code.

    Regards



  • @Jazor-CH-Ho said in How to create keyboard shortcuts:

    All I did was telling Alfred to press Bartender's hotkey then run your script

    You can also press hotkeys from within the script:

    For example if your Bartender hotkey is Shift-Control-Option-Command-M then insert these lines just after the first occurrence of tell application "System Events":

    keystroke "m" using {command down, control down, option down, shift down}
    delay 0.2
    

    Adapt the keystroke command to your needs. Perhaps the delay isn't necessary.

    – Tom


Log in to reply
 

Looks like your connection to f.lux forum was lost, please wait while we try to reconnect.