Help: do shell script "ls " & quoted form of POSIX path

The following code works great:

choose folder
set x to do shell script "ls " & quoted form of POSIX path of result
set y to paragraphs of result
--> My folder selection returns {"Accounts", "CD:DVD Folder", "Invoices"}

The problem I have is if a file/folder name contains a “/” (such as “CD/DVD Folder”) the “/” is replaced with a colon.
Example: “CD/DVD Folder” becomes “CD:DVD Folder”

I can find/replace “:” to “/” with vanilla Applescript but I was wondering if there’s a way to find/replace with a “do shell script” statement?

Thanks.

Although I’m not able to help you with the code, the shell function you want is called sed.

This should do it

choose folder
set x to paragraphs of (do shell script "ls " & quoted form of POSIX path of result & " | sed 's~:~/~g'")

That’s the expected behavior; POSIX paths are separted by slashes, so any slashes in a name are replaced by a colon. If you intend to use that path with other command line tools (or if you will change it to a file reference with POSIX file), then you should leave it as is.

Thanks James.

Bruce: The directory list is not needed for a file/folder reference. I need to know the name of each item of a chosen folder, sorted alphabetically or by modification date. I didn’t know how to get a list sorted by modification date with vanilla Applescript. Thanks.

I thought you would have a reason; I just wanted to point that out. :slight_smile:

One more example:

choose folder

do shell script "/bin/ls " & quoted form of POSIX path of result & " | /usr/bin/tr ':' '/'"
set something to paragraphs of result

Forgot about tr - translate characters. Easier than sed in this instance.

Also, to get a list of file names sorted by modification date, this plain vanilla applescript does it:


tell application "Finder"
	try
		set FEC to files of entire contents of (choose folder) as alias list
	on error -- only one file in it
		set FEC to files of entire contents of (choose folder) as alias as list
	end try
end tell

set N to {}
set MD to {}
repeat with F in FEC
	set N's end to name of (info for F)
	set MD's end to modification date of (info for F)
end repeat

set {MD, N} to sort2lists(MD, N)
N

to sort2lists(sortList, SecondList) -- sorts on the first, keeps the second in sync.
	tell (count sortList) to repeat with i from (it - 1) to 1 by -1
		set s to sortList's item i
		set r to SecondList's item i
		repeat with i from (i + 1) to it
			tell sortList's item i to if s > it then
				set sortList's item (i - 1) to it
				set SecondList's item (i - 1) to SecondList's item i
			else
				set sortList's item (i - 1) to s
				set SecondList's item (i - 1) to r
				exit repeat
			end if
		end repeat
		if it is i and s > sortList's end then
			set sortList's item it to s
			set SecondList's item it to r
		end if
	end repeat
	return {sortList, SecondList}
end sort2lists

This forum is great! Thanks for all the help.

That seemed to sort the last Modified file at the bottom.

Just adding the -t option to the other script, sorts by Modification date, last Modified at the top.

choose folder

do shell script "/bin/ls -t " & quoted form of POSIX path of result & " | /usr/bin/tr ':' '/'"
set something to paragraphs of result

:slight_smile:

FWIW, the Finder’s sort command is available in Mac OS X 10.4:

choose folder
set sourceFolder to result

tell application "Finder"
	set nameList to {}
	sort (get items of sourceFolder) by modification date
	
	repeat with thisItem in result
		set end of nameList to name of thisItem
	end repeat
end tell

nameList