Re: Getting the name of a file from an "alias"
Re: Getting the name of a file from an "alias"
- Subject: Re: Getting the name of a file from an "alias"
- From: Arthur J Knapp <email@hidden>
- Date: Wed, 13 Jun 2001 11:14:02 -0400
>
Date: Mon, 11 Jun 2001 23:15:18 +0200
>
Subject: Getting the name of a file from an "alias"
>
From: Jan Pieter Kunst <email@hidden>
>
alias "Disk:folder1:folder2:file"
>
>
How do I get just the name of the file (in the above case "file") into a
>
string?
The easiest way is to use the Finder:
tell application "Finder"
set theName to name of alias "Disk:folder1:folder2:file"
A vanilla method requires coercing the alias to a string, and then
parsing it. There are several methods for accomplishing this:
on NameFromPath(a_path) -- using the text item delimiters
-- a_path can be an alias or a string
--
set a_path to "" & a_path -- coerce to string
if (character -1 of a_path = ":") then
set name_item to -2 -- a_path = folder
else
set name_item to -1 -- a_path = file
end if
set text item delimiters of AppleScript to {":"}
set a_name to text item name_item of a_path
set text item delimiters to {""}
return a_name
end NameFromPath
on NameFromPath(a_path) -- by traversing
-- a_path can be an alias or a string
--
set a_path to "" & a_path -- coerce to string
if character -1 of a_path = ":" then -- a_path = folder
set a_path to text 1 thru -2 of a_path
end if
set x to -1
repeat until character x of a_path = ":"
set x to x - 1
end repeat
set a_name to text (x + 1) thru -1 of a_path
return a_name
end NameFromPath
on NameFromPath(a_path) -- using the offset-of command
-- a_path can be an alias or a string
--
set a_path to "" & a_path -- coerce to string
-- > "MacHD:Fold01:Fold02:File01"
if character -1 of a_path = ":" then -- folder
set a_path to text 1 thru -2 of a_path
end if
set parse_path to reverse of every character of a_path
-- > {"1", "e", "l", "i", "F", ":", "2", "d", etc... }
set parse_path to parse_path as string
-- > "1eliF:2dloF:1dloF:DHcaM"
set x to (offset of ":" in parse_path) - 1
-- > 5
set a_name to text -x thru -1 of a_path
return a_name
end NameFromPath
From the above method, we can work out these very verbose statements:
set a_path to "MacHD:Fold01:Fold02:File01"
-- Syntax 1
--
a_path's text -((offset of ":" in ("" & a_path's items's reverse)) - 1) thru
-1
set a_name to result
-- > "File01"
-- Syntax 2
--
tell a_path
it's text -((my (offset of ":" in ("" & it's items's reverse))) - 1)
thru -1
set a_name to result
-- > "File01"
end tell
Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden
Hey, check out:
http://www.natural-innovations.com/as/