I was wondering if I had a variable from a choose folder command with a value of
Macintosh HD:Users:johnsmith:Desktop
Is it possible to change it to the directory before it (ie johnsmith) without having to use a regulare expression?
Are you looking for something like this?
choose folder
set theFolder 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 -3 of theFolder) & "" as Unicode text
set AppleScript's text item delimiters to ASTID
return theFolder
You could also do this:
choose folder
tell application "Finder" to set theFolder to (container of (result)) as Unicode text
You can also use the ‘container’ property of a Finder item.
set f to choose folder
tell application “Finder”
try
set parent_of_f to container of f as alias
on error – no parent, f is startup disk
beep 2
return
end try
end tell
gl,
Avoid the Finder when you can:
tell application "System Events"
set parentContainer to container of childObject
end tell
Also:
set parentContainerAsPOSIX to (do shell script "/usr/bin/dirname" & space & quoted form of childObjectAsPOSIX)
Sprinkle in “as Unicode text” wherever you find it appropriate.
Note that this doesn’t work in OSX pre_Panther:
tell application “System Events”
set parentContainer to container of childObject
end tell
Getting properties from the Finder doesn’t take that much time.
gl,
This is more important than it might seem at first. If you have a number of scripts running continuously at any one time and you are also using the Finder manually things can quickly become bogged down and you’ll be plagued by timeouts and beach balls. I’ve found that using shell scripts to replace all Finder calls makes things run much more smoothly. It’s not that Finder calls are particularly slow, it’s just that the Finder is normally busy.
AppleScript’s text item delimiters are probably the fastest way.
and then
but I find that a shell script like this:
set theFldr to choose folder with prompt "Select a folder to index:"
set allFiles to (do shell script "ls -R " & quoted form of POSIX path of theFldr)
is waaay faster than any Finder equivalent (though maybe I’ve chosen very poor ones) for indexing a folder full of folders.
Maybe the best thing to do is learn C. Try to avoid AppleScript awhenever possible.
The thing about using the Finder is that it’s a good way to learn AppleScript.