Can one use a keyboard command to activate a script?

Is it possible to modify a script such that it activates after pressing a key keyboard combination?

Yes, open the preferences window and under Shortcuts you can assign a custom keyboard shortcut to any script.


Is it possible to use a script for navigation?

What kind of navigation? In general, scripts can do anything any other part of Glyphs can do, but for long-running tasks or code that needs to run in the background, a plugin is often more appropriate.

I would like to have something that would let me switch back and forth between two masters using one keyboard command. Either by it alternating between changing to the next/previous master, or something that would alternate between two predefined ones.

There doesn’t appear to be a shortcut that already lets me do that, so I believe a script or plugin as you mention might the correct solution.

This is a built-in feature in the ViewNavigation menu. The default shortcuts are Function-Option-Arrow Up and Function-Option-Arrow Down.

This could be done by a script.

Sure there is a command for this (I have it linked to Cmd+Shift+Plus/Minus). You know you can use Cmd+[Number] for switching between masters from 1-9?

View > Navigation > Previous/Next Master

Edit: Florian was quicker :wink:

You can also use the palette plugin Master Switcher, that is quite useful.
If you want to write a script, you can use

my_index = 0
Font.masterIndex = my_index

to switch between masters.

I might have explained what I meant in too little detail there. I have already use the built in “change to next/previous master” shortcut until now. The issue is that since I have now added to more masters to my font, that command will now alternate between all four masters instead of just the first two.

What I meant was a command that sends you to the next master when you use it, but the next time you use it, it sends you back to the previous master instead of the next one again. In a four master setup where you are currently viewing the first master, the command would change the view back and forth between the first and second master, while ignoring master 3 and 4.

That I didn’t know that actually, thanks. As for the default commands, those I usually map to cmd + k/l.

I need something slightly different than this at the moment though, I believe I explained it more throughly in my reply above.

Could you possibly explain it in a bit more detail?

Depends what you want to achieve with your script. What I meant to say is that you can define the currently active master with Font.masterIndex. If you set this to a certain index (provided it is available), the active master will change to that one.

This is exactly what the Master Switcher plugin does.

It doesn’t seem like it does on my end though. it seems to jump half the masters, in my font that would mean jumping back and forth between master 1 and 3, or master 2 and 4. Jumping between 1 and 2 or 3 and 4 doesn’t seem to be a specific function there.

It doesn’t appear that you can map any of that script’s functions to a keyboard command either.

I want to have a script that I can activate through a keyboard command that switches the active master between the first two of the four in my font.

If master 1 is in view, activating the script should change the viewed master to the second one. When master 2 is viewed, activating the script should change the viewed master to the first one

Why don’t you just use Cmd+1/2 in that case?

If, for whatever reason, you really, really want to trigger switching between masters 1 and 2 via an external script, try this:

Font.masterIndex = not Font.masterIndex

Again, I really don’t see the point of this, Cmd+1/2 is a lot faster (and works up to nine masters).

Well, creating such script would be an enormous job for that case.
using a shortcut is quicker, or like sebastian said, you can just use cmd+1 or cmd+2.

This is exactly what Previous Master and Next Master shortcut do. it can move back and forth without even have to cycle all the way through all the masters.

Thanks, that worked perfectly.