Output of Photoshop .jsx to KM Variable

I have a .jsx file for Photoshop that gives back the opacity value of the actual brush that is used. Right now it saves this value to the clipboard:

opacity.jsx:

#target photoshop

function BrushOpacityGet()
{
  var ref = new ActionReference();
  ref.putEnumerated(charIDToTypeID("capp"),
                    charIDToTypeID("Ordn"),
                    charIDToTypeID("Trgt")
                    );
  var AD = executeActionGet(ref);
  AD = AD.getObjectValue(stringIDToTypeID("currentToolOptions"));
  return(AD.getInteger(stringIDToTypeID("opacity")));
}


op = BrushOpacityGet();

ops = app.system("osascript -e 'set the clipboard to \"" + op +"\"'");

now I want to save this value in a KM Variable instead of clipboard, I already tried this:

ops = app.system("osascript -e 'tell application "Keyboard Maestro Engine" to setvariable "ps_opacity" to "op"'");

But that gives me back "Error 25: Expected: )."

I also tried this:

ops = app.system("osascript -e 'tell application \"" + Keyboard Maestro Engine +"\" to setvariable \"" + ps_opacity+"\"' to \"" + op +"\"'");

but without success...

How can I get this to work?

I think you need to escape the double quotes in your string:
ops = app.system("osascript -e 'tell application \"Keyboard Maestro Engine\" to setvariable \"ps_opacity\" to \"op\"'");

Regards,
René

2 Likes

thank you very much @Reheer! It works, I just changed \"op\" to \"" + op +"\" - so it gets the actual variable content from the jsx output.

Is there any good resource for escaping double quotes or maybe a kind of online generator to do that?

Not that I know of. I used VSCodium to check the escaping. You can use any javascript editor for that I guess.

But a quick google gave me this link Free online javascript escape / unescape tool.

I tested it on the complete line but that will (I should have expected that :smirk:) escape all the quotes, including the first and the last. So maybe only put in the string?

1 Like

haha, that would have been to good to be true :smiley: I will definitely try a bit more with that tool. But I guess it seems I need to learn a bit more about that.

One last question, I also want to trigger a KM macro in that jsx file, so my last line was:

opso = app.system("osascript -e 'tell application \"Keyboard Maestro Engine\" do script \"AA8641C7-065B-42F0-9324-2B4B84F25DEE\"'");

But its somehow not working, I even don't get an error message... The Macro is working, I tested it. Do you know whats wrong with that?

Good question. I haven't got a clue.
Up till now I could just answer based on my existing knowledge :nerd_face:

But I haven't got enough knowledge (yet) of KM or JS to help you with this.

Hopefully someone else will reply...

Mmmm, I couldn't get this out of my head. Let alone stand that I had no answer :grimacing:

So... I did some experimenting.
I took your osascript command, put in the UID of one of my macros and ran it in Terminal.
The result:
syntax error: Expected end of line but found identifier. (-2741)

Pasting only the AppleScript in Script Debugger resulted in 2 errors: expecting an 'end of line' and an unexpected 'end of script'. So I separated lines and added a ' end tell' command.

To 'translated' this to osascript you can use multiple -e switches. So your line should read:

opso = app.system("osascript -e 'tell application \"Keyboard Maestro Engine\"' -e 'do script \"AA8641C7-065B-42F0-9324-2B4B84F25DEE\"' -e 'end tell'");

Not tested in KM or JS, but I'm really curious if this will work... :face_with_monocle:

1 Like

Hey @Reheer, sorry for the delay! I just tested and it works! The strange thing is, in the meantime I solved it by running a Alfred Workflow that triggers a Keyboard Maestro macro, and it worked with this here:

oxo = app.system("osascript -e 'tell application id \"com.runningwithcrayons.Alfred\" to run trigger \"ps\" in workflow \"jsx\"'");

Don´t know why, but I will use your solution now - Thanks for your help!

:partying_face:

1 Like