need help!!

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.

Hi,

I don’t see where you set the variable theName.

Edited: Oh I see. It’s in the “a path to edit”. Is it the name of the file or the whole unix path name?

Edit #2: I guess it would just be the name, because you’re changing the location of where to look to the public folder right?

gl,

Your script is pretty confusing. What exactly are you trying to do? Editing a text file is pretty easy and can be accomplished with just a few lines of code. I don’t see the need for all the variables and changing of the text item delimiters. Can you be more specific about exactly what your script is supposed to be doing?

WF

Hi tk,

I think I would set it all up at the beginning, so you can work with text item delimiters all at one time (search and replace). Here I used the Documents folder and switched it to a Documents folder in the Public folder.

set theName to “/Library/Webserver/Documents/”
set itemReference to (theName as POSIX file) as alias
set itemName to name of (info for itemReference)
set publFolder to (path to “pubb”) as string
set uPath to POSIX path of publFolder
set u2Path to (uPath & itemName)
set hd_path to (path to startup disk) as string
set httpd_old to (hd_path & “Testfolder:test.txt”) as alias
set t to read httpd_old
set def_tid to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to {theName}
set temp_list to text items of t
set AppleScript’s text item delimiters to u2Path
set httpd_new to temp_list as string
set AppleScript’s text item delimiters to def_tid
set ref_number to (open for access httpd_old with write permission)
try
set eof ref_number to 0
write httpd_new to ref_number
close access ref_number
on error
close access ref_number
beep 2
return
end try

Note that setting the eof to 0 overwrites the file.

gl,

hi kel,

Regarding what you wrote, i am still not able to change the httpd.conf twice or more…
what is happening is if i change the U2Path for example instead of writing:

set u2Path to (uPath & itemName)       //First time value, First time run script/

I write:

set u2Path to (uPath & "Documents")  //Second time value for a second time run script/not working!!

…in the file httpd.conf, it is not refreshing it always keep putting the first value of the u2Path i used when i run the script!!

:confused:

Hi tk,

I don’t know if this is what you mean. There’s to strings:

“/Library/Webserver/Documents/”

and u2Path:

“/Users/UserName/Public/Documents/”

The first time it switches the first with the second. The second time it doesn’t find “/Library/Webserver/Documents/”, so it doesn’t do anything. If you want to switch it back you need to switch these two around. It might be easier to understand if you made a Search/Replace subroutine.

– this is the search string
set theName to “/Library/Webserver/Documents/”
– get item name “Documents”
set itemReference to (theName as POSIX file) as alias
set itemName to name of (info for itemReference)
– create the replace string
set publFolder to (path to “pubb”) as string
set uPath to POSIX path of publFolder
set u2Path to (uPath & itemName)
– get path to the file to read
set hd_path to (path to startup disk) as string
set httpd_old to (hd_path & “Testfolder:test.txt”) as alias
– read the file
set t to read httpd_old
– search and replace the text
set httpd_new to ReplaceText(theName, u2Path, t)
– overwrite new text to file
set ref_number to (open for access httpd_old with write permission)
try
set eof ref_number to 0
write httpd_new to ref_number
close access ref_number
on error
close access ref_number
beep 2
return
end try

– needs error checking
on ReplaceText(s, r, t)
set def_tid to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to {s}
set temp_list to text items of t
set AppleScript’s text item delimiters to {r}
set new_text to temp_list as string
set AppleScript’s text item delimiters to def_tid
return new_text
end ReplaceText

You might want to toggle it or search the text for existence of the string. Maybe use the ‘offset’ command. The offset will be 0 if the string is not found.

Hope this is whatyou meant. So you need to switch the two strings around the second time.

gl,