how to remove the last / from POSIX paths?

Howdy all, its me again!

I’m here messing with a POSIX path to a folder, and I want to use said folder in a shell script. The catch is that if it has the final slash (/) that POSIX path’s contain, the command won’t run properly.

So what I have is something along the lines of


set var1 to POSIX path of (choose folder with prompt "Please choose your folder")
do shell script "--don't remember this ATM"

the thing is that the POSIX path to var1 will return /users/kevinferguson/desktop/myfolder/ when I need /users/kevinferguson/desktop/myfolder (notice the lack of the final “/”)

Is there a command I can use to get rid of said slash?

Thanks!
=Parrot

Something like this?

choose folder with prompt "Please choose your folder"
set thePath to text 1 thru -2 of (POSIX path of (result))

That does work, thanks :slight_smile:

One question I have is that if it is possible to only return the container of my selection.

I.E-


set var1 to POSIX path of (choose folder with prompt "Choose your folder")

Is there something I’d throw in there to get the POSIX path of the container of the folder I select, instead of the folder itself? (I.E- if ‘stuff’ is inside of a folder ‘my stuff’ which is on the desktop, so the path I’d get is /users/kevinferguson/desktop/my stuff/stuff , when I want /users/kevinferguson/desktop/my stuff/ (notice the / is back :P)

what I tried was something like


set var1 to (choose folder with prompt "Please choose your ISO folder")
		set var2 to (container of var1)

It returns an error saying that “Can’t get container of alias “Kevins Powerbook:users:kevinferguson:desktop:test folder:”.(-1728)” right after selecting the folder.

I believe the error number means that it can’t make data into the requested type. What I can’t figure out is that those commands have worked before, I don’t understand why they aren’t working now.

Hi Parrothead3,

You’re probably thinking of what System Events or the Finder can do, something along the lines of this:

set chosenFolder to (choose folder with prompt "Please choose your ISO folder")
tell application "System Events" to set containerFolder to POSIX path of (container of chosenFolder)

This will also remove the last slash, simply because System Events likes to do it that way (I imagine it has its own ‘POSIX path of’), although I actually think it’s a bug.

Here’s another possibility:

choose folder with prompt "Choose your folder:"
set thePath to POSIX path of result

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set theContainer to text 1 thru text item -3 of thePath & "/"
set AppleScript's text item delimiters to ASTID

return theContainer

And Qwerty’s solution cascades for a file in nested several deep

set f to choose file
tell application "System Events" to set folderOuter to POSIX path of (container of container of container of f)

[ASIDE] You can get root volume name and user’s short name using delimiters this way too (should you be so inclined and already be in a Finder block)


set text item delimiters to ":"
tell application "Finder" to set d to {RootVol:item 1, UsrNm:item 3} of text items of ((container of desktop) as alias as string)
set text item delimiters to ""

Alternative aside:

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set {rootVolume, username} to {text item 1, text item 3} of (path to home folder as Unicode text)
set AppleScript's text item delimiters to ASTID

return {rootVolume, username}