1

I am trying to get the full path of a file that is selected by Finder. At this point, I have tried many methods:

>> Application('Finder').selection()[0]
=> Application("Finder").startupDisk.folders.byName("Users").folders.byName("example").folders.byName("Tools").folders.byName("my-tools").documentFiles.byName("example.sh")
>>
>> Application('Finder').selection()[0].name()
=> "example.sh"
>>
>> Application('Finder').selection()[0].toString()
=> "[object ObjectSpecifier]"
>>
>> Application('Finder').selection()[0].posixPath()
!! Error on line 1: Error: Can't get object.
>>
>> Application('Finder').selection()[0].path()
!! Error on line 1: Error: Can't get object.
>>
>> Application('Finder').selection()[0].url()
=> "file:///Users/example/Tools/my-tools/example.sh"
>>
>> Path(Application('Finder').selection()[0])
!! Error on line 1: Error: Can't get object.
>>
>> Path(Application('Finder').selection()[0]).toString()
!! Error on line 1: Error: Can't get object.

None of these commands obtain the posix path which is /Users/example/Tools/my-tools/example.sh.

Ultimately, I know I can simply strip the file:// off of the front of the url:

>> Application('Finder').selection()[0].url().replace(/^file:\/\//, '')
=> "/Users/example/Tools/my-tools/example.sh"

But that seems hacky, and it seems like there should be a command to obtain the posix path directly.

stiemannkj1
  • 4,418
  • 3
  • 25
  • 45
  • Also, if there are API docs for JXA, I would be grateful if anyone could point me to them. I know about [the JXA Cookbook](https://github.com/dtinth/JXA-Cookbook/wiki) and [the JXA Release Notes](https://developer.apple.com/library/content/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html), but I don't see any documentation that simply shows all the methods and properties that exist for each object (such as a file). – stiemannkj1 Jan 27 '17 at 05:03
  • 1
    JXA uses the AppleEvent definitions provided by automatable applications and scripting extensions, just like AppleScript does. To see the documentation for such a library, open Script Editor, set the Library window to show, locate the relevant application or extension (in this case: Finder) there, double click it to open the Library Viewer. There is a dropdown to select the language in which to display the documentation; if you mostly use JXA, you can also set that as the standard language in Script Editor preferences and the documentation should follow suit. – kopischke Mar 04 '17 at 10:58

1 Answers1

1

If you examin the file's properties(), you will see that the only property which contains the full path is the url(). However, special characters are escaped and the url is prefixed with the file:// protocol when using the url() property:

>> Application('Finder').selection()[0].name()
=> "asdf\"asdf.txt"
>>
>> Application('Finder').selection()[0].url()
=> "file:///Users/example/Tools/my-tools/asdf%22asdf.txt"

In order to get the POSIX path, you can unescape() the url() and remove the file:// protocol from the beginning of the url():

>> unescape(Application('Finder').selection()[0].url()).replace(/^file[:]\/\//, '')
=> "/Users/example/Tools/my-tools/asdf\"asdf.txt"
Community
  • 1
  • 1
stiemannkj1
  • 4,418
  • 3
  • 25
  • 45
  • 3
    Easier: `Path(decodeURI(Application('Finder').selection()[0].url()))` gets you a valid Path object (JXA’s equivalent to an AppleScript Alias). Encoding and decoding URIs is something where JavaScript _really_ has you covered, and the Path object is smart enough to handle `file://` protocol prefixes. – kopischke Mar 04 '17 at 11:03