Giter Site home page Giter Site logo

Comments (9)

Ontonator avatar Ontonator commented on May 10, 2024 1

You can call openForAccess using the current application with standard additions, like so:

var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.openForAccess(Path("/path/to/file"));

I'm a bit late, but someone may find this later like I did.

from jxa-cookbook.

Tatsh avatar Tatsh commented on May 10, 2024

It is true. A lot of weird stuff has to go through System Events rather than their direct objects. This also affects processes as I documented here: https://github.com/dtinth/JXA-Cookbook/wiki/System-Events#clicking-menu-items (where you cannot get menu items through Application('name of app') but you must go through SE).

I guess you are looking for something like basename(). I think there is no good short-cut right now, so you would have to make a function for yourself. You can even add it to Path prototype.

The Objective-C bridge way:

ObjC.import('Foundation');
var fm = $.NSFileManager.defaultManager;
var basename = function (fullpath) {
    return ObjC.unwrap(fm.displayNameAtPath(fullpath));
};

Or attach the SE version to Path() prototype:

Path.prototype.name = function () { return Application('System Events').aliases.byName(this.toString()).name(); };

I have not figured out if we can import the actual C function. That would be libgen but that fails to import. The things you can import are at /System/Library/PrivateFrameworks/JavaScriptAppleEvents.framework/Versions/A/Resources/BridgeSupportCache. If a framework defines $.basename() I would like to know (it would have to define basename() in C).

Alternative function with toString(), split('/'), and pop():

var basename = function (s) { return Path(s).toString().split('/').pop(); }
>> basename('/Applications/Xcode.app')
=> "Xcode.app"

How you call openForAccess I have no idea:

>> f = Application('Finder')
=> Application("Finder")
>> f.includeStandardAdditions = true
=> true
>> ref = f.openForAccess(Path('/Users/tatsh/Downloads/xcode_6.dmg'))
!! Error on line 1: Error: A privilege violation occurred.
>> ref = f.openForAccess(Path('/Users/tatsh/Downloads/xcode_6.dmg'), {writePermission: false})
!! Error on line 1: Error: A privilege violation occurred.

from jxa-cookbook.

Tekl avatar Tekl commented on May 10, 2024

Thanks for you detailed answer.

The pop() trick is nice, so I don't have to use Path() and just use the string.

'/path/to/file'.split('/').pop()

I don't understand what Path() is for. It has no properties.

from jxa-cookbook.

Tatsh avatar Tatsh commented on May 10, 2024

Seems to be there for some kind of internal mechanism that AppleScript layer uses. Old code would be: set ref to (path to '/Users/tatsh/somefile.txt') and you would have to use ref with the open function:

tell application "TextEdit"
    set ref to (path to "/Users/tatsh/somefile.txt")
    open ref
end tell

You could not just pass the string.

from jxa-cookbook.

dtinth avatar dtinth commented on May 10, 2024

Hello,

Thanks for your comments and contributions. I've never accessed files in my script; just through Finder so far, so I didn't have much need for that. That's why I did not cover about them.

I agree that file manipulation should be elaborated more. In fact, I think it even needs its own section, given the amount of multiple ways of doing things. I'll put that on my todo list.

About generic methods, I've never used "byName" in favor of ['name'] notation. I agree that it is worth mentioning briefly (better link to relevant docs to keep it small and simple).

from jxa-cookbook.

criztiano avatar criztiano commented on May 10, 2024

I tried using @Ontonator 's solution with QuickTime but still getting the same error posted by @Tatsh:

A privilege violation occurred.

Any idea of why this might happen?
(not posting the code as it's identical, just targeting QT instead of current app)

from jxa-cookbook.

Ontonator avatar Ontonator commented on May 10, 2024

I'm not sure why that's happening (although it is happening to me as well). It does seem to happen for other applications as well, and doesn't seem to happen for all commands (displayDialog, for example, seems to work fine).

Not that this is a solution, but would it still work if you accessed the file through Application.currentApplication() instead of QuickTime?

from jxa-cookbook.

criztiano avatar criztiano commented on May 10, 2024

@Ontonator strangely, it actually works this way... 🤔

from jxa-cookbook.

JMichaelTX avatar JMichaelTX commented on May 10, 2024

I just posted a file related question here, before I discovered this issue thread.
#8 (comment)

Just in case someone would like a challenge, here's one that has stumped all JXA experts so far.
What is the JXA equivalent of this AppleScript?

tell application "Finder"
set itemList to (selection as alias list)
end tell

from jxa-cookbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.