-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom AppleScript actions: support JavaScript for automations (JXA) #2604
Comments
I've made a little progress on this by replacing but having a hard time finding much documentation on the little magic strings like
An example of how Hammerspoon is doing it: Are these better documented somewhere by Apple?: Getting no hits with https://developer.apple.com/search/?q=aevtoapp&type=Documentation or https://duckduckgo.com/?t=ffab&q=aevtoapp+site%3Ahttps%3A%2F%2Fdeveloper.apple.com |
No, these are defined in https://github.com/quicksilver/Quicksilver/blob/master/Quicksilver/Scripting/Quicksilver.sdef; each app that wants to implement "scriptability" can apparently define their own magic strings in this way. Moreover, the key phrases from the AppleScript Dictionary such as using terms from application "Quicksilver"
end using terms from You could then see: $ strings QStest.scpt
FasdUAS 1.101.10
daed
alis
NateSSD
Quicksilver.app
Debug
-/:private:tmp:QS:build:Debug:Quicksilver.app/
*private/tmp/QS/build/Debug/Quicksilver.app
ascr But if you expand it to: using terms from application "Quicksilver"
on process text
end process text
end using terms from You'll now see
Interestingly, Quicksilver seems to determine what handlers a script supports by doing a plain old search for the magic string1. I was extremely surprised this morning to discover that one can run JXA scripts with no modifications to Quicksilver just by including these magic strings somewhere in the script. In contrast to AppleScript, when saved in Script Editor (configured as JavaScript), there is relatively little "compilation" done to JXA scripts -- one can For example, the below should work find in a current build of QS; the magic strings like const app = Application.currentApplication()
// When *not* running from XCode, you can also use:
// const app = Application("Quicksilver")
// but from XCode, this will hang on `app.showNotification` or `app.displayDialog`
app.includeStandardAdditions = true
// DAEDopnt
function processText(dobj, {with: iobj}) {
app.showNotification(typeof(iobj))
app.showNotification("dobj: " + dobj + ", iobjc: " + iobj)
return "bar"
}
// DAEDgdob
function getDirectTypes() {
return ["NSStringPboardType", "NSFilenamesPboardType"]
}
// DAEDgiob
function getIndirectTypes() {
return ["NSFilenamesPboardType"]
}
// Remove the asterisk to enable this handler; without disrupting the magic string,
// QS will try to use it preferentially
// D*AEDopfl
// function openFiles(dobj, {with: iobj}) {
// return
// }
// DAEDgarc
function getArgumentCount() {
return 3
} Once I'd figured this out, it was pretty each to add the JavaScript version of the functions in question to the list of magic strings: #2745 With that PR, the below JXA works as expected, including indirect const app = Application.currentApplication()
function processText(dobj, {with: iobj}) {
app.showNotification(typeof(iobj))
app.showNotification("dobj: " + dobj + ", iobjc: " + iobj)
return "bar"
}
function getDirectTypes() {
return ["NSStringPboardType", "NSFilenamesPboardType"]
}
function getIndirectTypes() {
return ["NSFilenamesPboardType"]
}
function getArgumentCount() {
return 3
}
function openFiles(dobj, {with: iobj}) {
app.showNotification("dobj: " + dobj + ", iobjc: " + iobj)
return
} I know it seems like JXA support may not be long for this world, but it certainly is much more pleasant to work with than AppleScript in my opinion; if this seems reasonable to merge, I'll be happy to update the wiki: Footnotes |
Nice! I never knew this was a thing. My only concern is that the function names are a bit generic (not prefixed with say Having to hard-code all the strings is ugly - I was looking at whether there's a cleaner way to do it (e.g. here ) but I don't think it's worth the effort. Documentation is definitely needed, but let's do away with the Wiki :) Can you copy the contents of those two files to create some new Markdown files, then add them here: https://github.com/quicksilver/manual Once done, I'll merge this. |
Oh man, using
I agree and had considered this, but the function names seem to be (automatically?) determined from the
I agree, I included at least the Of note, the existing codebase suffers from this same potential drawback, though accidentally including Ah, I'd forgotten about the manual when bemoaning the sprawl. The wiki pages for these topics are actually pretty good; I'd like to make a new section under the |
Not working for text objects for some reason. I'll investigate. Obvious issues: I included closing parenthesis for |
Working now, should be fixed by #2883 |
Copied from: https://groups.google.com/g/blacktree-quicksilver/c/I6dkTGN0bI8/m/ebbi1s46AAAJ
Low priority. I will take another look at this after 1.7.0
After a couple days of StackOverflow threads, I can't figure out whether I can use JavaScript for Automation (JXA) in Quicksilver actions.
I have a ton of AppleScript actions, and they're one of the best parts of Quicksilver IMO, but I really hate working with AppleScript. I'm not huge on JavaScript either, but having
map
/filter
and a more traditional syntax really is a blessing compared to AppleScript. (For context, most of my current AppleScript actions just shell out to python or what have you, which is workable.)If you open up Script Editor, set the language to JavaScript, then open the Quicksilver dictionary, it looks like it supports the same handlers, just with slightly different names (instead of
on process text
it'sprocessText
).Unfortunately I've had no luck! I've tried a million or so different configurations of the below.
Looking through the source, I don't know enough Obj C to say what the issue may be. I do see `"AppleScript Action: No handler? Aborting..."`` in the logs, and poking around that place in the source and adding a few debug logs, I can see that it isn't finding any handlers in my test file.
Any ideas on this? I'd sure rather be writing these in JS (or python, or go, or swift, or rust, or ...) rather than AppleScript!
The text was updated successfully, but these errors were encountered: