Applescript: renaming files in a system folder without having to enter admin password

Discussion in 'Mac Programming' started by Compton, Sep 26, 2011.

  1. Compton macrumors member

    Joined:
    Sep 8, 2010
    #1
    I back up my macbook on two different drives almost daily, one at my work place and one at home. I would like to automate this using applescript and I already found the code that does almost exactly what I want. The code is

    property pth : "Macintosh HD:Library:preferences:"
    property d1name : "Backup Disk 1"
    property d1 : "com.apple.TimeMachine.Disk1.plist"
    property d2name : "Backup Disk 2"
    property d2 : "com.apple.TimeMachine.Disk2.plist"
    property active : "com.apple.TimeMachine.plist"

    do shell script "defaults write com.apple.TimeMachine AutoBackup -bool false"

    if (list disks) contains d1name then
    tell application "Finder" to if exists (pth & d1) then
    set name of file (pth & active) to d2
    set name of file (pth & d1) to active
    end if
    else if (list disks) contains d2name then
    tell application "Finder" to if exists (pth & d2) then
    set name of file (pth & active) to d1
    set name of file (pth & d2) to active
    end if
    end if

    do shell script "defaults write com.apple.TimeMachine AutoBackup -bool true"


    The only problem is that Finder asks for my administrator password twice during the script to rename the plist files in the system folder. I tried changing the "sharing and permissions" option for these particular files through the "Get info" dialog to to enable read/write but this didn't appear to have any effect.

    How should the script be modified so that it would rename the plist files but not ask for system password? Thanks in advance.
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Renaming is not an operation on a file, but an operation on the folder that contains a name to the file. You could try changing the permissions of the /Library/Preferences folder, but I find that unsettling.
     
  3. Compton, Sep 26, 2011
    Last edited: Sep 27, 2011

    Compton thread starter macrumors member

    Joined:
    Sep 8, 2010
    #3
    Changing the permissions of the entire Preferences folder did work, thanks for the tip! I also find this solution a bit unsettling so I would be interested in hearing other options. I have read something about enabling read/write access for specific files through shell scripts. Perhaps someone could provide me with the appropriate shell script if this is the case.

    Edit: deleted an unnecessary question.
     
  4. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #4
    As it's hierarchical you'd need write permissions to the folder before the file.

    An alternative is to run the script as a launch daemon so it runs as root.
     
  5. Compton thread starter macrumors member

    Joined:
    Sep 8, 2010
    #5
    Could you provide an example? I'm not familiar with shell scripts or launch daemons so it would take me a while to figure out what you're saying.
     
  6. Compton thread starter macrumors member

    Joined:
    Sep 8, 2010
    #7
  7. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007

Share This Page