Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Denarius

macrumors 6502a
Original poster
Feb 5, 2008
690
0
Gironde, France
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

Code:
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.
 
Example command-lines:
Code:
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.
 
Example command-lines:
Code:
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.
 
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.

Code:
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
 
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:
Code:
echo ~www ~mysql
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.