Directory name and base name in Applescript

Alec Jacobson

July 29, 2009

weblog/

I wanted to extract the base directory of a URL which gave rise to the question in my last post of how to find the last offset in a string. Armed with this ability, I can now do in Applescript that which File.dirname and File.basename do so effortlessly in Ruby. I modeled my dirname and basename after their ruby counterparts just mentioned:
on dirname(the_path)
  set last_occurrence to last_offset(the_path, "/")
  if last_occurrence is equal to 0 then
    return "."
  end if
  if last_occurrence is equal to 1 then
    return "/"
  end if
  if last_occurrence is equal to (count of the_path) then
    set the_path to items 1 thru (last_occurrence - 1) of the_path as string
    return dirname(the_path)
  end if
  return items 1 thru (last_occurrence-1) of the_path as string
end dirname
on basename(the_path)
  set last_occurrence to last_offset(the_path, "/")
  if last_occurrence is equal to 0 then
    return the_path
  end if
  if last_occurrence is equal to 1 then
    return "/"
  end if
  if last_occurrence is equal to (count of the_path) then
    set the_path to items 1 thru (last_occurrence - 1) of the_path as string
    return basename(the_path)
  end if
  return items (last_occurrence + 1) thru -1 of the_path as string
end basename
I also wrote a third method similar to dirname but behaves better for striping out the base directory of a url:
on basedir(the_path)
  set last_occurrence to last_offset(the_path, "/")
  if last_occurrence is equal to 0 then
    return "."
  end if
  if last_occurrence is equal to 1 then
    return "/"
  end if
  return items 1 thru (last_occurrence) of the_path as string
end basedir
So what's the difference? Here are some examples:
display dialog basename("/")        -- "/"
display dialog basename("foo")      -- "foo"
display dialog basename("foo/")     -- "foo"
display dialog basename("foo/bar")  -- "bar"
display dialog basename("foo/bar/") -- "bar"
display dialog dirname("/")         -- "/"
display dialog dirname("foo")       -- "."
display dialog dirname("foo/")      -- "."
display dialog dirname("foo/bar")   -- "foo"
display dialog dirname("foo/bar/")  -- "foo"
display dialog basedir("/")         -- "/"
display dialog basedir("foo")       -- "."
display dialog basedir("foo/")      -- "foo/"
display dialog basedir("foo/bar")   -- "foo/"
display dialog basedir("foo/bar/")  -- "foo/bar/"
For these examples and all that I could think of, my dirname and basename return the same strings as ruby's File.dirname and File.basename. Notice that my basedir is not the same as either dirname, but it is convenient for operations on URLs like this:
tell application "Safari"
	set current_url to URL of front document as string
end tell
display dialog basedir(current_url)
But if you're messing with URLs in Applescript you probably want the other half to, so maybe just use last_offset on your own. Update: See my new post about extracting directory path, file name with extension and root with pure applescript.