I’m trying to write an AppleScript Studio app that will utilize Cron to run its internal functions at particular times, but I’m still trying to figure out the simplest way to use AppleScript to edit /etc/crontab…
I’ll need to add certain preset lines to /etc/crontab through AppleScript, and later find and remove these lines when the user requests them… i.e, they don’t need to be custom, the exact text can be coded in to the AppleScript file itself.
Thanks for any suggestions, and sorry if this is a major newbie question… I’m quite new to the combination of AppleScript and Cocoa.
If inserting/removing lines does not need to be dynamic (you will have only two different files which we’ll call “A” and “B”), you can simply bundle them within your own app and move them when needed.
Copy them to your “Resources” folder and locate them when needed using:
set resPath to resource path of main bundle
set A to resPath & "/A"
--> backup if you wish existing file
do shell script "cp -f /etc/crontab " & quoted form of (resPath & "/B")
--> substitute old file with A file (add authentication if needed)
do shell script "cp -f " & (quoted form of A) & " /etc/crontab"
So, you need add and remove your own inserted lines (whatever is previosly in the file). This can help, but needs some customization (eg, if you need the lines inserted in a specific place instead of at the end of the file):
property myLines : {"abc", "def"}
property fileToModify : alias "path:to:file.txt"
(*
OR:
property fileToModify : missing value
if fileToModify is missing value then set fileToModify to "/etc/crontab" as posix file
*)
to addMyLines()
set oldContents to read fileToModify
repeat with i in myLines
if (offset of i in oldContents) is 0 then --> insert line at end
if oldContents's item -1 is (ASCII character 10) then
set addOn to ""
else
set addOn to (ASCII character 10)
end if
set oldContents to oldContents & addOn & i & (ASCII character 10)
end if
end repeat
writeData(fileToModify, oldContents)
end addMyLines
to removeMyLines()
set oldContents to read fileToModify
repeat with i in myLines
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to (i & (ASCII character 10)) --> assume line ends with LF
set oldContents to oldContents's text items
set AppleScript's text item delimiters to oldDelim
set oldContents to oldContents as text
end repeat
writeData(fileToModify, oldContents)
end removeMyLines
to writeData(f, c) --> f = file; c = contents
set fRef to (open for access f with write permission)
set eof of fRef to 0
write c to fRef
close access fRef
end writeData
As you most probably will need auth to modify “/etc/crontab”, you can use this write-data routine instead:
to writeData(f, c) --> f = file; c = contents
try
set fRef to (open for access f with write permission)
set eof of fRef to 0
write c to fRef
close access fRef
on error msg number n
if n is -5000 then --> no write permission
writeData("/tmp/tmp" as POSIX file, c) --> write thing to tmp file
do shell script "mv -f /tmp/tmp /etc/crontab" with administrator privileges --> prompt for authentication, then move tmp file to final loc
end if
end try
end writeData