I notice that an earlier post was much viewed:
as a JavaScript footnote, here are two JavaScript for Automation snippets (followed, for reference, by AppleScript translations) for getting the:
- full posix path
- path of the folder
- file name (if the selection is a file)
from each item selected in CocoaTech Path Finder and OS X Finder. (For the latter, it will usually be better to use the a Keyboard Maestro For Each action)
(Two separate snippets, because their scripting interfaces differ)
Path Finder version in JavaScript for Automation:
// JAVASCRIPT Version for CocoaTech PATH FINDER
// for each selected item returns a triple of:
// - full path
// - folder path
// - file name (or "" if the selection is a folder)
function run() {
var pfSeln = Application("Path Finder").selection();
return pfSeln ? pfSeln.map(pathFolderFile) : [];
}
function pathFolderFile(x) {
var strPath = x.posixPath(),
lstParts = strPath.split(/\//),
blnFolder = x.kind() === 'Folder',
strFile = blnFolder ? '' : lstParts.pop();
return [strPath + (blnFolder ? '/' : ''), lstParts.join('/') + '/', strFile];
}
OS X Finder version in JavaScript for Automation
// JAVASCRIPT Version for OS X FINDER
// for each selected item returns a triple of:
// - full path
// - folder path
// - file name (or "" if the selection is a folder)
function run() {
var lstSeln = Application("Finder").selection();
return lstSeln.length ? lstSeln.map(pathFolderFile) : [];
}
function pathFolderFile(x) {
var strPath = decodeURI(x.url()).slice(7),
lstParts = strPath.split(/\//),
blnFolder = strPath[-1] === '/',
strFile = blnFolder ? '' : lstParts.pop();
return [strPath, lstParts.join('/') + '/', strFile];
}
APPLESCRIPT TRANSLATIONS (for reference - these are fairly direct translations, and could be briefer)
Path Finder:
-- APPLESCRIPT Version for CocaTech PATH FINDER
-- (translation of the JavaScript version)
-- for each selected item returns a triple of:
-- * full path
-- * folder path
-- * file name (or "" if the selection is a folder)
on run {}
tell application ("Path Finder") to set pfSeln to selection
ternary(pfSeln is not missing value, map(pfSeln, posixPath), {})
end run
-- fsItem -> String
on posixPath(x)
using terms from application "Path Finder"
set strPath to POSIX path of x
set blnFolder to kind of x = "Folder"
end using terms from
set lstParts to splitOn("/", strPath)
set strFile to ternary(blnFolder, "", item -1 of lstParts)
set strFolder to ternary(blnFolder, strPath, intercalate("/", items 1 thru -2 of lstParts))
return {strPath & ternary(blnFolder, "/", ""), strFolder & "/", strFile}
end posixPath
-- General library functions:
-- Bool -> a
on ternary(blnTest, x, y)
if blnTest then
x
else
y
end if
end ternary
-- [a] -> (a -> b) -> [b]
on map(xs, f)
script mf
property call : f
end script
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set end of lst to mf's call(item i of xs, i, xs)
end repeat
return lst
end map
-- Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set lstParts to text items of strMain
set my text item delimiters to dlm
return lstParts
end splitOn
-- Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
OS X Finder:
-- APPLESCRIPT Version for OS X FINDER
-- (translation of the JavaScript version)
-- for each selected item returns a triple of:
-- * full path
-- * folder path
-- * file name (or "" if the selection is a folder)
on run {}
tell application "Finder" to set lstSeln to selection
ternary(length of lstSeln > 0, pathsFoldersFiles(map(lstSeln, encodedPosixPath)), {})
end run
-- fsItem -> String
on encodedPosixPath(x)
using terms from application "Finder"
URL of x
end using terms from
end encodedPosixPath
-- String -> [String]
on splitOnCommas(x)
splitOn(",", x)
end splitOnCommas
-- [String -> String]
on pathsFoldersFiles(lstCoded)
return map(splitOn(return, do shell script "
osascript -l JavaScript <<JXA_END 2>/dev/null
['" & intercalate("', '", lstCoded) & "'].map(function (x) {
var strPath = decodeURI(x).slice(7),
lstParts = strPath.split(/\\//),
blnFolder = strPath[-1] === '/',
strFile = blnFolder ? '' : lstParts.pop();
return [strPath, lstParts.join('/') + '/', strFile];
}).join('\\n');
JXA_END
"), splitOnCommas)
end pathsFoldersFiles
-- general library functions
-- Bool -> a
on ternary(blnTest, x, y)
if blnTest then
x
else
y
end if
end ternary
-- [a] -> (a -> b) -> [b]
on map(xs, f)
script mf
property call : f
end script
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set end of lst to mf's call(item i of xs, i, xs)
end repeat
return lst
end map
-- Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set lstParts to text items of strMain
set my text item delimiters to dlm
return lstParts
end splitOn
-- Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate