Getting text items 1 thru x-1 and keeping the delimiters

Is there a quick way to do this without having to do a repeat?

I have a file path:

Macintosh HD:Users:steve:Desktop:tempFolder:AA-0644035

I want to drop the last item in the path (AA-0644035), retaining the other items (Macintosh HD:Users:steve:Desktop:tempFolder:)

I think what you want is the container of your file:

set F to alias "Macintosh HD:Users:steve:Desktop:tempFolder:AA-0644035"
tell application "Finder" to set C to (container of F) as alias

Edit: See Nigel’s post below.

Or extract them intact:

set tempFile to "Macintosh HD:Users:steve:Desktop:tempFolder:AA-0644035"

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set tempFolder to (text 1 thru text item -2 of tempFile) & ":"
set AppleScript's text item delimiters to ASTID

tempFolder
--> "Macintosh HD:Users:steve:Desktop:tempFolder:"

I knew that. :rolleyes:

you can also tell System Events to do the job

set F to alias "Macintosh HD:Users:steve:Desktop:tempFolder:AA-0644035"
tell application "System Events" to set C to path of (get container of F)

I think I’ve caught something from Nigel Garvey. :confused:

I just tested the three methods proposed above: using the Finder, using System Events, and using text item delimiters as Nigel did; running each 1000 trials starting with the same alias to a file and the results are: delay 5

time(Finder)/time(TID) = 5.5; and time(Sys Ev)/time(TID) = 11.2. It doesn’t surprise me that tid’s are fastest, but it does surprise me that the Finder (in this case) is twice as fast as Sys Events.

If you care:

lotsa(1000)

on lotsa(many)
	set F to alias "PutAliasToFileInsideFoldersHere"
	-- Dummy loop to absorb a small observed
	-- time handicap in the first repeat.
	repeat many times
	end repeat
	
	-- Test 1.
	set t to GetMilliSec
	repeat many times
		tell application "Finder" to set C to (container of F) as alias
	end repeat
	set t1 to ((GetMilliSec) - t) / many
	
	-- Test 2.
	set t to GetMilliSec
	repeat many times
		set ASTID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {":"}
		set tempFolder to (text 1 thru text item -2 of (F as text)) & ":"
		set AppleScript's text item delimiters to ASTID
	end repeat
	set t2 to ((GetMilliSec) - t) / many
	
	-- Test 3
	set t to GetMilliSec
	repeat many times
		tell application "System Events" to set C to path of (get container of F)
	end repeat
	set t3 to ((GetMilliSec) - t) / many
	
	-- Timings.
	return {t1, t2, t3, t1 / t2, t3 / t2, t3 / t1}
end lotsa

Oh. How awful! :o

It seems to be due to that ‘get’ in the System Events code. If you take it out, the code still works (OMM) but is twice as fast “ same speed as the Finder, in fact.

PS. For experimental rigour, the three tests should all produce the same result, but it doesn’t seem to affect the timings significantly.

Interesting with “get”. With the exception of (get selection) sometimes, I never use it. After that correction the Finder only beats Sys Ev by 18%. I don’t consider it “awful” that I’ve become much more interested in speed. I suppose it arises from finally learning that there are a lot of ways to accomplish the same things. In the beginning, one is satisfied with “it works”. Later, one gets interested in “it’s fast”.

As Nigel discovered some time ago, sometimes the shell is a big win - his example was for building directory trees using mkdir. I've been exploring the use of the shell's tools for extracting file metadata, as a means of filtering that can be much speedier than using the Finder with a filter. I'll post more on that when I've figured it out myself.