Escape whitespace in paths using nautilus script

十年热恋 提交于 2019-12-04 16:21:31

I think that $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS should be newline delimited, not space delimited. In that case, the following should work from bash:

echo -e $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | xargs python uploader.py 

Skipping one level of evaluation shows that the nautilus documentation is incomplete and that there is a better way much less subject to who-knows-how-many levels of interpretation. Nautilus passes selected files as script arguments:

$ cat ~/.gnome2/nautilus-scripts/arguments.sh
#!/bin/sh
rm -f /tmp/arguments.*
outf=/tmp/arguments.$$
echo "$0: $#" > $outf
while [ $# -gt 0 ] ; do
    echo "$1"
     if [ ! -r $1 ] ; then echo "cwd is not correct"; fi
    shift
done >> $outf
echo paths $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS >> $outf
for i in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS ; do
    echo "+$i+"
done >> $outf

I sent the output to /tmp since I didn't want to hunt for the stdout. Given:

$ ls -1
a
b
c with space
d
e with space
g

Select all the files in the directory and Scripts->arguments.sh yields:

$ cat /tmp/arguments.20447 
/home/msw/.gnome2/nautilus-scripts/arguments.sh: 6
a
b
c with space
d
e with space
g
paths /home/msw/junk/a /home/msw/junk/b /home/msw/junk/c with space 
  /home/msw/junk/d /home/msw/junk/e with space 
  /home/msw/junk/g
+/home/msw/junk/a+
+/home/msw/junk/b+
+/home/msw/junk/c+
+with+
+space+
+/home/msw/junk/d+
+/home/msw/junk/e+
+with+
+space+
+/home/msw/junk/g+

Could I have quoted $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS to avoid this? Sure. I didn't to demonstrate that how many levels of interpolation with the variable is questionable, but argv stays unmolested.

Use argv (or sys.argv in Python) and save some headache. Also the documented semantics of the environment variables is weird.

I can't test this as I'm at a Windows machine, but have you tried using $NAUTILUS_SCRIPT_SELECTED_FILE_URIS instead? Then, in python you could get the paths with something like:

[f.strip() for f in os.environment['NAUTILUS_SCRIPT_SELECTED_FILE_URIS'].split('file://') if len(f) > 0]

It turns out, echo sometimes turns newlines in variables into spaces. The following should work:

echo -e "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | xargs python uploader.py
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!