Thanks for reading this. I’m trying to append text to the /private/etc/cups/cupsd.conf file. The permissions are the file are as follows:
-rw-r–r-- 1 root _lp 2614 Sep 28 2007 cupsd.conf
This is the code that makes the changes to the file:
on addPrinter into pathToConfigurationFile from printerConfigurationText
-- Add a line break before and after you insert the text so the additional text is seperated from the previous text
set printerConfigurationText to ("\n" & printerConfigurationText & "\n")
--Add the printer text to the end of the file:
try
set writeReference to (open for access file pathToConfigurationFile with write permission)
write printerConfigurationText to writeReference starting at eof
close access writeReference
on error errorMessage
display alert "ERROR MESSAGE WHEN TRYING TO write to file CLEAN THIS UP LATER"
end try
end
addPrinter
When I run the script it coughs up an error and quits. Even though I am running this under an account with administrative privileges I need to elevate the script’s privileges before it can make the change to the file. My question is how do I do this with applescript? I know I can use the do shell script with administrative privileges command but I’m curious as to how I can do this by telling the finder/system events.
you can do this only with a temporary file.
The script reads cupsd.conf, appends and writes the text into a temp file, moves the file to the proper place replacing the existing cupsd.conf
and (very important!) adjusts owner (root), group (_lp) and access privileges (644).
An admin password is required
I haven’t tested the script. Please make a copy of cupsd.conf before running it
property pw : "¢¢¢¢¢" -- admin password
on addPrinter into pathToConfigurationFile from printerConfigurationText
set cupsdTemp to ((path to temporary items as text) & "cupsd.conf")
set theData to read POSIX file pathToConfigurationFile
try
set writeReference to open for access file cupsdTemp with write permission
write (theData & linefeed & printerConfigurationText & linefeed) to writeReference starting at eof
close access writeReference
do shell script "/bin/mv " & quoted form of POSIX path of cupsdTemp & space & pathToConfigurationFile password pw with administrator privileges
do shell script "/usr/sbin/chown root:_lp " & pathToConfigurationFile password pw with administrator privileges
do shell script "/bin/chmod 644 " & pathToConfigurationFile password pw with administrator privileges
on error errorMessage
display alert "ERROR MESSAGE WHEN TRYING TO write to file CLEAN THIS UP LATER"
try
close access file cupsdTemp
end try
end try
end addPrinter