The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   Replacing a text in applescript (http://hintsforums.macworld.com/showthread.php?t=50904)

tk. 01-31-2006 08:01 AM

hi,

I am writing the following script:
tell application "BBEdit"
set new_file to "/Users/admin/Desktop/test.conf"
open new_file
??//replace "line x" by "line y"//??
end tell

As you can see, I want to be able to "find and replace" a specific text with another....how can i do this?

thx

hayne 01-31-2006 08:05 AM

The crux of this is how to identify the line that is to be changed. You need to be able to tell BBBedit how to identify this line - e.g. by some text string that is contained in that line, by the line number, etc.

Carl Stawicki 01-31-2006 08:33 AM

You can do this without a text editor (unless you need the editor for other things) by using AppleScript's text item delimiters.

Code:

set new_file to alias "Users:admin:Desktop:test.conf"
set AppleScript's text item delimiters to "The original text you want to replace."
set text_items_of_new_file to text items of (read new_file) --A list that contains 2 chunks of text: Everything before the original line, and everything after
set AppleScript's text item delimiters to "The NEW line of text."
set NEW_new_file to text_items_of_new_file as Unicode text --New text for your new conf file

Now you have a string variable with your new text that can be saved to a file with your method of choice.

tk. 01-31-2006 09:23 AM

thx for the reply guys...however Carl i tried ur script but it seems that i am receiving a syntax error saying: File Users:admin:Desktop:test.conf wasn't found.

Carl Stawicki 01-31-2006 10:00 AM

I just used the path in your first post as an example. Replace what's there with the location of the actual original .conf file.

tk. 01-31-2006 10:01 AM

But it is a correct location...the file is in the Desktop...and i am still receiving this error...

tk. 01-31-2006 10:15 AM

One more thing....when i am removing the "alias" i am receiving an Apple "Script Error saying that: Can't make "Users:admin:Desktop:testing" into a file"

Carl Stawicki 01-31-2006 11:01 AM

Don't remove the alias.

Try using

set new_file to alias (((path to desktop) & "test.conf") as Unicode text)

EDIT: I forgot to mention in my original suggested script that you need to reset your delimiters back to the default:

set AppleScript's text item delimiters to ""

mark hunte 01-31-2006 02:02 PM

Quote:

Originally Posted by tk.
thx for the reply guys...however Carl i tried ur script but it seems that i am receiving a syntax error saying: File Users:admin:Desktop:test.conf wasn't found.

You need to add the full path
replace "Macintosh HD" with your hard disk name

"Macintosh HD:Users:admin:Desktop:test.conf"

tk. 02-01-2006 01:39 AM

It worked guys!!!
one last thing....do u know how i could save this in the original file..??

:D!!thx

tk. 02-01-2006 02:29 AM

I am trying to do the following but it is not working....

Check the last line plz (save....):
set new_file to alias (("RDServer HD:Users:admin:Desktop:testing.txt") as Unicode text)
set AppleScript's text item delimiters to ""
set AppleScript's text item delimiters to "good"
set text_items_of_new_file to text items of (read new_file) --A list that contains 2 chunks of text: Everything before the original line, and everything after
set AppleScript's text item delimiters to "nice"
set NEW_new_file to text_items_of_new_file as Unicode text --New text for your new conf file
save NEW_new_file in alias (("RDServer HD:Users:admin:Desktop:testing.txt") as Unicode text)

tk. 02-02-2006 08:49 AM

Refreshing Applescript Text Delimiters
 
I come up to do this script for now.....and its working fine....but the problem:confused: is that i can only do it once and i cannot edit the text anymore....how can i refresh the applescript delimiters...and start again..!!

Heres the script:
set new_file to alias (("RDServer HD:Users:admin:Desktop:Tita copy.txt") as Unicode text)
set AppleScript's text item delimiters to ""
set AppleScript's text item delimiters to "Mono"
set text_items_of_new_file to text items of (read new_file) --A list that contains 2 chunks of text: Everything before the original line, and everything after
set AppleScript's text item delimiters to "tatou"
set NEW_new_file to text_items_of_new_file as Unicode text --New text for your new conf file
display dialog the NEW_new_file
write NEW_new_file to alias (("RDServer HD:Users:admin:desktop:Tita copy.txt") as Unicode text)

thx!!! :)

Carl Stawicki 02-02-2006 09:55 AM

Sorry, I meant to reply but forgot :(

You can use a dialog to ask the user to enter the text each time the script is ran.

Also, the line 'set AppleScript's text item delimiters to ""' should be the last line of the script.

Hal Itosis 02-02-2006 04:56 PM

When posting code containing the character pair :D
it would probably be a good thing to check the box
labeled "Disable smilies in text" from the Additional
Options below the Reply to Thread editing section.

:D

tk. 02-03-2006 07:02 AM

The refresh did not work..:(..One last thing guys instead of putting /Users/admin/public..."admin" beeing the shortname of the current users...i want to be able to find the shortname in the script and replace it automatically...i dont want to do it manually...this is the aim of the whole script...so if you can help in that plz!!!

:D

tk. 02-22-2006 07:03 AM

guys i want to thank you for the help...but i need a last favor....

How can i get the name of my Hard Disk...using applescript!!!
Please its an urgent matter...!!
thx!
:)

NovaScotian 02-22-2006 09:28 AM

tell application "Finder" to set HD to name of startup disk

guardian34 02-22-2006 09:34 AM

Or…

set HD to text 1 thru -2 of (path to startup disk as Unicode text)

NovaScotian 02-22-2006 09:45 AM

Quote:

Originally Posted by guardian34
Or…

set HD to text 1 thru -2 of (path to startup disk as Unicode text)

Which is certainly faster than telling the Finder anything.

tk. 02-22-2006 10:59 AM

Thx man!! it worked but it seems i have some problems with permissions on folder. do u know how can we change a folder permissions??
all i know about this subject is this script:
tell application "Finder"
make new folder at desktop with properties {name:"aNewFolder", owner privileges:{read write}}
end tell

tk. 03-01-2006 08:24 AM

need help urgently
 
hi guys...i really need an answer on the last post reply i worte....it is a matter of life and death with my boss!!!!:eek: !!!!!

tk. 03-01-2006 08:27 AM

need help urgently
 
hi guys...i really need an answer on the last post reply i worte....it is a matter of life and death with my boss!!!!:eek: !!!!!
this was it:

Thx man!! it worked but it seems i have some problems with permissions on folder. do u know how can we change a folder permissions??
all i know about this subject is this script:
tell application "Finder"
make new folder at desktop with properties {name:"aNewFolder", owner privileges:{read write}}
end tell

Hal Itosis 03-01-2006 08:51 AM

do shell script "mkdir -p -m 700 ~/Desktop/newdir"

Uh, you never said what permissions you wanted.
700 is secure. 777 is insecure. Tweak as desired.

-HI-

tk. 03-01-2006 09:04 AM

i want read write permission on an already created folder...not a new one,,,!!!

tk.

Hal Itosis 03-01-2006 02:28 PM

read write ... but no "execute"!?!?!?
read write for who besides owner (group and/everyone)???

You are not at all clear. What's wrong with the permissions?
When a user creates a folder on their desktop, they already
get read write and execute (search) permissions.

Study some UNIX (like man chmod). Anyway, this should do:
do shell script "chmod 777 ~/Desktop/foldername"

tk. 03-02-2006 02:36 AM

i really need some urgent help overhere guys....
i need to change the permission of an already present file....
someone mentioned a do shell script....
tk.

tk. 03-02-2006 02:40 AM

thx guys....
i dont know but it seems i had some rfreshing problem....i couldnt see the new things u wrote....
anyways thx again....it WORKED....m SAVED!! :D!!

tk. 03-06-2006 11:11 AM

Hi,

I am writing the following script which consist of editing a file automatically using applescript...my problem is that i can only perform the modification ONCE!!

set hd to path to desktop
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set hdname to first text item of (hd as string)
set AppleScript's text item delimiters to astid
set httpd_old to alias ((hdname & ":Testfolder:test.txt") as Unicode text)
set AppleScript's text item delimiters to ""
set AppleScript's text item delimiters to "a path to edit"
set text_items_of_httpd_old to text items of (read httpd_old) --A list that contains 2 chunks of text: Everything before the original line, and everything after
set publFolder to (path to "pubb")
set uPath to POSIX path of publFolder
set u2Path to uPath & theName
set AppleScript's text item delimiters to u2Path
set httpd_new to text_items_of_httpd_old as Unicode text --New text for your new conf file
write httpd_new to alias ((hdname & ":Testfolder:test.txt") as Unicode text)


When i run the script for the first time....the path changes.....but if i need to do it and edit something else....it keep giving me the same result....

can u help me with it please....if there is anything unclear about the script let me know...!!
tk.

tk. 03-07-2006 10:01 AM

need helpp!!!
 
anyone availble guys

tk. 03-07-2006 10:03 AM

need helpp!!!
 
anyone available guys


All times are GMT -5. The time now is 06:04 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.