-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2017/02/28 12:00
# dMod: 2017/02/28 12:21
# Appl: AppleScriptObjC
# Task: Convert file or file-path (hfs or posix) into a NSURL – output as NSURL, HFS path, or POSIX Path.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Convert, @File, @Path, @NSURL, @HFS, @POSIX, @Path, @POSIX_Path
-------------------------------------------------------------------------------------------
use framework "Foundation"
use framework "AppKit"
use scripting additions
-------------------------------------------------------------------------------------------
set theAlias to path to downloads folder
set theFurl to theAlias as «class furl»
set thePosixPath to POSIX path of theAlias
set theTildePath to "~/Downloads/"
set converted_theAlias to its makeURLFromFileOrPath:theAlias
set converted_theFurl to its makeURLFromFileOrPath:theFurl
set converted_tildePath to its makeURLFromFileOrPath:theTildePath
set converted_posixPath to its makeURLFromFileOrPath:thePosixPath
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on makeURLFromFileOrPath:theFileOrPathInput
# Make input into a Cocoa object for easier comparison
set theFileOrPath to item 1 of (current application's NSArray's arrayWithObject:theFileOrPathInput)
if (theFileOrPath's isKindOfClass:(current application's NSString)) as boolean then
if (theFileOrPath's hasPrefix:"/") as boolean then -- input is full POSIX path
set myNSURL to current application's class "NSURL"'s fileURLWithPath:theFileOrPath
else if (theFileOrPath's hasPrefix:"~") as boolean then -- input is a POSIX path needing ~ expansion
set myNSURL to current application's class "NSURL"'s fileURLWithPath:(theFileOrPath's |stringByExpandingTildeInPath|())
else -- input must be an HFS path
set myNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of theFileOrPathInput)
end if
else if (theFileOrPath's isKindOfClass:(current application's class "NSURL")) as boolean then -- happens with files and aliases in 10.11
set myNSURL to theFileOrPath
else -- must be a file or alias
set myNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of theFileOrPathInput)
end if
# return myNSURL
# return myNSURL as text -- HFS path
return POSIX path of (myNSURL as text)
end makeURLFromFileOrPath:
-------------------------------------------------------------------------------------------