• 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: saving front window using osascript...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: saving front window using osascript...


  • Subject: Re: saving front window using osascript...
  • From: Fons Rademakers <email@hidden>
  • Date: Tue, 20 Nov 2007 16:10:42 +0100
  • Keywords: CERN SpamKiller Note: -50 Charset: west-latin
  • Organization: CERN

Hi Rick,

  sorry to come back to this issue. I've the script working:

#!/bin/sh
#
# Removes trailing blanks (spaces and tabs) and save to file.

osascript &lt;&lt; ENDOFSCRIPT
tell application "Xcode"

set myFile to associated file name of front window
set textDocuments to text documents
repeat with i in textDocuments
if path of i is myFile then
set orgText to text of i
set text of i to do shell script "echo " & quoted form of orgText & " | tr '\r' '\n' | perl -pe 's/[\t ]+$//g'"
save i
end if
end repeat
end tell
ENDOFSCRIPT


exit 0

tr first converts from CR to LF line endings, then perl strips off all trailing tabs and blanks from each source line and then the text is put back in the editor. However, the "set text of text document" does convert all \n to \r even strings like "\n", which is not a binary \n.

So running the script on:

void a()
{
   printf("bla\n");
}

sets the text back in the editor like:

void a()
{
   printf("bla
");
}

How can I prevent this behaviour?

Many thanks for any help.

Cheers, Fons.



Rick Ballard wrote:
On Nov 9, 2007, at 3:16 AM, Fons Rademakers wrote:
#!/bin/sh
#
# Removes trailing blanks (spaces and tabs) from the file.
#

osascript << ENDOFSCRIPT
tell application "Xcode"
set myFile to associated file name of front window
save the front document
do shell script "perl -pe 's/[\t ]+$//g' " & quoted form of myFile & " > ~/.xc-save.$$; mv ~/.xc-save.$$ " & quoted form of myFile
end tell
ENDOFSCRIPT


but this leaves my editor window empty since no buffer gets written back in. But as soon as something gets written back in the buffer is modified, hence not saved. Is there a catch-22 here?

Hi Fons,

I'm not completely sure I understand what you mean. Xcode won't notice that you've overwritten the file on disk right away, so maybe that's what you're seeing. Switching applications will trigger a refresh of the file on disk, as will closing and re-opening it.

You don't need to replace the file on disk in order to modify its contents, however; you can manipulate the text of the document directly via applescript. If you browse Xcode's scripting dictionary in script editor and look at the Text Document class, you should see what you need. Specificially, you can "set text of front text document to" whatever you want.

- Rick

-- Org: CERN, European Laboratory for Particle Physics. Mail: 1211 Geneve 23, Switzerland E-Mail: email@hidden Phone: +41 22 7679248 WWW: http://fons.rademakers.org Fax: +41 22 7669640 _______________________________________________ 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: saving front window using osascript...
      • From: Philip Aker <email@hidden>
References: 
 >saving front window using osascript... (From: Fons Rademakers <email@hidden>)
 >Re: saving front window using osascript... (From: Rick Ballard <email@hidden>)
 >Re: saving front window using osascript... (From: Fons Rademakers <email@hidden>)
 >Re: saving front window using osascript... (From: Rick Ballard <email@hidden>)
 >Re: saving front window using osascript... (From: Fons Rademakers <email@hidden>)
 >Re: saving front window using osascript... (From: Rick Ballard <email@hidden>)

  • Prev by Date: Xcode 3 and sample wchar_t data formatter
  • Next by Date: Re: Integer division
  • Previous by thread: Re: saving front window using osascript...
  • Next by thread: Re: saving front window using osascript...
  • Index(es):
    • Date
    • Thread