Permission Problem

tell application "Finder"
	set the_file to name of startup disk & ":private:var:spool:anacron:cron.monthly"
end tell

open for access alias the_file with write permission
set file_date to read file the_file using delimiter return

-- do some stuff here to change the file content

write file_date to file the_file starting at 1
close access file the_file

I get permission error while opening the file. I know that I do not have permission for that file. But, how can I run this script as root user? I couldn’t use “with administrator privileges” in the open for access statement, got syntax error. How can I make this script work?

I also tried to create this script as an application and put it into “Macintosh HD/Library/StartupItems/” folder to make it run on startup, but it did not run.

Browser: Safari 416.13
Operating System: Mac OS X (10.4)

Hello Cuneyt Ocaklilar,

Any chance that you can run something similar to this on the file?

tell application "Finder"
	set owner privileges of file "file" to read write
	set group privileges of file "file"  to  read write
	set everyones privileges of file "file" to read write
end tell

jON bEEBE

Okay:

  1. Please, people, stop using shell commands that /aren’t/complete/paths. E.g., /bin/chmod. This is especially important when invoking administrator privileges.

  2. This seems like a weird workaround for the problem. What edits do you want to do on the file? Chances are, there’s a way to script it with a short shell shot + administrator privileges that doesn’t require changing the file’s permissions.

It would be useful to know what the OP wants to do to the file before recommending altering its permissions.

tell application "Finder"
   set the_file to name of startup disk & ":private:var:spool:anacron:cron.monthly"
end tell

open for access alias the_file with write permission
set file_date to read file the_file using delimiter return

-- do some stuff here to change the file content ***

write file_date to file the_file starting at 1
close access file the_file

*** file has a date in yyyymmdd format. I do some date calculations in the script and get a new date. All I want to do is to replace the old date with this one in the file. So, I need write permissions for that file.

As a different workaround the script can read the file, make the date calculations and pass that value to a shell command to write into the file if it’s possible without asking permission.

I also, couldn’t find a UNIX command to write a value into a file replacing the old contents. If there were one I would like to know.

hi co,

how about the “>” operator? as in:

shell calculations > /file/i/want/to/overwrite

that will replace the old contents with your new contents. if you want to just add on to the file, try “>>”.

My script was intended to run periodicaly in background, so I didn’t want to add a “with administrator privileges” in it, because I didn’t want it to ask password while running. I also, didn’t want to give a fixed password in the script, it may cause problems if I change the administrator password in future or if this script runs in another system.

I created a monthly.local executable shell script file in /private/etc/ folder, to make it run every month just after the Anacron run the periodic monthly. I changed the owner and permissions of the file to “root” to make it run as root and solve the permission problem. But, I couldn’t. Although the script was running as root, same permission problem arised.

Then, I decided to change the permissions of the private:var:spool:anacron:cron.monthly file. The script run without any problem. But, I noticed that the permissions; which I changed, reset to their previous state after repairing permission.

At a last workaround, I added a chmod (without sudo) command just before the open command in the monthly.local executable shell script file. It works fine now.