Pass an Excel Command

I am trying to set an AppleScript command as a string and then use the string as the command.

The following script works

tell application "Microsoft Excel"
    activate
    tell application "System Events"
        tell process "Microsoft Excel"
            key code 123 using {control down, option down, command down}
            key code 2 using {control down, option down}
        end tell
    end tell
end tell

However if I try and pass the command like this it does not. It tells me “PosDes” not defined tried passing it as a string no luck with that either.

tell application "System Events"
    set PosDes to key code 124 using {control down, option down, command down}
end tell

tell application "Microsoft Excel"
    activate
    tell application "System Events"
        tell process "Microsoft Excel"
            PosDes
        end tell
    end tell
end tell

key code is a command that does not return a value. So setting a variable to a key code command does nothing.

What is it you are trying to do with these keystrokes? There might be a built-in Excel command to do the same thing.

Try this way. Not tested, and I am not sure it will work:


set PosDes to "key code 124 using {control down, option down, command down}"

tell application "Microsoft Excel" to activate

tell application "System Events" to run script PosDes

First thank you both and I apologise for not describing my issue correctly.

I use an application “Magnet” to position apps on two monitors, I had written some code in the past to do this but it never worked well. So I installed this app which is installed every time I start my machine hence in theory at least I can invoke by using the short cuts it provides.

That particular line “key code 123 using {control down, option down, command down}” moves Excel to the screen on the right if it happens to be on the left.

In the project I am working on I have open Excel, Safari and either script editor or Script Debugger at the same time.

KniazidisR, I had tried something similar to your suggestion not as elegant as yours, but to be sure I copied yours and tried to run that and the message I get is “A identifier can’t go after this property”

I assume I have to somehow turn that string into an object but cannot figure out how to.

Thank you both again

Usually that error means that a command is being issued outside the appropriate tell block, so moving the offending line might resolve that. But just guessing since you didn’t include your code.

Two questions… First, what happens if you give the key code command to Magnet instead of to Excel?

Second, out of curiosity, when you say you want to put Excel on the right-hand display do you mean its windows? Or does this app move the menus to the other display? If it’s just the windows, have you tried setting the window bounds so that it aligns to the appropriate display?

I have my external display on the left, so I could use this to position an excel window flush against that display’s left and top edges…

set bounds of window 1 to {-1920, 0, -100, 900}

You could also set up a repeat loop to cascade every open window there.

Thank you, not sure I understand your question correctly.

My first post included my code, and I tried the suggestion exactly as it was posted by KniazidisR.

It is only the windows I am moving and I understand setting bounds although if the screen with the menu changes that can confuse the issue.

As far as using “Magnet”, I am not sure how that will advance the cause as the window I am moving is Excel & others.

Magnet has 18 commands and a lot of them have a repetitive structure like {control down, option down ,etc} . What I am trying to down is shorten the strings, maybe a waste of time and I should go back to setting bounds. That will result in more code working out if the left is 0 or -1920.

As I understand now from your last explanation, your key code command belongs to Magnet.app, and not to Excel.app. So, send it to appropriate process:


tell application "System Events"
	tell application process "Magnet" to set frontmost to true
	key code 124 using {control down, option down, command down} -- goes to Magnet's window
	tell application process "Microsoft Excel" to set frontmost to true
end tell