1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Sparsebundle Applescript problem

Discussion in 'Mac Programming' started by Denarius, Sep 24, 2010.

  1. macrumors 6502a


    Hi there,

    I've been trying to find the answers to a couple of questions on the web without much luck and was wondering if anyone might be able to help.

    First question, how do you query the properties of a file/folder etc. using either its POSIX path or alias in Applescript?

    The second question is, how do I check if a sparsebundle exists. I tried

    if exists POSIX file [posix path to my sparsebundle]
    but I think it's falling over because it's not a file. Trouble is I can't figure out how to find out what type it actually is, hence question 1!

    Thanks for your help.
  2. macrumors 603

    Example command-lines:
    osascript -e 'info for Posix file "/etc"'
    osascript -e 'info for Posix file "/Users/Shared"'
    If the file doesn't exist, then 'info for' fails. Catch that error.
  3. macrumors 6502a


    Hi chown, thanks for the reply, I really meant in terms of Applescript handlers to return the file info as an Applescript list item.
  4. macrumors 6502a


    Sorry, just figured out what you were saying. Nice one, thanks.
  5. macrumors 6502a


    Didn't need anything that I was asking questions about, turned out that I didn't realise that you couldn't have ~/ in quotes in a shell command. If anyone's interested, I was making a script to automatically create a time machine sparsebundle of a specified size on a specified volume. Feel free to pick holes.

    on run
    	--Section for requesting the size of the sparsebundle and checking that the answer
    	set bundleSize to ""
    	repeat while class of bundleSize is text
    		display dialog "Specify the sparsebundle size in GB:" default answer ""
    		set bundleSize to (text returned of result)
    			set bundleSize to bundleSize as number
    		end try
    	end repeat
    	--Section for choosing where the user wishes the sparsebundle to be located
    	set destinationPathAlias to (choose folder with prompt "Select the destination time machine backup volume")
    	set destinationPath to (the POSIX path of destinationPathAlias)
    	--Finds the name of the computer for the sparsebundle name
    	set hostName to (do shell script "hostname -s")
    	--Finds the MAC address of en0 for the sparsebundle name
    	set macAddress to (do shell script "ifconfig en0 | awk '$1==\"ether\" {print $2}' | sed 's/://g'")
    	set fileName to hostName & "_" & macAddress
    	set sparseBundleName to hostName & "_" & macAddress & ".sparsebundle"
    	--creates the sparsebundle creation command
    	set bundleCreationCommand to "hdiutil create -size " & bundleSize & "g -type SPARSEBUNDLE -fs HFS+J -volname \"" & hostName & " Backup\" ~/" & sparseBundleName
    	--Executes the sparsebundle creation command
    	do shell script bundleCreationCommand
    	do shell script "mv ~/" & quoted form of sparseBundleName & " " & quoted form of destinationPath
    end run
  6. macrumors 603

    Since ~/ is the same as $HOME/, and $ works inside quotes, you can simply use "$HOME/" instead of "~/".

    The main advantage of ~/ is brevity in keyboard entry. It's also useful for admins who refer to the home folders of other users. For example, enter this command:
    echo ~www ~mysql
  7. macrumors 6502a


    Oh wow, I never realised that the tilde could extend to other users. Another little gem of knowledge gained! Cheers.

Share This Page