quoted form of the POSIX path & drag n drop

Hi. I have the following situation: A part of a script-application that copies files over the network checks if there is actually enough disc space left on the target volume using the df and du commands.

This is how I do it:

on open (dragged_items)
my handle Files(draggeditems)
end open

on handle Files(theseitems)

set the item_sizecheck to the quoted form of the POSIX path of these_items
do shell script ("du -sk " & item_sizecheck & " | awk '{print $1}'")
set x to the result

set destination to "/Volumes/basti"
do shell script ("df -k " & destination & " | xargs echo | awk '{print $11}'")
set y to the result

if x ≤ y then
display dialog "space there"
end if

if x ≥ y then
display dialog "no space"
end if
end handle_Files

Of course in the orininal script instead of displaying a dialog that there is enough space the script will simply copy the files.

The problem now is that I can't get the "quoted form of the POSIX path" for many items at the same time (let's say I want to copy 3 things simultaniously).
So I think I have to cycle through the items (set this_item to item i of these_items),but then I have a problem:

I think checking the filesize/discsize is easiest done by the simple equasion (x larger than y),but if i do this for many objekts i have to use a unknown number of variables.
example:

x1 would be the first of the 3 items I draged onto my application,x2 is the second and x3 the third (...)
Then I would have to add up the filesizes of all these items using du,and then compare it to the available disc space ((x1 x2x3) greater than y).

How do I do this?

iMac G3, Mac OS X (10.3.9), 500 Mhz, 768 Mb Ram, 18Mb VRAM, 120 GB HD

Posted on May 21, 2007 11:48 PM

Reply
9 replies

May 22, 2007 1:33 AM in response to mju:sick

Probably the best way would be to repeat through each item.

on open these_items
set unix_paths to {}
repeat with this_item in these_items
set end of unix_paths to (quoted form of POSIX path of this_item)
end repeat
set utid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {space}
set text_listing to unix_paths as string
set AppleScript's text item delimiters to utid
set t to do shell script "du -sk " & text_listing
display dialog t
end open

My -s doesn't work so I didn't go further. Note that you can use if then else instead of two if thens.

if b then
do something
else
do something else
end if

gl,

May 22, 2007 11:06 AM in response to mju:sick

The problem with your original script is in:

<pre class=command>set the item_sizecheck to the quoted form of the POSIX path of these_items
do shell script ("du -sk " & item_sizecheck & " | awk '{print $1}'")
...</pre>

In this case, 'these_items' is a list. You can't get a POSIX path of a list, only of files/aliases.

So you need to iterate through the items in the list:

<pre class=command>repeat with each_item in these_items
-- commands to run for each item in the list
end repeat</pre>

Once you have that you can run each_item through the du command. You can also eschew the xargs and awk statements and just:

<pre class=command>set fileSize to word 1 of (do shell script "du -sk " & quoted form of POSIX path of each_item</pre>

This will return the number of KB (the 'word 1' part just grabs the first word, which is the file size in KB).

So, now all you need to do is add those numbers as you iterate through the loop.

First start with a variable set to 0 and add the others as you go:

<pre class=command>on handle_files (these_items)
set total_size to 0 -- start off with 0

repeat with each_item in these_items -- iterate through the list
set this filesize to word 1 of (do shell script "du -sk " & quoted form of POSIX path of each_item) -- get the file size
set total_size to total_size + this filesize -- add to the running total
end repeat

display dialog "Total size needed = " & total_size & "KB" -- there ya go!
end handle_files</pre>

From here I'm guessing you can take care of comparing that to the available space on your destination drive.

May 22, 2007 11:16 AM in response to mju:sick

>For what do I need the Applescript Text delimiters,what do they do?

Text Item Delimiters are used by AppleScript anytime to convert a list to/from a text object.

Imagine you have a list myList containing the strings {"A", "B", "C", "D"}

If you ask AppleScript to:

<pre class=command>set myString to myList as text</pre>

You get:

<pre class=command>ABCD</pre>

However, if you set the TIDs to a space first, AppleScript inserts that between each item in the list:

<pre class=command>set my text item delimiters to space
set myString to myList as text</pre>

Now you get:

<pre class=command>A B C D</pre>

You can use any text string you like - it'll be inserted between each item in the list as it's coerced (or, if you're going from a string to a list, the list items will be delimited by the TIDs).

It's relevant in this example because you're trying to pass multiple file paths to a shell command. The shell requires each argument to be delimited by a space. If you didn't set the TIDs you'd end up with a shell command that looks something like:

<pre class=command>du sk /path/to/file1/path/to/file2/path/to/file3</pre>

which the shell will interpret as a single path. By using a space for your TID you get:

<pre class=command>du sk /path/to/file1 /path/to/file2 /path/to/file3</pre>

which works (assuming the paths are valid, of course).

The one gotcha with setting TIDs is that they're persistent. If you change them at one point in your script, they stay until they're changed again. This often causes confusion when debugging scripts because the TIDs are set somewhere else in the script that you're not currently focussing on. So you should always get in the habit of storing the current TID, making your coercion, then resetting the TIDs to the previous value afterwards - that way you always know you're safe:

<pre class=command>set oldTIDs to my text item delimiters -- save the current TIDs
set my text item delimiters to space -- set the TIDs to what you want
set myString to myList as text -- make your coercion
set my text item delimiters to oldTIDS -- then restore the previous value</pre>

May 22, 2007 3:04 PM in response to mju:sick

Hi,

Nice lesson by Camelot on the tids.

When I use du with two files with the c option:

du -ck /Installer\ Log\ File /Office\ X\ 10.1.2\ Update\ Log

I get a grand total in the last line:

4 /Installer Log File
8 /Office X 10.1.2 Update Log
12 total

At first I thought it was a bug with du and the s option in my system:

-s Display only the grand total for the specified files.

and I wasn't getting just the grand total with -s, but I guess not according to Camelot's post. So all you need to do is get the grand total from the last line with the c option.

I'll go back and reread this thread. Just got up.

Edited: btw, note that opening a new shell multiple times for multiple files with 'do shell script' really slows down a script. That's why I didn't use a repeat loop on each file with 'do shell script'.

Later,

May 22, 2007 3:09 PM in response to kel

>When I use du with two files with the c option:

Right, but the main issue is in how to get your list of aliases into something that can be passed to the shell. There are a couple of options, either build a list of posix paths and run one du command, or run multiple du's, one per file, adding up the sizes as you go (which was my approach).

May 22, 2007 3:32 PM in response to Camelot

Here's a modofication using du and the c option:

on open these_items
set unix_paths to {}
repeat with this_item in these_items
set end of unix_paths to (quoted form of POSIX path of this_item)
end repeat
set utid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {space}
set text_listing to unix_paths as string
set AppleScript's text item delimiters to utid
set t to do shell script "du -ck " & text_listing
set s to first word of last paragraph of t
display dialog "The grand total is: " & s
end open

Note that setting AppleScirpt's text item delimiters and coercions are quick and one should avoid multiple 'do shell script' statements if possible.

Camelot, thanks for verifying the bug with the -s option.

gl,

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

quoted form of the POSIX path & drag n drop

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.