Remove the trailing slash from a folder

Hi everyone,

My company has several remote sites that rely on documentation I write here at WHQ in order to deploy macs according to company standards. Since these documents are in constant update mode, I wrote a small droplet that I use to just drop the documentation folder on and copy it to the remote site servers overwriting any existing documentation folder.

I have the following piece of code:


if CHIError is not equal to "" then
display alert CHIError giving up after 5
else
repeat with anItem in droppedItems
if ((kind of (info for anItem)) = "Folder") then
set anItem to quoted form of POSIX path of anItem
do shell script "cp -rf " & anItem & space & "/Volumes/MacPackaging$/MacDocumentation/"
else
set anItem to quoted form of POSIX path of anItem
do shell script "cp -f " & anItem & space & "/Volumes/MacPackaging$/MacDocumentation/"
end if
end repeat
end if


The problem I am running into is that when anItem is a folder and converted to a POSIX path, it ends up with a trailing slash, causing cp -r to copy only the contents of the folder and not the folder itself... Is there a way to remove the trailing slash or another conversion method I can use to copy the entire folder?

If I take out the if statement to check for a folder and the -r flag on cp, cp errors out telling me that anItem is a directory and can't be copied, which is to be expected. Ah... how I wish bash could do droplets...

Posted on Dec 8, 2010 5:13 AM

Reply
5 replies

Dec 8, 2010 8:16 AM in response to Pierre L.

Though probably not needed, better to test for the slash first:

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #E6E6EE;
overflow: auto;"
title="this text can be pasted into the AppleScript Editor">
if text -1 of anItem is "/" then
set anItem to quoted form of text 1 through -2 of POSIX path of anItem
else
set anItem to quoted form of POSIX path of anItem
end if
</pre>

Dec 8, 2010 10:10 AM in response to Tony T1

Though probably not needed, better to test for the slash first:


As long as there are no changes in the AppleScript language itself (nor in the standard additions), POSIX path of an alias (as is the case here) should always return a trailing slash (for a folder). On the other hand, any “if statement” inside a repeat loop will slow down the script, although most often not noticeably. For those two reasons, and also because I like to make my scripts as short as possible, I would not test for the slash. Just my opinion of course.

Message was edited by: Pierre L.

Dec 8, 2010 10:14 AM in response to Andrew Caldwell

By the way, since the “info for” command is now deprecated, you could as well write your repeat block as follows:

*repeat with anItem in droppedItems*
* if last character of (anItem as text) is ":" then*
* set anItem to quoted form of text 1 through -2 of POSIX path of anItem*
* do shell script "cp -rf " & anItem & space & "/Volumes/MacPackaging$/MacDocumentation/"*
else
* set anItem to quoted form of POSIX path of anItem*
* do shell script "cp -f " & anItem & space & "/Volumes/MacPackaging$/MacDocumentation/"*
* end if*
*end repeat*



On the other hand, if you would like to use pure AppleScript, you could also replace the whole repeat block with the following single line of code:

*tell application "Finder" to duplicate droppedItems to POSIX file "/Volumes/MacPackaging$/MacDocumentation/" with replacing*

Dec 8, 2010 10:31 AM in response to Pierre L.

Pierre L. wrote:
Though probably not needed, better to test for the slash first:


As long as there are no changes in the AppleScript language itself (nor in the standard additions), POSIX path of an alias (as is the case here) should always return a trailing slash (for a folder). On the other hand, any “if statement” inside a repeat loop will slow down the script, although most


Good point and as I suspected, probably not needed 🙂

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.

Remove the trailing slash from a folder

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