Adding file names to folder paths (and combining filepath segments generally) can easily go wrong.
- does the left-hand segment already end with a
"/"
? - does the right-hand segment already start with one ?
Four combinatorial possibilities, which provide a rich source of confusions like the one just reported here:
If you're using Execute an AppleScript actions, or Execute a JavaScript for Automation actions, it's useful to be able to reach for a pre-cooked combine
function which just:
- takes two file path segments and returns their concatenation
- letting you forget about the problem of doubled or missing
"/"
I personally use these:
AppleScript:
-- combine (</>) :: FilePath -> FilePath -> FilePath
on combine(fp, fp1)
-- The concatenation of two filePath segments,
-- without omission or duplication of "/".
if "" = fp or "" = fp1 then
fp & fp1
else if "/" = item 1 of fp1 then
fp1
else if "/" = item -1 of fp then
fp & fp1
else
fp & "/" & fp1
end if
end combine
JavaScript:
// combine (</>) :: FilePath -> FilePath -> FilePath
const combine = fp =>
// The concatenation of two filePath segments,
// without omission or duplication of "/".
fp1 => Boolean(fp) && Boolean(fp1) ? (
"/" === fp1.slice(0, 1) ? (
fp1
) : "/" === fp.slice(-1) ? (
fp + fp1
) : `${fp}/${fp1}`
) : fp + fp1;
and FWIW I keep one for Python too:
# combine (</>) :: FilePath -> FilePath -> FilePath
def combine(fp):
'''The concatenation of two filePath segments,
without omission or duplication of '/'.
'''
def go(fp1):
if not fp or not fp1:
return fp + fp1
elif '/' == fp1[0]:
return fp1
elif '/' == fp[-1]:
return fp + fp1
else:
return fp + '/' + fp1
return go