PDA

View Full Version : Sparsebundle Applescript problem




Denarius
Sep 24, 2010, 07:41 AM
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.



chown33
Sep 24, 2010, 11:13 AM
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.

Denarius
Sep 24, 2010, 12:14 PM
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.

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

Denarius
Sep 24, 2010, 12:34 PM
Hi chown, thanks for the reply, I really meant in terms of Applescript handlers to return the file info as an Applescript list item.

Sorry, just figured out what you were saying. Nice one, thanks.

Denarius
Sep 24, 2010, 01:43 PM
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)
try
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

chown33
Sep 24, 2010, 02:09 PM
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.

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

Denarius
Sep 24, 2010, 02:30 PM
For example, enter this command:
echo ~www ~mysql


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