Re: Setting the correct linefeed.
Re: Setting the correct linefeed.
- Subject: Re: Setting the correct linefeed.
- From: Christopher Nebel <email@hidden>
- Date: Tue, 5 Feb 2008 12:18:04 -0800
On Feb 5, 2008, at 11:59 AM, Arnold Nefkens wrote: Im currently building a AppleScript Studio application that creates a series of set folders and creates 2 shell scripts . The problem is that the shell scripts do not get set with the correct linefeed. They are set to the Mac <CR> setting instead of the , correct in my case, setting.
Below the basis of the shell script creation:
set fileRef to (open for access alias scriptinstall with write permission) write "#!/bin/sh" & return to fileRef as «class utf8» starting at eof ...
Simple solution: use the "linefeed" constant (technically, it's a property of the AppleScript object, but never mind that) instead of "return". I think it was added in Tiger, so if you're using something earlier than that, then you can make your own:
set linefeed to (ASCII character 10)
Alternatively, you can use "\n" in strings, which means a linefeed, as in C. (There's also "\r" for CR, but of course you don't want that.) This can get a little dicey without the right options, however, since it will substitute a literal linefeed for the "\n" when you compile -- it's kind of hard to tell exactly what you've got at that point. There's a preference in Leopard's Script Editor to make it not do that and leave the "\n" as "\n". I've followed the second example and created the following routine: set txt to " #!/bin/sh" & return & ... set tid to AppleScript's text item delimiters set AppleScript's text item delimiters to ASCII character 10 -- (a line feed) set newTxt to text items of txt -- not text of, text items of set AppleScript's text item delimiters to tid -- whatever they were before - ALWAYS SET THEM BACK!
set fileRef to (open for access alias scriptinstall with write permission) write newTxt to fileRef starting at eof close access fileRef
You seem to have been groping toward a search-and-replace routine, but didn't quite get there. The basic technique is to exploit the "text item delimiters" -- given a string, you can turn it into a list of strings split on a certain search string, and then glom them all back together with a different string in between; the net effect is a search-and-replace of a particular literal over the entire string. Your routine got halfway there; here's the complete version:
set txt to " #!/bin/sh" & return & ... set tid to AppleScript's text item delimiters -- save the current delimiters set AppleScript's text item delimiters to ASCII character 13 -- search for this (a return) set textItems to text items of txt -- textItems is a list of strings, without any returns. set AppleScript's text item delimiters to ASCII character 10 -- change to this (a linefeed) set newTxt to (textItems as text) -- change the list back to a single string. set AppleScript's text item delimiters to tid -- whatever they were before - ALWAYS SET THEM BACK!
--Chris Nebel AppleScript Engineering
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden