Blues
December 8, 2020, 8:27pm
1
Hi,
I looking a way to remove text, specifically the path of a folder from a variable, leaving behind only the folder name behind.
For example:
/Users/mac110/Desktop/Jobs/21389_BoringPrintProject
I just want the name of folder that I'm in, so "21389_BoringPrintProject" is what I want in the variable.
I kinda want to use JavaScript to perhaps combine the .split JavaScript that I been using for my Job Number variables, however that's the only one I know of.
Perhaps this kind of thing, in an Execute JS action which captures the file path from a KM variable ?
(() => {
'use strict';
const main = () => {
// const
// fp = Application('Keyboard Maestro Engine')
// .getvariable('varName')
const fp = '/Users/mac110/Desktop/Jobs/21389_BoringPrintProject';
return last(fp.split('/'));
};
// --------------------- GENERIC ---------------------
// last :: [a] -> a
const last = xs =>
// The last item of a list.
0 < xs.length ? (
xs.slice(-1)[0]
) : undefined;
return main();
})();
For testing whether that last element is a folder or a file you can use things like:
// https://github.com/RobTrew/prelude-jxa
ObjC.import('AppKit')
// doesDirectoryExist :: FilePath -> IO Bool
const doesDirectoryExist = fp => {
const ref = Ref();
return $.NSFileManager.defaultManager
.fileExistsAtPathIsDirectory(
$(fp)
.stringByStandardizingPath, ref
) && ref[0];
};
// doesFileExist :: FilePath -> IO Bool
const doesFileExist = fp => {
const ref = Ref();
return $.NSFileManager.defaultManager
.fileExistsAtPathIsDirectory(
$(fp)
.stringByStandardizingPath, ref
) && 1 !== ref[0];
};
Blues:
I looking a way to remove text, specifically the path of a folder from a variable, leaving behind only the folder name behind.
For example:
/Users/mac110/Desktop/Jobs/21389_BoringPrintProject
Have you tried using the KM Get File Attribute action , which also works with folders?