• 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: Editing a text file using Applescript and Text Edit or Text Wrangler
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Editing a text file using Applescript and Text Edit or Text Wrangler


  • Subject: Re: Editing a text file using Applescript and Text Edit or Text Wrangler
  • From: hd <email@hidden>
  • Date: Thu, 29 Oct 2009 18:28:23 +0000

Hi

For textwrangler, assuming that you want to save in the front window:

tell application "TextWrangler" to close window 1 with saving

or just

tell application "TextWrangler" to save window 1




The following will prompt you to create a text file. It will then open it for access, write some text into it, close access, open for access again, write some more text to the end, then close access


set nu_file to (choose file name)
set the_file to (open for access nu_file with write permission)
write "Hello world" to the_file
close access the_file
set the_file to (open for access nu_file with write permission)
write return & "Farewell cruel world" to the_file starting at eof
close access the_file

This uses AppleScript's Standard Additions commands, and does not require any application opening and closing windows. To find out about it (and any application's scripting dictionary, in Script Editor (or AppleScript Editor in Snow Leopard) go to the Edit Menu, choose Open Dictionary, and browse down to StandardAdditions.osax

Hope this helps.

H






This script will create a new text file at a location chosen by yourself and write some text to it.
On 27 Oct, 2009, at 18:34, Derek Nugent wrote:


That sounds great but the file needs to grow it's an XML file that get's bigger with every entry (or at least that's my idea)

The bit I'm stuck at is getting TextWrangler to save : (

I do think however that you may have solved my problem with the first line containing "write permission"
could you give me an example of how you tell Applescript to open a "fileName" with write permission in Applescript syntax?



(my guess is that it's not too different from what I just typed in : ) )


I'm pretty excited now!
Many many thanks
Derek Nugent


email: email@hidden email@hidden web: http://www.dereknugent.com



----- Original Message ----
From: hd <email@hidden>
To: email@hidden
Sent: Tue, October 27, 2009 1:18:43 PM
Subject: Re: Editing a text file using Applescript and Text Edit or Text Wrangler


You can do it using AppleScript without using an external editor.

There are several ways. Here's one (this is pseudocode, not an AppleScript).

open for access the file with write permission
read the file, write contents to a variable
append a return character and then your new text to the text variable
clear the file
write the full text variable into the file
close access the file

If you want to use an external editor, TextWrangler and Tex-Edit Plus are both highly scriptable. TextEdit… well.

There are lots of scripts for Tex-Edit Plus at http://dougscripts.com/texedit/

Rather than positioning the insertion point, the approach is to tell the application to make a new paragraph at the end of the front document, and then set that paragraph to the text you want to append.

Hope this gets you started.

On 27 Oct, 2009, at 17:46, Derek Nugent wrote:

Is it possible to edit and save a text file using Applescript and Text Edit or Text Wrangler...

I'm trying to add a line of text to the end of a plain text document
but I'm having trouble moving the insertion point to the end of the
document and then saving the document.

Any progress would be good right now...

Many thanks in advance


Best regards Derek Nugent


email: email@hidden email@hidden web: http://www.dereknugent.com




_______________________________________________ Do not post admin requests to the list. They will be ignored. Automator-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Automator-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden





_______________________________________________ Do not post admin requests to the list. They will be ignored. Automator-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Filter Paragraphs (From: Gavin Stone <email@hidden>)
 >Re: Filter Paragraphs (From: Ben Waldie <email@hidden>)
 >Editing a text file using Applescript and Text Edit or Text Wrangler (From: Derek Nugent <email@hidden>)
 >Re: Editing a text file using Applescript and Text Edit or Text Wrangler (From: hd <email@hidden>)

  • Prev by Date: Re: Filter Paragraphs
  • Next by Date: Applescript 123
  • Previous by thread: Re: Editing a text file using Applescript and Text Edit or Text Wrangler
  • Next by thread: Re: Filter Paragraphs
  • Index(es):
    • Date
    • Thread