Re: PBXOutput=ReplaceSelection question [SOLVED]
Re: PBXOutput=ReplaceSelection question [SOLVED]
- Subject: Re: PBXOutput=ReplaceSelection question [SOLVED]
- From: Cem Karan <email@hidden>
- Date: Mon, 10 Apr 2006 11:44:14 -0400
On Apr 10, 2006, at 11:09 AM, James Bucanek wrote:
Cem Karan wrote on Monday, April 10, 2006:
I've got a simple python user script that I'm having trouble getting
to behave exactly as I want. Here is the script:
---------------------------- Start ------------------
<clip>
---------------------------- End --------------------
The problem is that when I replace the text, I always end up with an
extra newline at the end. I can't tell if this is because XCode is
throwing a newline in when it shouldn't, or if its a script problem.
When I try to remove the last character from the wrapped string
before printing, I end up missing characters rather than remove the
newline, so I'm leaning towards XCode doing something funky, but I'd
like either confirmation, or a note saying how to fix this before I
file a bug.
I can't tell you anything about your script, because I don't know
Python. However I will comment on a couple of things and I can
confirm that I've written many different scripts that replace
selections without any problem or extranious characters.
First, your script uses both
# %%%{PBXInput=Selection}%%%
and
stringToWrap = """%%%{PBXSelectedText}%%%"""
The first places the current selection on stdin and the second
replaced the %%%{PBXSelectedText}%%% with the text of the current
selection in your script. It appears that your script is using the
second, but not the first. If you don't really use the contents of
stdin then set %%%{PBXInput=None}%%%.
Having said that, %%%{PBXSelectedText}%%% is really, *REALLY*, hard
to use in scripts. It replaces the macro with the exact text found
in your selection. The problem is that most scripting languages
have a syntax ('duh!') which includes quoting, escape characters,
etc. If my selected text was
Quoth (""") the rave\n never 'or
How will Python interpret this?
stringToWrap = """Quoth (""") the rave\n never 'or"""
It is typically much more reliable to use %%%{PBXInput=Selection}%%
% and read the text of the selection from stdid.
So THAT is how its supposed to be used! I misunderstood and thought
that we had to use both for selected text; and you're right, Python,
like more or less every other language, is greedy in its matches. It
would have see the internal """ rather than the outer one. I've
changed my script to match your suggestion.
Once you've written your script this way, you can verify it by
piping data to it and looking at the output in detail:
echo -n "text test case" | python myscript | hexdump -C
As a debugging step, often capture stdin to a temporary file. This
lets me read the data multiple times and I can peek at it
afterwards to see exactly what data Xcode piped to my script.
selfile="$(mktemp /tmp/XcodeSelection.XXXXXX)"
cat - > ${selfile}
...
#rm ${selfile}
Bingo. Turns out that Python (at least, the version XCode is using,
which may not be what it shipped with b/c I installed a newer
version) has a bug in it. The last statement in my script "print
outputString," should print the line without adding a newline to the
end, but it turns out that it IS adding in the newline regardless.
The fix (for any python people out there) is to do "sys.stdout.write
(outputString)".
Thanks for your help!
Thanks,
Cem Karan
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden