Skip to content

Instantly share code, notes, and snippets.

@jsbain

jsbain/add_action.py

Last active Dec 9, 2016
Embed
What would you like to do?
add_action.py
# coding: utf-8
'''
Manipulate action (wrench) menu
example:
add_action('/stash/launch_stash.py','monitor')
save_defaults() # so it is stored for next launch
'''
from objc_util import *
NSUserDefaults = ObjCClass('NSUserDefaults')
def add_action(scriptName,iconName='python',iconColor='',title=''):
'''adds an editor action. scriptName should start with /
(e.g /stash/stash.py)
iconName should be an icon without leading prefix, or trailing size. i.e alert instead of iob:alert_256
iconColor should be a web style hex string, eg aa00ff
title is the alternative title
Call save_defaults() to store defaults
')'''
defaults=NSUserDefaults.standardUserDefaults()
kwargs=locals()
entry={ key:kwargs[key]
for key in
('scriptName','iconName','iconColor','title','arguments')
if key in kwargs and kwargs[key] }
editoractions=get_actions()
editoractions.append(ns(entry))
defaults.setObject_forKey_(editoractions,'EditorActionInfos')
def remove_action(scriptName):
''' remove all instances of a given scriptname.
Call save_defaults() to store for next session
'''
defaults=NSUserDefaults.standardUserDefaults()
editoractions=get_actions()
[editoractions.remove(x) for x in editoractions if str(x['scriptName'])==scriptName]
defaults.setObject_forKey_(editoractions,'EditorActionInfos')
def remove_action_at_index(index):
''' remove action at index. Call save_defaults() to save result.
'''
defaults=NSUserDefaults.standardUserDefaults()
editoractions = get_actions()
del editoractions[index]
defaults.setObject_forKey_(editoractions,'EditorActionInfos')
def get_defaults_dict():
'''return NSdictionary of defaults'''
defaults=NSUserDefaults.standardUserDefaults()
return defaults.dictionaryRepresentation()
def get_actions():
'''return action list'''
defaults=NSUserDefaults.standardUserDefaults()
return list(defaults.arrayForKey_('EditorActionInfos'))
def save_defaults():
'''save current set of defaults'''
defaults=NSUserDefaults.standardUserDefaults()
NSUserDefaults.setStandardUserDefaults_(defaults)
@ywangd

This comment has been minimized.

Copy link

@ywangd ywangd commented Mar 4, 2016

@jsbain I am going to create a command "action" based on this script if you are OK with it.
I am thinking that the command would be used like:

action add ..
action remove ...
action list ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.