2

I'm currently trying to write an AppleScript for my Touchbar, which does copy a predefined variable into the clipboard and then pastes it into a word document.

Well, it does work, but for some reason in addition of just pasting the variable text, it also adds a Enter/NewLine to it. Now I already did some testing and just assigned plain text directly to the clipboard and the NewLine was still there, so it isn't the variable. I also tested the Cmd+V output and also when I just paste the text manually afterwards it still pastes it with an enter, so it seems like there is something wrong with the assignment. But since I'm new to AppleScript and the language is really weird, I don't really get what I'm doing wrong here. Here is the important code part:

activate application "Microsoft Word" --I'm doing this, but you prolly don't need to.
set Donnerstag12 to "Anwendungsentwicklung"
set the clipboard to Donnerstag12
tell application "System Events" to keystroke "v" using {command down}

Now I just noticed that when I let AppleScript paste into Pages, TextEdit or straight into the code, it doesn't include a NewLine. Any Ideas?

jweaks
  • 3,674
  • 1
  • 19
  • 32
derRAV3N
  • 25
  • 3

2 Answers2

0

It must be something that Word is doing with it's "Smart Cut & Paste" options, though I'm not seeing that behavior in Word 2011. You can look to alter that behavior in the advanced Advanced settings. You can also just alter your script to delete the paragraph marker when you're pasting into Word.

activate application "Microsoft Word" --I'm doing this, but you prolly don't need to.
set Donnerstag12 to "Anwendungsentwicklung"
set the clipboard to Donnerstag12
tell application "System Events"
    keystroke "v" using {command down}
    if name of (first process whose frontmost is true) is "Microsoft Word" then key code 51
end tell
jweaks
  • 3,674
  • 1
  • 19
  • 32
  • wow, this is absolute madness, I just noticed that Word does this with everything that could seem like a title. Looking through the options atm, but my hopes are low.. – derRAV3N Feb 01 '17 at 21:29
  • what do you mean by "could seem like a title"? Do you mean when you're currently pasting into the Title style? Why is my workaround not working? – jweaks Feb 01 '17 at 21:31
  • I mean for example when just pasting one word, not a whole sentence. Also when just pasting a Mac address (just used that for testing) there is no NewLine. Your workaround does work, yes, but I'd like to solve it without a workaround ;) – derRAV3N Feb 02 '17 at 07:49
0

You can use the type text command of Microsoft Word.

set Donnerstag12 to "Anwendungsentwicklung"
tell application "Microsoft Word" to type text selection text Donnerstag12
jackjr300
  • 7,111
  • 2
  • 15
  • 25
  • Nice, how many ways are there for typing text in? keystroke, key code and type text? I tried keystroke before, but as the A is in Caps, it also did write some letters after that in Caps for some reason. But type text seems to work fine. thank you! – derRAV3N Feb 02 '17 at 19:21
  • `type text` is a command for the **Word** application only, `keystroke` and `key code` are commands for the processes suite of the **"Sytem Events"** application. Some applications **AppleScriptable** are commands to insert text at the cursor position, the name of this command is different for each application. – jackjr300 Feb 03 '17 at 18:21