Here is an improved version of my getPOSIXPath handler.
It ensures that, in the case of a folder path, the path does NOT end in a "/", consistent with Unix conventions.
use
AppleScript
version
"2.4" -- Yosemite (10.10) or later
use
scripting additions
set
tempFolderHFS to
path to
home folder
as text
set
tempFileHFS to
tempFolderHFS & "test.txt"
set
homeAlias
to
path to home folder
set
fileURL to
tempFileHFS as «class
furl»
set
myList
to {"one",
"two", "three"}
tell
application
"TextEdit"
-- as an example of tell block
set
pathFolderFromHFS to
my getPOSIXPath(tempFolderHFS)
set
pathFileFromHFS to
my getPOSIXPath(tempFileHFS)
set
pathFromAlias to
my getPOSIXPath(homeAlias)
set pathFromTilde
to my getPOSIXPath("~")
set pathFromTildeFolder
to my getPOSIXPath("~/Test")
set pathFromPOSIX
to my getPOSIXPath("/Users/Shared/")
set pathfromFURL
to my getPOSIXPath(fileURL)
--set errorTest to my getPOSIXPath(myList)
end
tell
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on getPOSIXPath(pAnyPath)
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
-- VER: 1.1 2016-09-13
(*
PURPOSE: Convert any path or alias to POSIX path
PARAMETERS:
• pAnyPath: Any Path; class: text, alias, or furl
RETURNS: POSIX Path (Never has "/" at end)
AUTHOR.@JMichaelTX
REF: makeAlias() by @ccstone (Chris Stone)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
--- FROM ALIAS ---
if
class of
pAnyPath
is alias
then
set
posixPathStr to
POSIX path
of
pAnyPath as
text
--- FROM TEXT ---
else if class
of pAnyPath
is text
then
if pAnyPath
contains ":"
then -- HFS
set
posixPathStr to
POSIX path
of
pAnyPath
--- FROM POSIX Path (various forms/shortcuts) ---
else
if pAnyPath
is
"~" or
pAnyPath is
"~/"
then
set posixPathStr
to POSIX path
of (path to
home folder) as
text
else if pAnyPath
starts with "~/"
then
set posixPathStr
to (POSIX path
of (path to
home folder)) & text
3 thru -1
of pAnyPath
else if pAnyPath
starts with "/"
then
set
posixPathStr to
pAnyPath
end if
--- FROM FILE URL ---
else if class
of pAnyPath
is «class
furl»
then
set
posixPathStr to
POSIX path
of (pAnyPath
as
text)
else
--- INVALID CLASS ---
set
classPath to
class
of pAnyPath
error "\"" &
classPath & ¬
"\" is NOT a valid class for:" & return & ¬
"Handler: on getPOSIXPath (pAnyPath)" &
return &
return &
"Must be: text, alias, or furl"
end if
--- MAKE SURE PATH DOES NOT END WITH "/" ---
if (last
character of
posixPathStr = "/")
then
set
posixPathStr to
text
1 thru
-2
of posixPathStr
end if
return
posixPathStr
end getPOSIXPath