Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I wish to know "how", after pasting, the position of the cursor is determent (like with zen coding) programmatically.
With the 'insertion point' I intend to say: that blinking thing where our text shows up when we type.
So, let's say I paste this: '<text>%insertion point</text>'
I want the 'insertion point' to be at a specific location (in this example at %insertion point).
Some details:
I work with/on Linux (Ubuntu/Fluxbox)
Language doesn't really matter (prefer: Python, C, C++ (must be compatible with linux))
With pasting I intend: plain pasting in any text area (not in a terminal)
What I'm 'not' looking for:
a desktop program to do this, unless it's scriptable (such as shell
tools)
a text editor that provide this functionality (such as a vim module)
I'm also curious about tabbing to the next selection so i.e.:
<text>%first</text><detail>%second</detail>
If there is a better place on the internet for asking my question, please let me know about it.
Any information that would point me in the right direction would be highly appreciated.
Thank you for considering my question
EDIT:
I should mention that it doesn't necessary have to be after pasting, the important thing is to manipulate the location of the 'insertion point'.
You could use xvkbd to send the paste key combo CtrlV followed by a series of <- (left arrow) keys to accomplish the template behaviour and then map a Fluxbox keyboard shortcut to executing this command
The command would be something like
xvkbd -text "\Cv\[Left]\[Left]\[Left]\[Left]"
Then assign it to a shortcut in your ~/.fluxbox/keys file (Mod1 is Windows key)
Mod1 P :ExecCommand xvkbd -text "\Cv\[Left]\[Left]\[Left]\[Left]"
However this would need to be customised somehow for each template, and won't work for applications where CtrlV is not "paste" (like xterm). Perhaps for common templates you could just hardcode them to different shortcuts
Mod1 T :ExecCommand xvkbd -text "<text></text>\[Left]\[Left]\[Left]\[Left]\[Left]\[Left]\[Left]"
I suspect you're going to have a better time if you just customise each editor to offer this functionality separately.
EDIT
Here's a Python script that reads from stdin and writes output suitable for xvkbd. It uses the percent sign as the insertion point (eg <text>%</text>), so you need to escape any real percent signs with a backslash.
#!/usr/bin/env python
import sys
import re
clip = sys.stdin.read()
# split on percent sign not prefixed by backslash
parts = re.split(r'(?<!\\)%', clip, 1)
# un-escape percent sign
parts = [p.replace('\\%', '%') for p in parts]
tail_len = len(parts[-1])
print(''.join(parts) + '\[Left]' * tail_len)
You need to pipe the clipboard to it and then pass the result to xvkbd. I'm using xclip here to output the X clipboard to stdout
xclip -out | python pastetemplate.py | xargs -0 -I{} xvkbd -text "{}"
It still has the problem of taking a while to move the cursor, however it does automate the prior process.
I really don't think you're going to have any luck moving the cursor programmatically, as I imagine you'd have to use a different automation library depending on the GUI toolkit of the target app (eg GTK, QT, Flash, etc). However if you target a specific app or toolkit, you might be able to work it out.
Vim stores the position of the previously yanked / pasted / changed text with 2 registers namely '[ for start line and '] for the other end. There are character registers as well to know the exact character virtual column of start & end, read :h '[ for more info.
Related
(I'm not a pyschologist nor pysychopy export but helping one)
We're developing an expirement using the Coder (not Builder).
The output of an experiment is written out using the data.ExperimentHandler.addData().
When I run the application on my machine I get output like this:
,,1,1,z,f,o,o,1.254349554568762,0.8527608618387603,0.0,1.0,FSAS,,,,,,,,,,,,,,,52,male,52,
When the other person runs the application she gets:
;;0;1;z;z;j;j;105.498.479.999.369;0.501072800019756;1.0;1.0;FNES;;;;;;;;;;;;;;;23;male;0
(the difference I want to show is about format, not the values)
There are 2 differences:
RT values > 1 are printed wrong: the value 105.498.479.999.369 (RT less then 1.5s) should be 1.05498479999369
value separator is ; in my output and , in hers. I don't know what is the best value separator comma or semilcolon for later processing in R ?
I think it has to do something with regional settings.
Question: Is it possible to force the format so the application always generates the same output format independant from local settings ?
You don't specify whether you are using the graphical Builder interface to PsychoPy to generate your Python script, or writing a custom script directly in Python.
If using the Builder interface, click the "Experiment settings" toolbar icon select the "Data" tab, and specify that the datafile delimiter should be "comma" rather than "auto".
If running your own script, then in the saveAsWideText() method of the ExperimentHandler class, similarly specify ',' as the delim parameter. The API is here:
https://psychopy.org/api/data.html
In future, you will likely get better support at the dedicated PsychoPy forum at https://discourse.psychopy.org
The actual PsychoPy developers and other experienced users are much more likely to see your queries there compared to here at StackOverflow.
I want to write an extension or find some way to automatically import to an Inkscape document the new files that appear in a given folder. I don't want to simply update the SVG/XML document because Inkscape has to be open for another extension (AxiDraw) to work.
My problem is that I cannot find a way to make my extension work "in the background", either it goes into an infinite loop that crashes the program or, if I use for example the Timer from threading it just stops after the first execution.
Also using the command line doesn't seems to work since there is not an import command and verbs don't work in shell mode.
Is there some relatively easy way to work around these problems without having to dig into the c++ source code?
Well, turned out it is not possible. From the answer to my same question on the Inkscape forum:
Python extensions currently work by 'halting' the Inkscape main
process. Then they modify the document, which is then loaded back into
Inkscape.
So this cannot work. You'd need to run it manually in regular
intervals, or set up some 'click bot' that does that for you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have been looking at some python modules and powershell capabilities to try and import some data a database recently kicked out in the form of folders and text files.
File Structure:
Top Level Folder > FOLDER (Device hostname) > Text File (also contains the hostname of device) (with data I need in a single cell in Excel)
The end result I am trying to accomplish is have the first cell be the FOLDER (device name) and the second column contain the text of the text file within that folder.
I found some python modules but they all focus on pulling directly from a text doc...I want to have the script or powershell function iterate through each folder and pull both the folder name and text out.
This is definitely do-able in Powershell. If I understand your question correctly, you're going to want to use Get Child-Itemand Get Content then -recurse if necessary. As far as an export you're going to want to use Out-File which can be a hassle when exporting directly to xlsx. If you had some code to work with I could help better but until then this should get you started in the right direction. I would read up on the Getcommands because Powershell is very simple to write but powerful.
Well, Since you're asking in a general sense -- you can do this project simply in either choice of script languages. If it were me -- and I had to do this job once -- I'd probably just bang out a PoSH script to do it. If this script had to be run repeatedly, and potentially end up with more complex functions I'd probably then switch to Python (but that's just based on my personal preference as PoSH is pretty powerful in a Windows env). Really this seems like a 15 line recursive function in either choice.
You can look up "recursive function in powershell" and the same for python and get plenty of code examples, as File Tree Walks (FTW :) ) are one of the most solved problems on sites like this. You'd just replace whatever the other person is doing in their walk with a read of the file and write. You probably also want to output in CSV format because it's easier and imports into excell just fine.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm looking for a script that auto spams pictures of nicholas cage on the desktop
I have this script right now but what I want to do is make it automatically run as soon as the USB is plugged in
import shutil
src = ('Kim.jpg')
dst = ('H:/profile/desktop/Nic')
count = 1
while count < 10:
shutil.copyfile(src, dst + str(count) + ".jpg")
count += 1
Assumption: You are using Windows.
Almost on every new windows platform you need to approve an autorun of any executable so no leads here. But here is an alternative using some social/psychological engineering:
Convert your program to a standalone executable using a tool like py2exe. I assume that after conversion your file is named spammer.exe. Then paste this file in the USB and super-hide it by opening Command Prompt inside your USB and typing:
attrib +h +s +r spammer.exe
Now create a shortcut with and icon of a typical folder of Windows and name it something attractive (if you know what I mean) and point it to spammer.exe. The user (in excitement) clicks at it and Kaboom!
Assuming that you're on a version of windows that isn't too old and you have physical access to the machine in question, you should be able to follow these instructions:
http://windows.microsoft.com/en-us/windows/run-program-automatically-windows-starts#1TC=windows-7
Create a .bat file that executes your script. You could even have the script sleep for a random (or semi-random) period of time before going off.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Ideally, I'd like a module or library that doesn't require superuser access to install; I have limited privileges in my working environment.
I've been working on a library called Pyth, which can do this:
http://pypi.python.org/pypi/pyth/
Converting an RTF file to plaintext looks something like this:
from pyth.plugins.rtf15.reader import Rtf15Reader
from pyth.plugins.plaintext.writer import PlaintextWriter
doc = Rtf15Reader.read(open('sample.rtf'))
print PlaintextWriter.write(doc).getvalue()
Pyth can also generate RTF files, read and write XHTML, generate documents from Python markup a la Nevow's stan, and has limited experimental support for latex and pdf output. Its RTF support is pretty robust -- we use it in production to read RTF files generated by various versions of Word, OpenOffice, Mac TextEdit, EIOffice, and others.
OpenOffice has a RTF reader. You can use python to script OpenOffice, see here for more info.
You could probably try using the magic com-object on Windows to read anything that smells ms-binary. I wouldn't recommend that though.
Actually parsing the raw data probably won't be very hard, see this example written in .bat/QBasic.
DocFrac is a free open source converter betweeen RTF, HTML and text. Windows, Linux, ActiveX and DLL platforms available. It will probably be pretty easy to wrap it up in python.
RTF::TEXT::Converter - Perl extension for converting RTF into text. (in case You have problems withg DocFrac).
Official Rich Text Format (RTF) Specifications, version 1.7, by Microsoft.
Good luck (with the limited privileges in Your working environment).
If you are on Mac , you can convert an RTF file file.rtf to TXT from the CLI like:
textutil -convert txt file.rtf
Have you checked out pyrtf-ng?
Update: The parsing functionality is available if you do a Subversion checkout, but I'm not sure how full-featured it is. (Look in the rtfng.parser.base module.)
Here's a link to a script that converts rtf to text using regex:
Regular Expression for extracting text from an RTF string
Also, and updated link on github:
Github link
There is good library pyrtf-ng for all-purpose RTF handling.
PyRTF-ng 0.9.1 has not parsed any of my RTF documents, both with the ParsingException.
First document was generated with OpenOffice 3.4, the second one with Mac TextEdit.
Pyth 0.5.6 parsed without problems both documents, but has not processed cyrillic symbols properly.
But each editor opens other's editor document correctly and without trouble, so all libraries seems to have a weak rtf support.
So I'm writing my own parser with with blackjack and hookers.
(I've uploaded both files, so you can check RTF libraries by yourself: http://yadi.sk/d/RMHawVdSD8O9 http://yadi.sk/d/RmUaSe5tD8OD)
I just came across pyrtflib - there's not much (any) documentation on it, it's kinda a case of installing it and then using the inbuilt help() function to find out what's available and what everything does.
Having said that in my little trial run of its rtf.Rtf2Html.getHtml() function it went well enough. I haven't tried the Rtf2Txt function but given the simpler nature of converting rtf to plaintext it should do fine I'd expect.
I ran into the same thing ans I was trying to code it myself. It's not that easy but here is what I had when I decided to go for a commandline app. Its ruby but you can adapt to python very easily.
There is some header garbage to clean up, but you can see more or less the idea.
f = File.open('r.rtf','r')
b=0
p=false
str = ''
begin
while (char = f.readchar)
if char.chr=='{'
b+=1
next
end
if char.chr=='}'
b-=1
next
end
if char.chr=='\\'
p=true
next
end
if p==true && (char.chr==' ' or char.chr=='\n' or char.chr=='\t' or char.chr=='\r')
p=false
next
end
if p==true && (char.chr=='\'')
#this is the source of my headaches. you need to read the code page from the header and encode this.
p=false
str << '#'
next
end
next if b>2
next if p
str << char.chr
end
rescue EOFError
end
f.close
Conversely, if you want to write RTFs easily from Python, you can use the third-party module rtflib. It's a fairly new and incomplete module but still very powerful and useful. Below is an example that writes "hello world" in rich text to an RTF called helloworld.rtf. This is a very primitive example, and the module can also be used to add colors, italics, tables, and many other aspects of rich text to RTF files.
from rtflib import *
file = RTF("helloworld.rtf")
file.startfile()
file.addstrict()
file.addtext("hello world")
file.writeout()