• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: PBXOutput=ReplaceSelection question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: PBXOutput=ReplaceSelection question


  • Subject: Re: PBXOutput=ReplaceSelection question
  • From: James Bucanek <email@hidden>
  • Date: Mon, 10 Apr 2006 08:09:19 -0700

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.

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}

James Bucanek
____________________________________________________________________
Author of Beginning Xcode                           ISBN: 047175479X
<http://www.beginningxcode.com/>           Available April 3rd, 2006
 _______________________________________________
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

  • Follow-Ups:
    • Re: PBXOutput=ReplaceSelection question [SOLVED]
      • From: Cem Karan <email@hidden>
References: 
 >PBXOutput=ReplaceSelection question (From: Cem Karan <email@hidden>)

  • Prev by Date: Re: Best approach to adding a Spotlight importer
  • Next by Date: Re: PBXOutput=ReplaceSelection question [SOLVED]
  • Previous by thread: PBXOutput=ReplaceSelection question
  • Next by thread: Re: PBXOutput=ReplaceSelection question [SOLVED]
  • Index(es):
    • Date
    • Thread