Using modifiers with RDP Windows session

Thanks. I made changes in my script. Universal way to Script Editor and Script Debugger using:


tell application "System Events"
       -- Get user's script editor name
	set scriptEditorName to name of item 1 of (processes whose frontmost is true)
	-- Hiding script editor
	set visible of process scriptEditorName to false
	-- Get name of app, which was frontmost before the script editor
	set appName to name of item 1 of (processes whose frontmost is true)
	-- Press shortcut
	tell process appName to key code 9 using shift down
end tell

-- Show the result
tell application scriptEditorName to activate
appName

Thanks!

Although my problem remains. It wont send or print (on notepad) uppercase V but only lowercase v.

I have never used RDP, so I can not understand the subtleties. In your script, I fixed only the obvious problem. In addition, you provide little information. If you provided at least a screenshot of what you are trying to do, there is probably someone who understands RDP.

Or, at least, what app name does my script return when run on your computer? Or, what does this shortcut you tried to send on this app? Is on remote computer enabled GUI keystrokes and key codes?

Probably, need send keystroke not to app but to its window, or to some element of its window.

And no need uppercase “V” to keystroke, it uses “v” syntax

Thanks again. At this point I am only trying to send some text with an uppercase letter (ex: Victory) to an open notepad in RDP (windows). I see that when I use keystroke or key code (individually sending each key with using shift down on v as shown in original post), it only prints victory and not Victory. It is not limited to this combination, any modifier I try, it is not being sent through (or received through) but only letters are printed (without modifiers).

With your script I get the app name as: Microsoft Remote Desktop
You mention checking enabling GUI keystrokes, where and how would I do that?

As I see, here no need modifiers. Replace this

tell application process theAppName to key code 9 using shift down

with this

tell application process theAppName to keystroke "Victoria"

I tried it again with keystrokes… and it only prints victoria and not Victoria.

I don’t believe :lol: And this?

tell application "System Events" to keystroke "Victoria"

or this?


tell application "System Events" to tell process appName to tell window 1 to keystroke "Victoria"

I have Microsoft Remote Desktop on my machine. But how can I connect to some Notepad for free to try myself?

Both still prints just victoria. No uppercase.

I was able keystroke in the Notepad word “Victoria” successfully (not on RDP, but on my Mac) only with this code:

tell application "System Events"
	-- Get user's script editor name
	set scriptEditorName to name of item 1 of (processes whose frontmost is true)
	-- Hiding script editor
	set visible of process scriptEditorName to false
	-- Get name of app, which was frontmost before the script editor
	set appName to name of item 1 of (processes whose frontmost is true)
	-- Press shortcut
	tell process appName -- "wineloader" without RDP
		set value of attribute "AXEnhancedUserInterface" to true
		keystroke "Victoria"
		delay 1
	end tell
end tell

-- Show the result
tell application scriptEditorName to activate
appName

NOTE: Notepad on my machine is in one wine bottle of Crossover.app. It seems, need delay after the keystroke :lol: Now I go to work.

Okay it worked. I updated my RDP client to latest and just works. Both using keystroke and key code I was able to pass modifiers. I am not sure what was up with the previous version of RDP I was using.

Thank you KniazidisR for helping me here.

I have a similar problem. I can’t figure out how to send the alt modifier to RDP. Here’s the script I have but it doesn’t work because alt is not supported:


events = Application("System Events")
events.keystroke("m", {
    using:['control down', 'alt down']
});

Using option down instead seems like it would work, but it doesn’t. Is there a way to send modifiers using their key codes instead? Something tells me this is the solution.

Not sure if it will help in your situation but the modifier keys do have codes.

[format]55 - command (either side)
57 - left shift*
58 - left option
59 - left control
60 - right shift
61 - right option
[/format]
I see 57 for both the left shift and the caps lock key but no 56, so it’s possible that I have that code wrong and that the other is 56.

Yeah, I guess there are two questions associated with this:

  1. How do you send modifier keys by their key codes in a way that they are ‘down’ in concert with another key?
  2. Can I send a non-mac key (alt)?

Basically, how KniazidisR explained it. I think you have been mixing ‘key code’ with ‘keystroke’.

Key code is part of System Events. You can view the syntax in the System Events dictionary (along with keystroke) but basically there are three styles. The code by itself… in this case the letter ‘v’. To get a single modifier key, append with ‘using command down’ (or shift, option, control). Finally, to get multiple modifier keys, place them inside curly braces.

Do not use quotes of any kind as you’re not sending text (as with ‘keystroke’).

tell application "System Events"
	
	key code 9
	key code 9 using command down
	key code 9 using {command down, option down}

end tell

The ‘alt’ key isn’t a non-mac key.

FWIW, while I don’t do much remotely, some apps have settings to determine how the software/remote machine should interpret various commands. Perhaps RDP does too.

OK. Yeah, this is kind of what I expected. There is no easy way to call a modifier by their key code (at least with what comes with Script Editor out of the box) and you can’t send the alt keydown modifier on a mac. There is a way to configure RDP but the problem is I’m two RDP sessions deep and I don’t have control over the settings for the 2nd RDP session (hence why I’m just trying to send the keys the 2nd RDP expects; seemed like the simplest approach to me, but alas, I realize it’s not sigh).

I’m not sure I understand you.

‘key code’ is not ‘keystroke’

It is very straightforward to send it. If you want to send ‘alt-control-m’ then this command will do so. How much easier can it be?

tell application "System Events" to key code 46 using {option down, control down}

Hi,

Making keystrokes work in JXA requires a few extra steps.

For example, let’s take Finder.app as the target application. Opening the “Downloads” folder in the Finder.app menu has the shortcut “L” + Command + Option. I will program it in my example:


// the Keystroke and the Keycode commands belond to Standard Additions,
// so, following code line is requred 
Application.currentApplication().includeStandardAdditions = true;

// bring to front the target application (it is Finder in my example)
Application('Finder').activate();

// optional delay. Maybe, need some delay for stability
delay(1);

// keysroke which opens Downloads folder in the Finder
Application('System Events').keystroke('l', { using: ['command down', 'option down'] });

NOTE:

  1. save this JXA example as application. Run it.

  2. The prompt to control System Events by this new app will pop up. Select Yes.

  3. Other prompt - “Not allowed to send keystrokes” will pop up. Go to Accessibility of Security & Privacy pane of System Preferences. Add your new application to applications allowed to control your Mac.

  4. Now all settings is OK, so the app can be used any moment. Run it again to ensure it works as expected.

Thanks KniazidisR, Fredrik71 and Mockman. Thanks to Mockman’s suggestion I did find some settings in Citrix Viewer that might do the trick (Unfortunately my 2nd RDP isn’t working at the moment so I can’t actually test it).

Regarding mixing up the concept of “key code” vs “keystroke,” I don’t think that’s the problem here. I simply was trying to find a way to call the alt key directly without needing to use two key combinations (such as command down, option down). Whether that was by using a single key code for the alt key or keystroke of some kind, I didn’t really care (and yes, I do know the difference). Thanks to Mockman’s (a very apt user name I might add) suggestion, I think the problem has more to do with Citrix Viewer. I changed a setting:

Hopefully, this propagates to the second, child RDP session.

So, yeah, this entire thing seems to have been a nothing burger because it had to do with a setting in Citrix Viewer, not something I needed to script.

Regarding KniazidisR’s post about bringing the correct app to front, thanks for that input as well. I’m using FastScripts and calling scripts using hotkeys so I don’t need to worry about making sure the target app is in focus.

Anyway, thanks for all the input. I appreciate all your suggestions!

I would say more acerbic than mocking but I could never come up with a fitting username. Glad you’re seeing some resolution to your problem.