I’m a little under the wire here, but to start things out I did perform a quick search on the forums to find a thread that would be a little helpful. I unfortunately was unable to find anything terribly relevant so I figured posting might actually yield some results.
The long of it:
So! I’m a CS major who’s still in school, whom has 0 experience with AppleScript. I have a script that was generated by a past employee that’s been a bit buggy. The script is essentially a simple little program that asks the user to input a flash drive, then asks them for their student ID. It then records a speech to the students H: drive as well as the flash drive. The error I’m running into now is a variable not being defined. I narrowed this down to a particular method not actually returning what it’s supposed to return, and narrowed it down further to a particular variable not being set to anything.
The short of it
I have a dialog box that returns the result of “drive” and “driveSpace”. What I can’t figure out is why driveSpace never actually receives any sort of information. When the dialogbox pops up, there is nothing where dialogbox should be, it just shows me the name of the drive.
on verifyDriveSpace(drive)
try
--returns in the form of 999[Mi, Gi]
set driveSpace to do shell script "df -h | grep " & drive & " | awk {'print $4'}"
--The below dialog box is for testing purposes to find out what driveSpace is set to.
tell application "Finder"
activate
with timeout of dialogTimeout seconds
display dialog "test" & drive & driveSpace buttons {"Okay","Quit"} with title {"Test Box"}
end timeout
end tell
end try
When I go into terminal and type
df-h | grep LEXAR MEDIA (the name of the drive) | awk {‘print $4’}
I get the drive space remaining, however when I type
df -h | grep " & LEXAR MEDIA & " | awk {‘print $4’}
I get no result.
Could that be the problem? When I remove the ampersands applescript won’t compile and if I remove the ampersands in addition to the quotes it doesn’t compile either.
the variable dialogTimeout in the handler is not defined
there could be another problem if the drive’s name contains a space character.
In the shell space characters are parameter separators
To avoid the problem quote the name of the drive
set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
The timeout block can simplified
tell application "Finder"
activate
display dialog "test" & drive & driveSpace buttons {"Okay", "Quit"} with title {"Test Box"} giving up after 5
end tell
This is just a method, dialogTimeout is a variable initialized way above to some inordinately long number.
The space in the drive name seems to be the culprit. Any drive that I insert that only has a 1 word name works just fine, and the one that I’ve been able to get consistent errors with has a space character. The name of the drive is quoted however as it is in the blurb I posted in my OP.
drive of course being the parameter passed into the method.
That makes sense but I don’t understand how I would pass the unquoted form and use the quoted. Too many uses of the words form and quote
If you could please just elaborate or break it down for a simpleton for me to understand it would be much appreciated.
Edit:
I’ve attempted a myriad of ways of quoting / unquoting / singlequoting / etc.
set currentDrive to "LEXAR MEDIA"
verifyDriveSpace(currentDrive)
on verifyDriveSpace(drive)
try
set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
on error e
display dialog e
end try
end verifyDriveSpace
There’s no point in the code where we would be able to put the flash drives name in quotes AFAIK. It generates a disk list for the user to choose which disk they would like to record to. Just to try out what you had posted however I came up with
try
set tempVar to "Lexar Media"
set driveSpace to do shell script "df -h | grep " & tempVar & " | awk {'print $4'}"
end try
Granted that’s without the fun error checking. I dunno, maybe I’m a dunce but I got nothin’.
Just a question.
If the passed name is the startup Volume one, I get an empty string as result.
Is it normal ?
path to startup disk
tell application "Finder"
set currentDrive to name of result
end tell
verifyDriveSpace(currentDrive)
on verifyDriveSpace(drive)
try
set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
on error e
display dialog e
end try
end verifyDriveSpace
tell current application
path to startup disk
--> alias "Macintosh HD:"
end tell
(*
tell application "Finder"
get name of alias "Macintosh HD:"
--> "Macintosh HD"
end tell
tell current application
do shell script "df -h | grep 'Macintosh HD' | awk {'print $4'}"
--> ""
end tell
*)
Yvan KOENIG (VALLAURIS, France) vendredi 30 mars 2012 20:45:48
I’m actually now getting “Variable driveSpace is not defined.” from
verifyDriveSpace(currentDrive)
on verifyDriveSpace(drive)
try
set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
on error e
display dialog e
end try
end verifyDriveSpace
The error handling yields
“Can’t make quoted form of {{“LEXAR MEDIA”}} into type Unicode text.”
Edit: Thinking about just making finder access the drive and return free space.
Thanks again Stefan for your help. I was eventually able to just:
tell application "Finder"
set driveSpaceInMB to (free space of disk drive) / 1.0E+6)
end tell
then check the resulting number and return the appropriate information. I bypassed almost twenty lines of code with that simple statement. I suppose that’s more subject to change than unix shells, but all of the issues I’ve had thus far with this script has been unix based.
path to startup disk
set currentDrive to POSIX path of result
verifyDriveSpace(currentDrive)
on verifyDriveSpace(drive)
try
--do shell script "df -h " & quoted form of drive
set driveSpace to do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}"
on error e
display dialog e
end try
end verifyDriveSpace
I get the result :
“309Gi
0Bi
0Bi
0Bi”
which seems curious because
do shell script “df -h ‘/’”
returns :
“Filesystem Size Used Avail Capacity Mounted on
/dev/disk0s2 931Gi 621Gi 309Gi 67% /”
More surprising (at least for me)
If I run the main script replacing $4 by $1, I get :
“/dev/disk0s2
devfs
map
map”
I really don’t guess where are the three extraneous values grabbed from.
Yvan KOENIG (VALLAURIS, France) vendredi 30 mars 2012 21:42:17
OK Stephan but it doesn’t explain why I get the four values with the complete script.
The log report is :
tell current application
path to startup disk
→ alias “Macintosh HD:”
do shell script “df -h | grep ‘/’ | awk {‘print $4’}”
→ “309Gi
0Bi
0Bi
0Bi”
end tell
It’s because I got these values that I tried to get the datas returned before applying grep.
It seems that, to get the value related to boot volume, the device identifier to pass is not slash but /dev/disk0s2
As I can’t guess this string, I edited the script this way :
path to startup disk
set currentDrive to POSIX path of result
--set currentDrive to "baseÆ’ebay"
verifyDriveSpace(currentDrive)
on verifyDriveSpace(drive)
try
set driveSpace to paragraph 1 of (do shell script "df -h | grep " & quoted form of drive & " | awk {'print $4'}")
on error e
display dialog e
end try
end verifyDriveSpace
This way the handler returns only the first value.