2

So I have a simple AppleScript that I think should type a bunch of keys, but the period character doesn't print in VMware Fusion's virtual machines. This is odd because my period key (.) enters a period like normal in those VMs.

MCVE

Here is my latest version that I tried:

on run {input, parameters}

    set file_text to "this another test: ... hello ..."
    delay 0.3
    repeat with ch in file_text
        tell application "System Events" to keystroke ch
        delay 0.1
    end repeat

    return input
end run

I built this as a service in Automator. I run this by going to my application, then in the Title bar I select Services -> quicktype.

Expected Behavior

It should just type the text this another test: ... hello .... In many application, it does exactly that.

True Behavior

In a FreeBSD VM, pressing . renders a period at the shell (like normal). When the above script runs, however, the periods look like backspaces. End result: FreeBSD after running script

In a Windows VM, . renders a period in Notepad (like normal). When the above script runs, however, the periods seem to be ignored completely. End result: Windows Notepad after running script

Questions

Is there a way to truly emulate pressing the keys from a string of characters in AppleScript? Because clearly the above way isn't doing that.

Does anyone know why the behavior I described above is happening? Why does this work in TextMate and Terminal, and other mac apps, but not in Fusion VMs?

2
  • If by chance something, somewhere in the VM is changing the three consecutive periods: . . . into an ellipsis: ... it would explain the backspace behavior in the BSD shell. Try two periods instead of three, or separate them with spaces. (As it happens, I had to separate the three periods in this post with spaces to prevent them from turning into an ellipsis.)
    – Doc G.
    Apr 10, 2018 at 3:45
  • @DocG. I did already; I used a bunch of different test strings. My original string was u = Address.first. I used vim to write everything so it wasn't converting anything like some word processors do. Apr 10, 2018 at 3:47

1 Answer 1

1

I don't understand why this isn't working, however I do have a fix. I figured out that doing tell application "System Events" to key code 47 sends a period correctly.

So, I wrapped my read into a conditional like this:

on run {input, parameters}

    #set input_file to "/path/to/file"
    #set file_text to read input_file
    set file_text to "test: ... hello ..."
    delay 0.3
    repeat with ch in file_text
        if ch as string = "." then
            tell application "System Events" to key code 47
        else
            tell application "System Events" to keystroke ch
        end if
        delay 0.1
    end repeat

    return input
end run

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .