AppleScript favor to ask

Discussion in 'Mac Programming' started by prostuff1, Aug 17, 2009.

  1. prostuff1 macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #1
    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.
     
  2. Wowzera macrumors 6502a

    Joined:
    Oct 14, 2008
    Location:
    Brazil
    #2
    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
     
  3. prostuff1 thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #3
    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.
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    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
     
  5. prostuff1 thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #5
    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
     
  6. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #6
    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
     
  7. prostuff1 thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #7
    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
     
  8. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #8
    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
     
  9. prostuff1 thread starter macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #9
    This one works perfectly and does everything that I needed it to.

    Thanks you mysterytramp for all the work that you did.
     

Share This Page