I would like (love!) to get to solution for this one:
set this_file to choose file
-- Result looks like: "Mac OS X:Users:myusername:Desktop:Somefolder:thefile sometime-with-spaces.xml"
But i do not need the filename (in this example thefile sometime-with-spaces.xml) in only need the
“Mac OS X:Users:myusername:Desktop:somefolder:” part.
I do not want to use the “choose folder” because then users need to choose twice…
choose file without invisibles
set theFile to result as Unicode text
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set theFolder to (text items 1 thru -2 of theFile) & "" as Unicode text
set AppleScript's text item delimiters to ASTID
return theFolder
choose file without invisibles
set theFile to result
tell application "Finder" to set theFolder to container of theFile as Unicode text
And what’s interesting too is that Nigel’s modified form of Bruce’s script is just over 15 times faster than invoking System Events to get the container in my test (as follows):
choose file without invisibles
set theFile to result as Unicode text
set t1 to GetMilliSec
repeat 100 times
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set theFolder to (text 1 thru text item -2 of theFile) & ":" as Unicode text
set AppleScript's text item delimiters to ASTID
end repeat
set t2 to GetMilliSec
(t2 - t1) / 100
vs:
choose file without invisibles
set theFile to result
set t1 to GetMilliSec
repeat 100 times
tell application "System Events" to set theFolder to path of container of theFile as Unicode text
end repeat
set t2 to GetMilliSec
(t2 - t1) / 100
Apart from the ‘choose file’ command, which is in the StandardAdditions OSAX, Bruce’s script is pure, vanilla AppleScript and the action’s a simple text manipulation that’s handled by AppleScript itself. System Events and the Finder, on the other hand, are both external applications, so control has to be passed to and fro between them and the script. They also have to do a little work to recognise commands and analyse references that arrive. I don’t know how they ascertain the container of a file, but I suspect it’s not just by manipulating text…
Not at all Nigel. Actually, I recall figuring that one out not too long ago. I haven’t been feeling well lately, so it must have slipped my mind. Regardless, thanks for the input.
I suppose I should also have noted that if there’s any danger that ‘theFile’ might refer to a package, an allowance will have to be made for the colon at the end:
choose file without invisibles
set theFile to result as Unicode text
if (theFile ends with ":") then
set c to -3
else
set c to -2
end if
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set theFolder to text 1 thru text item c of theFile & ":"
set AppleScript's text item delimiters to ASTID
return theFolder
I’m sorry to hear that. I hope you’re soon recovered.