Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

prostuff1

macrumors 65816
Original poster
Jul 29, 2005
1,482
18
Don't step into the kawoosh...
I need a script that does the following to a file.

Code:
read in contents of file at /Applications/CrashPlan.app/Contents/Resources/Java/conf/ui.properties
if line containing "servicePort=4200" exists then remove it
else add line "servicePort=4200" to end of file

The contents of said file look like this originally:
Code:
#Fri Dec 09 09:50:22 CST 2005
#serviceHost=127.0.0.1
#servicePort=4243
#pollerPeriod=1000  # 1 second
#connectRetryDelay=10000  # 10 seconds
#connectRetryAttempts=3
#showWelcome=true

#font.small=
#font.default=
#font.title=
#font.message.header=
#font.message.body=
#font.tab=

If there are any other question please let me know.
 
Could I ask you what are you doing working with fonts? I'm developing some tools to change Snow Leopard font through a simple Applescript, so it's the reason I am collecting information everywhere :p
 
Could I ask you what are you doing working with fonts? I'm developing some tools to change Snow Leopard font through a simple Applescript, so it's the reason I am collecting information everywhere :p

Actually, I am not working with the fonts at all. Those just happen to be in the same file. that I am trying to edit.

The file I am trying to edit belongs to the application CrashPlan (obviously). It is very similar to Mozy in what it does. I installed it on my server (unRAID), which has no way of accessing the GUI for the app. unRAID management is all done through a web page and therefore I could not get to some of the stuff I needed to to set the app up. Well I did some redirecting of ports via SSH tunnels so that the CrashPlan gui would point to the CrashPlan Engine on the server. That way I can set it up and get it going.
 
How's this:

Code:
set theFile to choose file
try
	set fileRef to (open for access theFile with write permission)
on error errMsg number errNum
	display dialog ("Open for Access, Error Number: " & errNum as string) & return & errMsg
end try

set filesEOF to get eof fileRef

try
	set dataIn to read fileRef for filesEOF
on error errMsg number errNum
	display dialog ("Read, Error Number: " & errNum as string) & return & errMsg
end try
set whereItIs to offset of "servicePort=4200" in dataIn


if whereItIs = 0 then
	set dataOut to dataIn & "servicePort=4200" & return
	beep 3
else
	display dialog whereItIs
	copy characters 1 through (whereItIs - 1) of dataIn to dataOut
	set startPoint to whereItIs + 16 -- "servicePort=4200"&return is 17 characters less one is 16
	if startPoint < filesEOF then
		set dataOut to dataOut & (characters startPoint thru filesEOF of dataIn)
	end if
end if

set dataOut to dataOut as text

set eof of fileRef to 0

try
	write dataOut to fileRef
on error errMsg number errNum
	display dialog ("Write, Error Number: " & errNum as string) & return & errMsg
end try

set eof of fileRef to (length of dataOut)


try
	close access fileRef
on error errMsg number errNum
	display dialog ("Close, Error Number: " & errNum as string) & return & errMsg
end try

mt
 
How's this:

Code:
brilliant code

mt

This seems to work great and do what it is supposed to do. The only thing that I am noticing is that if the line exists and it is removed I get a little popup box with the number 281 in it. Not quite sure where this is coming from but I would love to have that say something like "servicePort=4200 Removed" and then the box disappear in like 2 seconds. And the same goes for when the line is added to the file. It would say something like "servicePort=4200 Added" and then the box disappears.

Thanks for everything so far
 
Here you go:

Code:
set theFile to choose file
try
	set fileRef to (open for access theFile with write permission)
on error errMsg number errNum
	display dialog ("Open for Access, Error Number: " & errNum as string) & return & errMsg
end try

set filesEOF to get eof fileRef

try
	set dataIn to read fileRef for filesEOF
on error errMsg number errNum
	display dialog ("Read, Error Number: " & errNum as string) & return & errMsg
end try
set whereItIs to offset of "servicePort=4200" in dataIn


if whereItIs = 0 then
	set dataOut to dataIn & "servicePort=4200" & return
		set msgToUser to "servicePort=4200 added"
else
	copy characters 1 through (whereItIs - 1) of dataIn to dataOut
	set startPoint to whereItIs + 16 -- "servicePort=4200"&return is 17 characters less one is 16
	if startPoint < filesEOF then
		set dataOut to dataOut & (characters startPoint thru filesEOF of dataIn)
		set msgToUser to "servicePort=4200 removed"
	end if
end if

set dataOut to dataOut as text

set eof of fileRef to 0

try
	write dataOut to fileRef
on error errMsg number errNum
	display dialog ("Write, Error Number: " & errNum as string) & return & errMsg
end try


set eof of fileRef to (length of dataOut)


try
	close access fileRef
on error errMsg number errNum
	display dialog ("Close, Error Number: " & errNum as string) & return & errMsg
end try

display dialog msgToUser

The "281" was the location of the string. It was a bit of error checking I should have removed before adding this.

This version will say whether the string is added or removed after the file is closed properly.

mt
 
OK, that makes sense. I assumed the number had something to do with the end of the file or the like.

Now with that said, I seem to be getting the "servicePort=4200 added" message all the time. It happens whether the line is added or removed.

Thanks
 
Code:
set msgToUser to ""

set theFile to choose file
try
	set fileRef to (open for access theFile with write permission)
on error errMsg number errNum
	display dialog ("Open for Access, Error Number: " & errNum as string) & return & errMsg
end try

set filesEOF to get eof fileRef

try
	set dataIn to read fileRef for filesEOF
on error errMsg number errNum
	display dialog ("Read, Error Number: " & errNum as string) & return & errMsg
end try
set whereItIs to offset of "servicePort=4200" in dataIn


if whereItIs = 0 then
	set dataOut to dataIn & "servicePort=4200" & return
	set msgToUser to "servicePort=4200 added"
else
	copy characters 1 through (whereItIs - 1) of dataIn to dataOut
	set msgToUser to "servicePort=4200 removed"
	set startPoint to whereItIs + 16 -- "servicePort=4200"&return is 17 characters less one is 16
	if startPoint < filesEOF then
		set dataOut to dataOut & (characters startPoint thru filesEOF of dataIn)
	end if
end if

set dataOut to dataOut as text

set eof of fileRef to 0

try
	write dataOut to fileRef
on error errMsg number errNum
	display dialog ("Write, Error Number: " & errNum as string) & return & errMsg
end try


set eof of fileRef to (length of dataOut)


try
	close access fileRef
on error errMsg number errNum
	display dialog ("Close, Error Number: " & errNum as string) & return & errMsg
end try

display dialog msgToUser
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.