Edit/update config file with Applescript??

am trying to read the file and then trying to change the values of the same with permissions, pls suggest how to write the same file with permissions.

The below script gives me the values as shown below, i need to change those values like aaaaa to 1, pls suggest.


set fileContents to {}
set x to ""
set filepath to POSIX path of "Macintosh HD:Library:Application Support:Macromedia:abc.cfg"
try
    open for access filepath
    set fileContents to (read filepath)
    close access the filepath
on error
    return false
end try
fileContents
output :
"aaaaa=0
bbbbbbb=0
"

Mac OS X (10.7.4)

Posted on Dec 13, 2012 6:28 AM

Reply
7 replies

Dec 13, 2012 10:21 AM in response to madhusudhanjr

If you want to write to the file you need to open it with write permissions


open for accessfilepath with write permission


Ideally you should store the result of that command as a reference to the opened file:


set myFile to open for accessfilepath with write permission

now you can read the data:


set fileData to readmyFile


You can erase the file by setting the EOF to 0:


set eofmyFileto 0


(otherwise writing to the file appends data, rather thatn overwriting it)


You can write data to the file:


write "blah blah blah" to myFile

and close it when you're done:


close accessmyFile

Dec 14, 2012 4:11 AM in response to Camelot

Hi,

thanks for the reply.. but it not working..


i tried as... its not writting to the file..!!!


set fileContents to {}
set myFile to {}
set filepath to POSIX path of "/Users/rajasekaranr/Desktop/mms.cfg"
try
    #open for access filepath with write permission
    set myFile to open for access filepath with write permission
    #set fileContents to (read filepath)
    set fileData to read myFile
    set eof myFile to 0
    write "blah blah blah" to myFile
    close access myFile
end try

Dec 14, 2012 10:24 AM in response to madhusudhanjr

The script works pefectly for me, as written (with an amended path, of course).


The only error I could trigger was if the file didn't exist, since you can't read its contents, but as long as there's a file there, that script replaces its contents with 'blah blah blah'.


Maybe a little debugging is in order:



set myFile to open for accessfilepath with write permission

set fileData to readmyFile

display dialog "Current data: " & fileData

set eofmyFileto 0

write "blah blah blah blah blah blah" to myFile

close accessmyFile

display dialog "New data: " & (read filepath)


this will show two dialogs - the first with the original data in the file, the second with the data after the script has (presumably) updated the file.

Dec 17, 2012 2:59 AM in response to Camelot

Hi thanks for the reply, its wroking fine... but stuck with some prob here..when i used the above script to edit file placed in diff folder, its working fine, but if my filename is mms.cfg its saying file is already opened, i tried the beolw script.. its giving error msg as shown below. pls suggest to overcome this issue



set fileContents to {}
set myFile to {}
set filepath to POSIX path of "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"
try
    set myFile to open for access filepath with write permission
       on error errstr number errNum

    if errNum = -49 then

        close access filepath

        set myFile to open for access filepath with write permission
    end if
    set fileData to read myFile

    display dialog "Current data: " & fileData

    set eof myFile to 0

    write "blah blah blah blah blah blah" to myFile

    close access myFile

    display dialog "New data: " & (read filepath)
end try


error :


error "File file Macintosh HD:Library:Application Support:Macromedia:mms.cfg is already open." number -49 from file "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"

Dec 19, 2012 5:04 AM in response to Camelot

Hi,

Finally the script below was working fine, but sometimes am getting the below error, pls guide me.



set filepath to POSIX path of "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"
try
    tell application "System Events"
        if file filepath exists then
            set myFile to open for access file filepath with write permission
            set fileData to read myFile
            set eof myFile to 0
            write "blah blah" to myFile
            close access myFile
        else
            return "File Not Found"
        end if
    end tell
on error
    return false
end try


Error:


"Network file permission error." number -5000 from file "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Edit/update config file with Applescript??

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.