I wanted a handler to return a folder a specified distance up the path hierarchy from a submitted path. But Applescript supports an annoying number of possible ways to specify a path, and I wanted it to handle all of them. It’s working fine, I’m posting it to share, but at the end I’ve got a question I came across while working on it, in case anyone would care to chime in. It’s not important, it seems to be working fine in testing, I’m just idly curious.
Also, if anyone has improvements or suggestions to add features, let me know.
The script:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- TESTING VARIOUS TYPES
set aliasPath to path to downloads folder
tell application "Finder" to set folderObject to folder aliasPath
set fileObject to item 1 of folderObject
set HFStextPath to aliasPath as text
set POSIXtextPath to POSIX path of aliasPath
set POSIXfile to POSIX file POSIXtextPath
set stepsUp to 2
set aliasTest to step_up_path(aliasPath, stepsUp)
set folderObjectTest to step_up_path(folderObject, stepsUp)
set fileObjectTest to step_up_path(fileObject, stepsUp)
set HFStextTest to step_up_path(HFStextPath, stepsUp)
set POSIXtextTest to step_up_path(POSIXtextPath, stepsUp)
set POSIXfileTest to step_up_path(POSIXfile, stepsUp)
-- Accepts a file or folder reference submitted in any format - alias, Applescript file or folder object, POSIX file, or a POSIX or HFS path as text. Returns reference to higher folder in the same format in which the path was submitted. Second argument is the number of steps to traverse up the heirarchy.
on step_up_path(aPath, numberOfSteps)
set pathClass to the class of aPath
if pathClass is «class furl» then -- class of POSIX path
set aPath to aPath as alias
else if pathClass is text then
try
set aPath to alias aPath
set textPathType to "HFS"
on error
try
set aPath to (POSIX file aPath) as alias
set textPathType to "POSIX"
on error
display dialog "Error: Text was input to the function \"step_up_path\", but the text could not be converted to a valid HFS or POSIX path."
return
end try
end try
end if
set originalPath to aPath
tell application "Finder"
repeat numberOfSteps times
try
set aPath to the parent of aPath
on error
display dialog "Error: In the function \"step_up_path,\" the number of requested steps (" & numberOfSteps & ") was greater than the number of path elements in:" & return & originalPath as text
return
end try
end repeat
if pathClass is alias then
set returnPath to aPath as alias
else if pathClass is «class furl» then
set returnPath to POSIX path of (aPath as alias)
else if (pathClass is folder) or (pathClass is document file) then
set returnPath to aPath
else if pathClass is text then
if textPathType is "HFS" then
set returnPath to (aPath as alias) as text
else if textPathType is "POSIX" then
set returnPath to POSIX path of (aPath as alias)
end if
end if
end tell
return returnPath
end step_up_path
OK, two questions that came up writing this.
ONE
set aliasPath to path to downloads folder
set aliasClass to the class of aliasPath
set textPath to aliasPath as text
-- works
set convertClassExplicitly to textPath as alias
-- errors
set convertClassWithVariable to textPath as aliasClass
If you can store a class in a variable, why can’t you convert things to that class specifying it as a variable? Is there a way to do this? I guess it can be meta-scripted:
set aliasPath to path to downloads folder
set aliasClass to the class of aliasPath
set textPath to aliasPath as text
-- works
set convertClassExplicitly to textPath as alias
-- works
set convertClassWithVariable to run script "set convertClassWithVariable to \"" & textPath & "\" as " & aliasClass as text
But that always feels confusing and hack-y to me.
As you can see in my script, I just worked around it with a long “if…then” statement for all the classes.