Forum Archive

Why does the custom shortcut append the editor file path ?

eddo888

if you run the following as a custom shortcut

import sys, editor, console

console.clear()

print('\nwhy does the custom shortcut append the editor file path ?')
print(sys.argv)

print('\nwhen you can get the path from the editor library')
print(editor.get_path())

print('\nwhich is a more intuitive way to do it')
cvp

@eddo888 If thé script of your shortcut is not in one of the editor tabs,
editor.get_path() will return the path of the open tab, not the path of your script.

But, perhaps I didn't understand correctly your question, thus sorry

eddo888

the actual calling script is in sys.argv[0]
the current editor can be obtained from editor.get_path()
so why does shortcut need to append the editor path to the args, this is redundant and not the same behaviour as from the command line cf stash
i use the args in the shortcut and have to do all sorts of ugly logic to remove the editor path

ccc

sys.argv.pop() # does not seem too ugly

cvp

@eddo888 I'm sorry but I have to insist.
If you start a script from an (home screen) shortcut, you can't be sure that the script is in an edited tab. In this case, editor.get_path will not return the path of your script but the path of the edited script......which could be different of the wanted path.

eddo888

@cvp sys.argv[0] is allways the actual script

cvp

@eddo888 Sure. I only explain why you never have to use editor.get_path

eddo888

@ccc said:

sys.argv.pop() # does not seem too ugly

more like

try:
    # from pythonista libraries
    import editor
    # fix for pythonista tools icons
    if sys.argv[-1] == editor.get_path():
        argv = sys.argv[1:-1]
    else:
        argv = sys.argv[1:]
except:
    argv = sys.argv[1:]
eddo888

@cvp Thanks for responding, it is encouraging that a thriving Pythonista community exists.
my desire is to have a Rossum approach to code so my scripts work on all my platforms

ccc
import editor
# fix for pythonista tools icons
if sys.argv[-1] == editor.get_path():
    sys.argv.pop()
argv = sys.argv[1:]
eddo888

@ccc at the risk of going on about it,
if shortcusts didn't put a redunant arg on the args list
I wouldn't have to do anything.

JonB

The main use case for wrench shortcuts was for processing scripts in the editor.
thus, for scripts which take one arg, a filename, sys.argv is being populated the way you would run from the command line -- [0] is the command name, [1] is the target file. Anyway, that's the rationale behind the first two args -- the idea was to be able to have a script that accepts a file name, and does something to that file -- publishing to a blog, converting markdown to html, etc -- and you could run that script on your desktop or on pythonista in the same way.

Running from the play button or from a home screen shortcut probably shouldn't populate argv[1] unless you asked it to.

Arguably, it would be nice to have a switch in the wrench editor, that says whether it populates argv[1] with the editor filename, or not.

If you want to simulate a pure command line that doesn't pass in the editor filename, you should consider writing a little launcher script, that populates sys.argv, and then calls runpy that way you can run your other scripts. I.e have a single launcher script that accepts as argv the module to run and any argv -- then simply fix argv and call runpy.

eddo888

elegent response, thanks for taking the time to provide a thorough detailed description. I'm glad I have an opportunity to try and contribute to such a wonderful app such is Pythonista. one of my favourite in flight/bus/walking/lunch/etc entertainment apps.