Single-Quote a POSIX Path String or Tilde-Based POSIX Path String

Hey Folks,

A lot of people have trouble understanding how to properly quote a POSIX Path string.

POSIX Paths don't need to be quoted unless they have spaces in them.

A tilde-based POSIX Path CANNOT have the first two characters ~/ quoted, because if they're quoted the shell won't see them as a token to expand to the user's home directory – it will see them as a simple string.

So – let's make a macro to take the work out of quoting a POSIX Path.

  • Tested only with Keyboard Maestro 8.1.1

Single-Quote a POSIX Path String or Tilde-Based POSIX Path String.kmmacros (7.3 KB)

I'll include another macro that operates on selected-text here in a few minutes.

-Chris

1 Like

Here's the other macro.

  • Operates on the selected-text in any application or text-field.
  • Tested only with Keyboard Maestro 8.1.1

Single-Quote the Selected POSIX Path String or Tilde-Based POSIX Path String.kmmacros (7.0 KB)

1 Like

On the narrower issue of tilde expansion and filePath normalisation, here are snippets for execute script actions in:

Applescript

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

-- filePath :: String -> FilePath
on filePath(s)
    ((current application's ¬
        NSString's stringWithString:s)'s ¬
        stringByStandardizingPath()) as string
end filePath


on run
    
    filePath("~/Desktop")
    
end run

Javascript for Automation

(() => {
 
    // filePath :: String -> FilePath
    const filePath = s =>
        ObjC.unwrap(ObjC.wrap(s)
            .stringByStandardizingPath);
            
            
    return filePath('~/Desktop');
    
})();
1 Like