spakin / SimpInkScr

Simple Inkscape Scripting
https://inkscape.org/~pakin/%E2%98%85simple-inkscape-scripting
GNU General Public License v3.0
320 stars 31 forks source link

Saved Scripts #61

Closed jeff-miller-cfa closed 1 year ago

jeff-miller-cfa commented 1 year ago

Is there a way to save a list of scripts and add shortcuts for them?

spakin commented 1 year ago

I don't know a good way, but perhaps you'll find the following approach somewhat tolerable:

import tkinter

root = tkinter.Tk()
root.title('Script selection')
root.geometry('200x100')
options_list = [
    'Square',
    'Circle',
    'Triangle'
]
selection = tkinter.StringVar(root)
selection.set('Select a script')
menu = tkinter.OptionMenu(root, selection, *options_list)
menu.pack()

def run_script():
    "Run a script based on the user's selection."
    sel = selection.get()
    if sel == 'Square':
        rect((0, 0), (100, 100), fill='yellow')
    elif sel == 'Circle':
        circle((50, 50), 50, fill='red')
    elif sel == 'Triangle':
        path([Move(50, 0), Line(100, 100), Line(0, 100), ZoneClose()], fill='green')
    root.destroy()
    return None

submit_button = tkinter.Button(root, text='Run', command=run_script)
submit_button.pack()
root.mainloop()

That script uses Tkinter to present a menu of predefined scripts to run. While the code as presented inlines the predefined scripts, it shouldn't be too hard to alter run_script to read an external script and exec it.