some nice things I made, maybe you'll want to steal them :)

1,429 views
Skip to first unread message

Szymon Kaliski

unread,
Nov 4, 2015, 2:03:39 PM11/4/15
to Hammerspoon
Hey!

I've been long-time Mjolnir user, but recently switched to HS, and boy was it worth it :)
I'm the guy who adapted animated window transforms as Mjolnir (that were originally made by some other guy), that are in HS now.

Anyways, few things I've been working on:


Forces app to launch, think of the times when launchOrFocus("finder") isn't working, or you have calendar opened on some other space and then launchOrFocus("calendar") isn't showing you any windows, etc... This function tries launchOrFocus first, waits 500ms, and then checks if frontmostApp is still the one that you wanted, and if it is, but there are no windows, then tries selecting Window→APPNAME which usually focuses the app, or sends cmd+n to create new window. There are still some cases where it brakes (Slack), so if you have any ideas how to update — let me know :)


You use it like this: hs.hotkey.bind({ 'cmd', 'ctrl' }, 'b', function() smartLaunchOrFocus({ 'Safari', 'Google Chrome' }) end) — if you hit cmd-ctrl-b and neither safari nor chrome is running it will launch safari (because it's first in the table) and create it's window with forceLaunchOrFocus. If there is Safari window it will focus it. If there are more than one Safari windows it will cycle them (so hitting cmd-ctrl-b will move focus between all the windows). If there's safari and chrome it will focus any windows of any of those two apps, and cycle between them. If there's only chrome opened, it will only open it's window.

My usecase is this: for "normal" browsing I use Safari, so I hit cmd-ctrl-b and start it, and then hit it again to re-focus, but for dev work I ten to use Chrome, so I start it with spotlight, and then cmd-ctrl-b will focus it's window. Hope it's clear :)


Thanks to hs.application.watcher (https://github.com/szymonkaliski/Dotfiles/blob/b5a640336efc9fde1e8048c2894529427746076f/Dotfiles/hammerspoon/init.lua#L723-L738) I assign this function to cmd+q with Safari and Chrome, it checks if there are more than 1 windows open (on all spaces, thanks to applescript), and shows dialog asking if I'm sure to quit. On some occasions I quit Safari when there was another window with some important stuff on separate space, so it helps me here :)
Strangely applescript shows that there are two windows open for Safari, if there's only one, that's why "count" is configurable.


This draws little dots on the bottom of my screen, imitating iOS space switcher. I can even click on them! It requires hs._asm.undocumented.spaces module to work, and looks like this:



I think it would be cool if we had proper space to share snippets like this, linking to whole init.lua files on wiki is nice, but mine is almost 800 lines of code, and I doubt if anyone wants to read all of that to find things like this (well, I sometimes waste time like that, but still...).


I'd be interested to see what things other people came up with, and to hear about your crazy HS workflows!


S.



Richard Guay

unread,
Nov 4, 2015, 9:09:10 PM11/4/15
to Hammerspoon
Where do I get the spaces module? I do not see it in the forums here. That would be great since the program I used to use is dead in El Capitan.

Szymon Kaliski

unread,
Nov 5, 2015, 4:25:32 AM11/5/15
to Richard Guay, Hammerspoon
Hey, this module is by asmagill and available here: https://github.com/asmagill/hammerspoon_asm.undocumented/tree/master/spaces :)

--
You received this message because you are subscribed to a topic in the Google Groups "Hammerspoon" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hammerspoon/VabaeIBw9qw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hammerspoon...@googlegroups.com.
To post to this group, send email to hamme...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/hammerspoon/d5ebcd19-b90c-458d-89f6-6fb9d288c062%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brad Parks

unread,
Nov 5, 2015, 12:54:46 PM11/5/15
to Hammerspoon
Thanks for the post... I've been wanting to implement something exactly like your "smartLaunchOrFocus", and will totally be using that... much appreciated!

Szymon Kaliski

unread,
Nov 5, 2015, 1:07:17 PM11/5/15
to Hammerspoon
Thanks!


Enjoy!

karlbe...@gmail.com

unread,
Jun 9, 2016, 3:14:19 AM6/9/16
to Hammerspoon
Hi! I’d like to use smartLaunchOrFocus with Safari, but when I copy your code from here to my init.lua and append a line with

hs.hotkey.bind({ 'cmd', 'ctrl' }, 'b', function() smartLaunchOrFocus({ 'Safari' }) end)

I get the following error: 

*** ERROR: /Users/USER/.hammerspoon/init.lua:255: attempt to index a nil value (global 'ext')


stack traceback:


        /Users/USER/.hammerspoon/init.lua:255: in main chunk


        [C]: in function 'xpcall'


        ...app/Contents/Resources/extensions/hs/_coresetup/init.lua:410: in function 'hs._coresetup.setup'


        (...tail calls...)



Wha

Chris Jones

unread,
Jun 9, 2016, 6:55:13 AM6/9/16
to karlbe...@gmail.com, Hammerspoon
Hey

If you take a look at the top of the init.lua, ext is defined as a table. This is Szymon containing all of his stuff in a little module of its own. You can either create the table, or strip the name of the function so it's just "smartLaunchOrFocus" instead of ext.app.smartLaunchOrFocus.

Cheers,
Chris

-- 
You received this message because you are subscribed to the Google Groups "Hammerspoon" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hammerspoon...@googlegroups.com.

To post to this group, send email to hamme...@googlegroups.com.

karlbe...@gmail.com

unread,
Jun 10, 2016, 9:59:02 AM6/10/16
to Hammerspoon, karlbe...@gmail.com
That works! Cycling through Safari Windows works but opening Safari when not running doesn’t. Neither does opening a new windows when safari is running without any windows. That should work I suppose?

Thanks for your help!

Szymon Kaliski

unread,
Jun 13, 2016, 10:14:42 AM6/13/16
to Hammerspoon, karlbe...@gmail.com
You still need ext.app.forceLaunchOrFocus - you need to either have a table like that, or rename this to forceLanuchAndFocus and copy that function inside...


Cheers!
S.
Reply all
Reply to author
Forward
0 new messages