Comments (9)
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.
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.
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.
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.
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.
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.
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.
@Ontonator strangely, it actually works this way... 🤔
from jxa-cookbook.
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)
- Possible to intercept http link clicks from an app?? HOT 3
- better integrating Objective-C Bridge documentation + proposed expansions HOT 6
- JXA chat network HOT 1
- Problems with Mojave? HOT 20
- Can't import a custom framework HOT 4
- Using IconRef HOT 2
- How can I create a http request with JXA? HOT 5
- How to copy image or file to clipboard? HOT 15
- Integrating with JXA-Snippets example scripts HOT 1
- Object specifiers and Apple Music HOT 4
- How do you get a random number in JXA? HOT 2
- How to make FSEventStream Work HOT 4
- NSInvalidArgumentException: NSGetSizeAndAlignment(): unsupported type encoding spec 'G' at 'GPoint}"size"{CGSize}}' in '{CGPoint}"size"{CGSize}}' HOT 7
- Arrow functions now work in shebang files HOT 2
- Please state a license for use/reuse of the text
- How do I initialize a pointer to a specific C struct? HOT 3
- Mismatched method names in ObjC example
- writeToFileAtomicallyEncodingError has "getFileSystemRepresentation:maxLength"
- Add Missing Event Function `GURLGURL` (JXA-equivalent of `open location`)
- Library impoting
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jxa-cookbook.