Skip to content

Instantly share code, notes, and snippets.

@cesarandreu
Last active October 1, 2018 00:25

Revisions

  1. cesarandreu revised this gist Oct 1, 2018. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions FinderUtils.js
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,22 @@
    function getSelection () {
    return Application('Finder')
    .selection()
    .map(item => urlToPath(item.url()))
    .join('\n')
    return Application('Finder')
    .selection()
    .map(item => urlToPath(item.url()))
    .join('\n')
    }

    function getPath () {
    const windows = Application('Finder').windows()
    if (!windows.length) {
    const windows = Application('Finder').windows()
    if (!windows.length) {
    throw new Error('No Finder windows open')
    } else {
    } else {
    return urlToPath(windows[0].target().url())
    }
    }
    }

    function urlToPath (url) {
    if (!url.startsWith('file://')) {
    throw new Error(`Unexpected URL scheme: ${url}`)
    }
    return $.NSURL.alloc.initWithString($(url)).fileSystemRepresentation
    if (!url.startsWith('file://')) {
    throw new Error(`Unexpected URL scheme: ${url}`)
    }
    return $.NSURL.alloc.initWithString($(url)).fileSystemRepresentation
    }
  2. cesarandreu created this gist Oct 1, 2018.
    22 changes: 22 additions & 0 deletions FinderUtils.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    function getSelection () {
    return Application('Finder')
    .selection()
    .map(item => urlToPath(item.url()))
    .join('\n')
    }

    function getPath () {
    const windows = Application('Finder').windows()
    if (!windows.length) {
    throw new Error('No Finder windows open')
    } else {
    return urlToPath(windows[0].target().url())
    }
    }

    function urlToPath (url) {
    if (!url.startsWith('file://')) {
    throw new Error(`Unexpected URL scheme: ${url}`)
    }
    return $.NSURL.alloc.initWithString($(url)).fileSystemRepresentation
    }
    22 changes: 22 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # macOS JXA utilities

    * `pfd`: Return the path of the frontmost Finder window. Mnemonic: **p**rint **F**inder **d**irectory.
    * `pfs`: Return the current Finder selection. Mnemonic: **p**rint **F**inder **s**election.
    * `cdf`: cd to the current Finder directory. Mnemonic: **cd** **F**inder.

    ## Installation

    1. Open Script Editor from `/Applications/Utilities`.
    2. Create a new script and save it to `~/Library/Script Libraries` with the name `FinderUtils.scpt`. Make sure that File Format is set to Script in the save dialog.
    3. Copy the contents of `FinderUtils.js` to your newly created script library.
    4. Save each utility to `~/.local/bin` with permissions set to `755`.

    ## Notes

    None of these may be used remotely, they will only work locally.

    The `pfd` utility will fail for special views such as Recents, Computer, AirDrop, or Network. I don't know if it works with iCloud Drive.

    These utilities were heavily inspired by oh-my-fish's [osx plugin](github.com/oh-my-fish/plugin-osx).

    License: MIT.
    4 changes: 4 additions & 0 deletions cdf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    #!/usr/bin/env sh
    # cd to the current Finder directory
    set -e
    cd "$(pfd)"
    3 changes: 3 additions & 0 deletions pfd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    #!/usr/bin/env osascript -l JavaScript
    // Return the path of the frontmost Finder window
    Library('FinderUtils').getPath()
    3 changes: 3 additions & 0 deletions pfs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    #!/usr/bin/env osascript -l JavaScript
    // Return the current Finder selection
    Library("FinderUtils").getSelection()