0

I know that you can tell Chrome or Safari to do JavaScript (or write entirely in JS... à la JXA) but whilst answering another question, I got curious...

Is there a way to arbitrarily execute an inline JavaScript statement inside an AppleScript (without telling another process)?

So far I've only used something like this (in an AppleScript):

do shell script "osascript -l JavaScript -e '\"" & myVar & "\".trim()'"

But that seems such a silly workaround

Community
  • 1
  • 1
brandonjp
  • 3,018
  • 2
  • 28
  • 27
  • Why do you want to write JavaScript inside an AppleScript? Why not just write it all in JXA? – JMichaelTX Mar 05 '16 at 01:40
  • @JMichaelTX Mainly because JS has conveniences that AS does not (like `trim()` for example)... sometimes it's much quicker for me to write the JS than figure out the same thing in AS or convert the whole script to JXA – brandonjp Mar 08 '16 at 18:58

1 Answers1

-2

If you want to write JavaScript, then you should set the Script Editor language to "JavaScript", and write the entire script in JavaScript, not AppleScript.

If you don't need access to a web page, you can enter any JavaScript you like directly into the Script Editor:

var myVar = "   whatever   "
myVar = '"' + myVar + '"'
myVar = myVar.trim()
JMichaelTX
  • 1,659
  • 14
  • 19
  • Thanks for the reply. Yea, I'm aware you can write the entire file in JS. I should've mentioned that & will add it to the question. – brandonjp Mar 04 '16 at 23:08
  • 1
    WHO gave me a DOWN vote for my answer? Please explain why. It may not be the perfect answer, but it is helpful, at least to some who may not realize that now the Mac supports JavaScript for Automation (JXA). – JMichaelTX Mar 05 '16 at 01:36
  • that was me with the down vote. The answer is not useful to me, but I think you're right that it is helpful to "some who may not realize that now the Mac supports JavaScript for Automation (JXA)" -- Good point! – brandonjp Mar 08 '16 at 18:54
  • @brandonjp: Then please remove your downvote. May I suggest that just because an answer is not the one you are looking is NOT a valid reason to downvote it. IMO, an answer should NOT be downvoted unless it is completely off-topic, completely non-responsive to the question. By downvoting on answers you don't like, you are discouraging people from posting great answers that may be a little bit out of the box. – JMichaelTX Mar 08 '16 at 22:04
  • I'm sorry you feel offended. When hovering over the down vote button, the tooltip says "This answer is not useful." To me, the answer was not useful, because it did not answer the question. – brandonjp Mar 09 '16 at 18:09
  • @brandonjp: I can see how the DownVote tooltip is misleading. See [When should I vote down?](http://stackoverflow.com/help/privileges/vote-down) – JMichaelTX Mar 10 '16 at 01:53
  • From [When should I vote down?](http://stackoverflow.com/help/privileges/vote-down): [quote] When should I vote down? Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. You have a limited number of votes per day, and answer down-votes cost you a tiny bit of reputation on top of that; use them wisely. [/quote] – JMichaelTX Mar 10 '16 at 01:54
  • I'm not going to down-vote this myself, but @JMichaelTX you could consider making information like this a comment to the question rather than an answer. This information, while correct and maybe helpful to other users, does not answer this (clearly-worded and specific) question. Unfortunately Stack Overflow just isn't very friendly to "answers" that consist only of adjacent information, correct though they may be. A comment is a great place to provide that. – Ben Zotto Apr 17 '16 at 23:46
  • @BenZotto: I am really more interested in providing information that will be helpful to the community at large, even if it does not meet the esoteric "requirements" of where to put info in StackOverflow. It is interesting to note that this question has NOT received any answers other than the one I posted. I think that tells you something. I'm more of a function over form guy, even if it doesn't fit the form properly. – JMichaelTX Apr 18 '16 at 06:20