You are not logged in.
Hi,
I have a python script which copy the full path of text files opened in gedit:
#!/usr/bin/env python3
import subprocess
import sys
name = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip()
if all(["(" in name, ")" in name]):
path = name[name.find("(")+1:name.find(")")]
if sys.argv[1] == "-file":
fname = name[:name.find("(")]
elif sys.argv[1] == "-path":
fname = ""
command = "echo "+'"'+path+"/"+fname+'"'+" | tr -d '\n' | sed 's/.$//' | xclip -selection clipboard"
subprocess.Popen(["/bin/bash", "-c", command])
Works as expected, but I'd like to have the result reported inside quotes:
"path/to/file"
I've tried various attempts using sed, but the code looks "ugly", "strange" and doesn't work, maybe because this part
command = "echo "+'"'+path+"/"+fname+'"'+" | tr -d '\n' | sed 's/.$//' | xclip -selection clipboard"
already have various quotation, and adding quotes to the code cause a mess.
How to achieve in the best way?
Offline
I don't know exactly this is not a native language that I use but maybe you will like this
#!/usr/bin/env python3
import subprocess
import sys
name = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip()
if all(["(" in name, ")" in name]):
path = name[name.find("(") + 1:name.find(")")]
if sys.argv[1] == "-file":
fname = name[:name.find("(")]
elif sys.argv[1] == "-path":
fname = ""
command = 'echo "' + path + '/' + fname + '" | tr -d "\n" | sed "s/.$//" | xclip -selection clipboard'
subprocess.Popen(["/bin/bash", "-c", command])
Offline
#!/usr/bin/env python3
import subprocess
import sysname = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip()
if all(["(" in name, ")" in name]):
path = name[name.find("(") + 1:name.find(")")]
if sys.argv[1] == "-file":
fname = name[:name.find("(")]
elif sys.argv[1] == "-path":
fname = ""
command = 'echo "' + path + '/' + fname + '" | tr -d "\n" | sed "s/.$//" | xclip -selection clipboard'
subprocess.Popen(["/bin/bash", "-c", command])
Hi, unfortunately doesn't work: the path is still reported without quotes.
Offline
I solved with a bash script:
#!/bin/bash
path=$(xdotool getactivewindow getwindowname | grep -oP '\(\K[^)]+')
path2="${path/#\~/$HOME}"
filename=$(xdotool getactivewindow getwindowname | cut -d"(" -f1 | rev | cut -c2- | rev)
echo "\"$path2/$filename"\" | tr -d '\n' | xclip -selection clipboard
And so, I got, eg: "/home/dave/Documents/text file"
Last edited by D.dave (2023-05-20 17:41:10)
Offline
Here a simple example for the command "date" and how to print its output in quotes:
import subprocess
import sys
command = "date"
output_bytes = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE).stdout.read()
output = output_bytes.decode().strip()
print(f'"{output}"')
Offline
[ Generated in 0.007 seconds, 7 queries executed - Memory usage: 561.15 KiB (Peak: 609.02 KiB) ]